-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day14.kt
162 lines (137 loc) · 4.37 KB
/
Day14.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package day14
import common.InputRepo
import common.readSessionCookie
import common.solve
import util.splitIntoPair
fun main(args: Array<String>) {
val day = 14
val input = InputRepo(args.readSessionCookie()).get(day = day)
solve(day, input, ::solveDay14Part1, ::solveDay14Part2)
}
data class Pos(val x: Int, val y: Int)
data class Path(val a: Pos, val b: Pos) {
private var fixedX: Boolean = false
private var fixedAxis: Int = 0
private var points: IntRange = 0..0
init {
if (a.x == b.x) {
fixedX = true
fixedAxis = a.x
points = if (a.y < b.y) {
a.y..b.y
} else {
b.y..a.y
}
} else if (a.y == b.y) {
fixedX = false
fixedAxis = a.y
points = if (a.x < b.x) {
a.x..b.x
} else {
b.x..a.x
}
} else {
throw IllegalArgumentException("Invalid path")
}
}
fun getPositions(): List<Pos> {
return if (fixedX) {
points.map { Pos(fixedAxis, it) }
} else {
points.map { Pos(it, fixedAxis) }
}
}
}
fun solveDay14Part1(input: List<String>): Int {
val takenPositions = parseInput(input)
val sandOrigin = Pos(500, 0)
val floorLevel = takenPositions.maxOf { it.y }
return simulateSand(takenPositions, sandOrigin, floorLevel, fallThrough = true)
}
fun solveDay14Part2(input: List<String>): Int {
val takenPositions = parseInput(input)
val sandOrigin = Pos(500, 0)
val floorLevel = takenPositions.maxOf { it.y } + 2
return simulateSand(takenPositions, sandOrigin, floorLevel, fallThrough = false) + 1 // add one for source
}
fun printCave(source: Pos, current: Pos, points: Set<Pos>, depth: Int) {
for (y in 0..depth) {
for (x in 400..520) {
if (points.contains(Pos(x, y))) {
print("#")
} else if (current.x == x && current.y == y) {
print("o")
} else if (source.x == x && source.y == y) {
print("+")
} else {
print(".")
}
}
println()
}
println()
}
fun simulateSingleSandBlock(points: Set<Pos>, sandOrigin: Pos, floorLevel: Int, fallThrough: Boolean): Pos? {
var currentSandPos = sandOrigin
var nextSandPos = moveSand(currentSandPos, points)
while (nextSandPos != currentSandPos) {
currentSandPos = nextSandPos
nextSandPos = moveSand(currentSandPos, points)
if (nextSandPos.y > floorLevel) {
// freefall detected
break
}
if (!fallThrough && nextSandPos.y == floorLevel) {
// cant go through floor
return currentSandPos
}
if (nextSandPos == currentSandPos) {
// we settled
return currentSandPos
}
if (nextSandPos == sandOrigin) {
return null
}
}
return null
}
fun moveSand(currentSandPos: Pos, points: Set<Pos>): Pos {
val down = Pos(currentSandPos.x, currentSandPos.y + 1)
val downLeft = Pos(currentSandPos.x - 1, currentSandPos.y + 1)
val downRight = Pos(currentSandPos.x + 1, currentSandPos.y + 1)
return if (down !in points) {
down
} else if (downLeft !in points) {
downLeft
} else if (downRight !in points) {
downRight
} else {
currentSandPos
}
}
private fun simulateSand(
takenPositions: MutableSet<Pos>, sandOrigin: Pos, floorLevel: Int, fallThrough: Boolean
): Int {
var cnt = 0
do {
val block = simulateSingleSandBlock(takenPositions, sandOrigin, floorLevel, fallThrough)
if (block != null) {
cnt++
if (cnt % 1000 == 0) {
printCave(sandOrigin, block, takenPositions, depth = floorLevel + 1)
}
takenPositions.add(block)
}
} while (block != null)
printCave(sandOrigin, sandOrigin, takenPositions, floorLevel + 1)
return cnt
}
private fun parseInput(input: List<String>): MutableSet<Pos> {
val takenPositions = input.flatMap {
it.split(" -> ").map {
val p = it.trim().splitIntoPair(",")
Pos(p.first.toInt(), p.second.toInt())
}.zipWithNext { a, b -> Path(a, b) }
}.flatMap { it.getPositions().toSet() }.toMutableSet()
return takenPositions
}