File tree Expand file tree Collapse file tree 3 files changed +858
-0
lines changed Expand file tree Collapse file tree 3 files changed +858
-0
lines changed Original file line number Diff line number Diff line change 1+ fun main () {
2+ val input = object {}.javaClass.getResource(" input-5.txt" ).readText().split(' \n ' )
3+
4+ val ids = input.map {
5+ val row = findBinary(128 , it.substring(0 , 7 ))
6+ val column = findBinary(8 , it.substring(7 ))
7+ (row * 8 ) + column
8+ }.sorted()
9+
10+ ids.forEachIndexed { index, id ->
11+ if (index != 0 && index != ids.size - 1 && ids[index] == ids[index + 1 ] - 2 ) {
12+ println (id + 1 )
13+ return
14+ }
15+ }
16+ }
17+
Original file line number Diff line number Diff line change 1+ fun main () {
2+ val input = object {}.javaClass.getResource(" input-5.txt" ).readText().split(' \n ' )
3+
4+ input.map {
5+ val row = findBinary(128 , it.substring(0 , 7 ))
6+ val column = findBinary(8 , it.substring(7 ))
7+ (row * 8 ) + column
8+ }.maxOrNull().also (::println)
9+ }
10+
11+ fun findBinary (max : Int , key : String ): Int {
12+ var l = 0
13+ var r = max
14+ var idx = 0
15+ while (idx < key.length) {
16+ when (key[idx]) {
17+ ' F' , ' L' -> r = (l+ r)/ 2
18+ ' B' , ' R' -> l = (l+ r)/ 2
19+ }
20+ idx++
21+ }
22+ return l
23+ }
24+
You can’t perform that action at this time.
0 commit comments