generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.kt
137 lines (111 loc) · 3.48 KB
/
Day05.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package year2021.`05`
import readInput
private data class AdventPoint(
val x: Int,
val y: Int
) {
override fun toString(): String = "($x,$y)"
operator fun plus(other: AdventPoint): AdventPoint {
return this.copy(
x = x + other.x,
y = y + other.y
)
}
}
private data class CustomLine(
val from: AdventPoint,
val to: AdventPoint
) {
override fun toString(): String = "$from->$to"
}
fun main() {
fun parse(input: List<String>): List<CustomLine> {
return input.map {
fun String.toPoint(): AdventPoint {
val (x, y) = split(",").map { it.trimStart().trimEnd().toInt() }
return AdventPoint(x, y)
}
val (first, second) = it.split("->")
val fromPoint = first.toPoint()
val toPoint = second.toPoint()
CustomLine(fromPoint, toPoint)
}
}
fun MutableMap<AdventPoint, Int>.incrementCounter(point: AdventPoint) {
if (this.containsKey(point)) {
this[point] = this[point]?.inc() ?: error("Impossible state")
} else {
this[point] = 1
}
}
fun between(line: CustomLine): Set<AdventPoint> {
val from = line.from
val to = line.to
val deltaPoint = AdventPoint(
x = when {
from.x > to.x -> -1
from.x < to.x -> 1
else -> error("ILLEGAL")
},
y = when {
from.y > to.y -> -1
from.y < to.y -> 1
else -> error("ILLEGAL")
}
)
val mutableSet = mutableSetOf<AdventPoint>()
var currentPoint = from
do {
mutableSet.add(currentPoint)
currentPoint += deltaPoint
} while ((currentPoint != to))
mutableSet.add(currentPoint)
return mutableSet
}
fun processItem(
line: CustomLine,
mutableSet: MutableMap<AdventPoint, Int>,
hasDiagonal: Boolean = false
) {
val fromX = line.from.x
val toX = line.to.x
val fromY = line.from.y
val toY = line.to.y
when {
fromX == toX -> (minOf(fromY, toY)..maxOf(fromY, toY)).forEach {
mutableSet.incrementCounter(AdventPoint(fromX, it))
}
fromY == toY -> (minOf(fromX, toX)..maxOf(fromX, toX)).forEach {
mutableSet.incrementCounter(AdventPoint(it, fromY))
}
else -> if (hasDiagonal) {
between(line).forEach { mutableSet.incrementCounter(it) }
} else {
Unit
}
}
}
fun part1(input: List<String>): Int {
val lines = parse(input)
val mapOf = mutableMapOf<AdventPoint, Int>()
lines.forEach { processItem(it, mapOf) }
return mapOf.count { it.value >= 2 }
}
fun part2(input: List<String>): Int {
val lines = parse(input)
val mapOf = mutableMapOf<AdventPoint, Int>()
lines.forEach { processItem(it, mapOf, true) }
return mapOf.count { it.value >= 2 }
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day05_test")
val part1Test = part1(testInput)
val part2Test = part2(testInput)
println(part1Test)
println(part2Test)
check(part1Test == 5)
check(part2Test == 12)
val input = readInput("Day05")
println(part1(input))
println(part2(input))
}