-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.kts
152 lines (138 loc) · 3.41 KB
/
Day17.kts
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
import java.io.File
import java.util.*
import kotlin.system.exitProcess
// input processing
val inputs = File("inputs/day17.txt").readLines()
var minX = Int.MAX_VALUE
var maxX = Int.MIN_VALUE
var minY = Int.MAX_VALUE
var maxY = Int.MIN_VALUE
val records = inputs.map {
val r = IntArray(4)
val parts = it.split(", ")
val part1 = parts[0].split("=")
val v1 = part1[1].toInt()
val parts2 = parts[1].split("=")[1].split("..")
val v2 = parts2[0].toInt()
val v3 = parts2[1].toInt()
if (part1[0] == "x") {
updateX(v1)
updateY(v2)
updateY(v3)
} else {
r[0] = 1
updateY(v1)
updateX(v2)
updateX(v3)
}
r[1] = v1
r[2] = v2
r[3] = v3
r
}
val rowNum = maxY + 1
val colNum = maxX + 1
val map = Array(rowNum, { CharArray(colNum, { '.' } ) })
map[0][500] = '+'
for (r in records) {
for (i in r[2]..r[3]) {
if (r[0] == 1) {
map[r[1]][i] = '#'
} else {
map[i][r[1]] = '#'
}
}
}
val source: Queue<Pair<Int, Int>> = LinkedList<Pair<Int, Int>>()
source.add(Pair(0, 500))
while(!source.isEmpty()) {
val curr = source.poll()
move(curr.first, curr.second)
}
fun move(row: Int, col: Int) {
val touch = goDown(row, col)
if (touch != null) {
for (p in spread(touch.first, touch.second)) source.add(p)
}
}
fun spread(r: Int, c: Int): List<Pair<Int, Int>> {
if (map[r][c] == '~') return emptyList()
var cLeft = c
var cRight = c
map[r][c] = '~'
while(map[r][cLeft - 1] != '#' && map[r+1][cLeft - 1] != '.' && map[r+1][cLeft - 1] != '|') {
cLeft--
map[r][cLeft] = '~'
}
while(map[r][cRight + 1] != '#' && map[r+1][cRight + 1] != '.' && map[r+1][cRight + 1] != '|') {
cRight++
map[r][cRight] = '~'
}
if (map[r][cLeft-1] == '#' && map[r][cRight+1] == '#') return spread(r-1, c)
val ans = mutableListOf<Pair<Int, Int>>()
if (map[r][cLeft-1] == '.') {
for (i in cLeft-1..cRight) map[r][i] = '|'
map[r][cLeft-1] = '|'
ans.add(Pair(r, cLeft-1))
}
if (map[r][cRight+1] == '.') {
for (i in cLeft..cRight+1) map[r][i] = '|'
map[r][cRight+1] = '|'
ans.add(Pair(r, cRight+1))
}
return ans
}
fun goDown(r: Int, c: Int): Pair<Int, Int>? {
var row = r
while(row+1 < rowNum && map[row+1][c] != '#'){
map[row+1][c] = '|'
row++
}
if (row+1 == rowNum) return null
else return Pair(row, c)
}
printMap()
fun printMap() {
var ans = 0
for (i in minY..maxY) {
for (j in (minX -2)..maxX) {
print(map[i][j])
if (map[i][j] == '~' || map[i][j] == '|') ans++
}
println()
}
println(ans)
}
count()
fun count() {
var ans = 0
for (i in minY..maxY) {
var j = minX
var started = false
var idx = 0
while (j<= maxX) {
if (map[i][j] != '#') {
if (map[i][j] != '~') started = false
j++
continue
}
else if (started) {
ans += j-idx
idx = j+1
} else {
started = true
idx = j+1
}
j++
}
}
println(ans)
}
fun updateX(v: Int) {
minX = Math.min(minX, v)
maxX = Math.max(maxX, v)
}
fun updateY(v: Int) {
minY = Math.min(minY, v)
maxY = Math.max(maxY, v)
}