-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day03.kt
117 lines (109 loc) · 4.11 KB
/
Day03.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.File
import java.lang.IllegalArgumentException
fun main() {
val reader = File("files/day3.txt").bufferedReader()
val a = reader.readLine().split(",")
val b = reader.readLine().split(",")
println(Day03().part1(a, b))
println(Day03().part2(a, b))
}
class Day03 {
fun part1(aas: List<String>, bbs: List<String>): Int {
return makeSet(aas)
.intersect(makeSet(bbs))
.asSequence()
.minus(Pair(0, 0))
.toList()
.map { x -> Math.abs(x.first) + Math.abs(x.second) }
.sortedBy { x -> x }
.first()
}
fun part2(aas: List<String>, bbs: List<String>): Int {
val intersectionsSet = makeSet(aas)
.intersect(makeSet(bbs))
.asSequence()
.minus(Pair(0, 0))
val stepMapA = makeSetWithSteps(aas).filter { x -> x.first in intersectionsSet }.toMap()
val stepMapB = makeSetWithSteps(bbs).filter { x -> x.first in intersectionsSet }.toMap()
val res = intersectionsSet.map { x -> stepMapA.getOrDefault(x, 0) + stepMapB.getOrDefault(x, 0) }
return res.sorted().first()
}
private fun makeSet(xs: List<String>): Set<Pair<Int, Int>> {
val res: MutableSet<Pair<Int, Int>> = mutableSetOf()
var current = Pair(0, 0)
for (x in xs) {
val dir = x[0]
val value = x.substring(1).toInt()
when (dir) {
'R' -> {
for (i in 0..value) {
res.add(Pair(current.first + i, current.second))
}
current = Pair(current.first + value, current.second)
}
'L' -> {
for (i in 0..value) {
res.add(Pair(current.first - i, current.second))
}
current = Pair(current.first - value, current.second)
}
'U' -> {
for (i in 0..value) {
res.add(Pair(current.first, current.second + i))
}
current = Pair(current.first, current.second + value)
}
'D' -> {
for (i in 0..value) {
res.add(Pair(current.first, current.second - i))
}
current = Pair(current.first, current.second - value)
}
else -> throw IllegalArgumentException("no.")
}
}
return res
}
private fun makeSetWithSteps(xs: List<String>): Set<Pair<Pair<Int, Int>, Int>> {
val res: MutableSet<Pair<Pair<Int, Int>, Int>> = mutableSetOf()
var current = Pair(0, 0)
var c = 0
for (x in xs) {
c--
val dir = x[0]
val value = x.substring(1).toInt()
when (dir) {
'R' -> {
for (i in 0..value) {
c++
res.add(Pair(Pair(current.first + i, current.second), c))
}
current = Pair(current.first + value, current.second)
}
'L' -> {
for (i in 0..value) {
c++
res.add(Pair(Pair(current.first - i, current.second), c))
}
current = Pair(current.first - value, current.second)
}
'U' -> {
for (i in 0..value) {
c++
res.add(Pair(Pair(current.first, current.second + i), c))
}
current = Pair(current.first, current.second + value)
}
'D' -> {
for (i in 0..value) {
c++
res.add(Pair(Pair(current.first, current.second - i), c))
}
current = Pair(current.first, current.second - value)
}
else -> throw IllegalArgumentException("no.")
}
}
return res
}
}