-
Notifications
You must be signed in to change notification settings - Fork 1
/
day-9.toit
77 lines (64 loc) · 1.98 KB
/
day-9.toit
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
import .file as file
import reader show BufferedReader
get grid/List x/int y/int -> int:
if x < 0 or y < 0: return 10
if x >= grid[0].size: return 10
if y >= grid.size: return 10
return grid[y][x]
main:
reader := BufferedReader
file.Stream.for_read "9.txt"
grid := []
while line := reader.read_line:
grid.add
(line.split "").map: int.parse it
danger_level := 0
h := grid.size
w := grid[0].size
basin_sizes := []
for y := 0; y < h; y++:
for x := 0; x < w; x++:
up := get grid x y - 1
down := get grid x y + 1
left := get grid x - 1 y
right := get grid x + 1 y
current := get grid x y
if up > current and down > current and left > current and right > current:
danger_level += 1 + current
basin := {Coordinate x y}
iterations := [basin]
while true:
new := {}
iterations[iterations.size - 1].do: | coord |
ex := coord.x
ey := coord.y
edge := get grid ex ey
height := get grid ex - 1 ey
if height > edge and height < 9: new.add (Coordinate ex - 1 ey)
height = get grid ex + 1 ey
if height > edge and height < 9: new.add (Coordinate ex + 1 ey)
height = get grid ex ey - 1
if height > edge and height < 9: new.add (Coordinate ex ey - 1)
height = get grid ex ey + 1
if height > edge and height < 9: new.add (Coordinate ex ey + 1)
if new.size > 0:
iterations.add new
else:
break
all := {}
iterations.do: all.add_all it
basin_sizes.add
all.size
print danger_level
basin_sizes.sort --in_place: | a b | b - a
print basin_sizes[0] * basin_sizes[1] * basin_sizes[2]
class Coordinate:
x /int
y /int
constructor .x .y:
hash_code -> int:
return x + y * 1000
operator == other -> bool:
return x == other.x and y == other.y
stringify -> string:
return "[$x,$y]"