-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.kt
57 lines (52 loc) · 1.85 KB
/
Day2.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package aoc2016.day02
import util.Coord
import util.readInputLineByLine
val deltas = mapOf('D' to Coord(0, 1), 'U' to Coord(0, -1), 'R' to Coord(1, 0), 'L' to Coord(-1, 0))
fun readInputAndDial(path: String, isPart2: Boolean = false): String {
var code = ""
for (line in readInputLineByLine(path)) {
code += if (isPart2) findNumberPart2(line) else findNumber(line)
}
return code
}
// 1
// 2 3 4
// 5 6 7 8 9
// A B C
// D
fun findNumberPart2(input: String): Char {
var coord = Coord(0, 0)
val keypad = mapOf(
Coord(0, -2) to '1',
Coord(-1, -1) to '2', Coord(0, -1) to '3', Coord(1, -1) to '4',
Coord(-2, 0) to '5', Coord(-1, 0) to '6', Coord(0, 0) to '7', Coord(1, 0) to '8', Coord(2, 0) to '9',
Coord(-1, 1) to 'A', Coord(0, 1) to 'B', Coord(1, 1) to 'C',
Coord(0, 2) to 'D'
)
for (char in input) {
val delta = deltas[char]!!
val newCoord = Coord(coord.x + delta.x, coord.y + delta.y)
if (newCoord in keypad.keys) {
coord = newCoord
}
}
return keypad[coord]!!
}
fun findNumber(input: String): Int {
var coord = Coord(0, 0)
val keypad = mapOf(
Coord(-1, -1) to 1, Coord(0, -1) to 2, Coord(1, -1) to 3,
Coord(-1, 0) to 4, Coord(0, 0) to 5, Coord(1, 0) to 6,
Coord(-1, 1) to 7, Coord(0, 1) to 8, Coord(1, 1) to 9
)
for (char in input) {
val delta = deltas[char]!!
val newX = coord.x + delta.x
val newY = coord.y + delta.y
coord = Coord(
if (newX == 2) 1 else if (newX == -2) -1 else newX,
if (newY == 2) 1 else if (newY == -2) -1 else newY
)
}
return keypad[coord]!!
}