Skip to content

Commit 003fcce

Browse files
committed
Add day 14
1 parent 8e4c9f7 commit 003fcce

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

2022/14/14.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
fun main() {
2+
val lines = generateSequence(::readlnOrNull).toList()
3+
.map { it.split(" -> ", ",").map { it.toInt() }.chunked(2) }
4+
val width = lines.flatten().maxOf { it[0] } + 200
5+
val height = lines.flatten().maxOf { it[1] } + 2
6+
val field = MutableList(height) { MutableList(width) { '.' } }
7+
fun range(a: Int, b: Int) = minOf(a, b)..maxOf(a, b)
8+
fun drawLine(rx: IntRange, ry: IntRange) = rx.map { x -> ry.map { y -> field[y][x] = '#' } }
9+
lines.map { it.zipWithNext { (x1, y1), (x2, y2) -> drawLine(range(x1, x2), range(y1, y2))} }
10+
11+
fun fill(): Int {
12+
var endDfs = false
13+
fun dfs(x: Int, y: Int): Boolean {
14+
endDfs = endDfs || y >= field.size || field[0][500] == 'O'
15+
if (endDfs || field[y][x] != '.')
16+
return endDfs
17+
if (!(dfs(x, y+1) || dfs(x-1, y+1) || dfs(x+1, y+1)))
18+
field[y][x] = 'O'
19+
return true
20+
}
21+
while (!endDfs) dfs(500, 0)
22+
return field.flatten().count { it == 'O' }
23+
}
24+
println(fill())
25+
field.add(MutableList(width) { '#' })
26+
println(fill())
27+
}

Media/2022/14.png

-57 Bytes
Loading

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<a href="2022/13/13.kt">
4646
<img src="Media/2022/13.png" width="161px">
4747
</a>
48-
<a href="2022/14/14.py">
48+
<a href="2022/14/14.kt">
4949
<img src="Media/2022/14.png" width="161px">
5050
</a>
5151
<h1 align="center">

0 commit comments

Comments
 (0)