-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
161 lines (138 loc) · 2.77 KB
/
main.lua
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
function main()
if (#arg ~= 2)
then
print("incorrect number of arguments")
return
end
if (arg[1] == "part01")
then
xpcall(part01, error_handler)
elseif (arg[1] == "part02")
then
xpcall(part02, error_handler)
end
end
function error_handler(err)
print("execution failed with: ", err)
end
function read_data()
local i = 1
local data = {}
for value in io.lines(arg[2])
do
data[i] = value
i = i + 1
end
return data
end
function find_minima(data)
local minima = {}
for y = 1,#data,1
do
for x = 1,#data[y],1
do
current = char_at(data[y], x)
if y > 1 and current >= char_at(data[y-1], x)
then
goto continue
end
if x > 1 and current >= char_at(data[y], x - 1)
then
goto continue
end
if y < #data and current >= char_at(data[y+1], x)
then
goto continue
end
if x < #data[y] and current >= char_at(data[y], x + 1)
then
goto continue
end
minima[#minima+1] = {y, x}
::continue::
end
end
return minima
end
function part01()
local data = read_data()
risk = 0
local minima = find_minima(data)
for i, m in ipairs(minima)
do
risk = risk + 1 + char_at(data[m[1]], m[2])
end
print(risk)
end
function part02()
local data = read_data()
local basin_data = {}
for y = 1,#data,1
do
basin_data[y] = {}
for x = 1,#data[y],1
do
basin_data[y][x] = char_at(data[y], x) + 0
end
end
local basins = {}
local minima = find_minima(data)
for i, m in ipairs(minima)
do
basins[i] = 1
basin_data[m[1]][m[2]] = -i
end
local current
local changed = true
while changed
do
changed = false
for y = 1,#basin_data,1
do
for x = 1,#basin_data[y],1
do
current = basin_data[y][x]
if current >= 0
then
goto continue
end
if y > 1 and basin_data[y-1][x] >= 0 and basin_data[y-1][x] < 9
then
basin_data[y-1][x] = current
basins[-current] = basins[-current] + 1
changed = true
end
if x > 1 and basin_data[y][x-1] >= 0 and basin_data[y][x-1] < 9
then
basin_data[y][x-1] = current
basins[-current] = basins[-current] + 1
changed = true
end
if y < #basin_data and basin_data[y+1][x] >= 0 and basin_data[y+1][x] < 9
then
basin_data[y+1][x] = current
basins[-current] = basins[-current] + 1
changed = true
end
if x < #basin_data[y] and basin_data[y][x+1] >= 0 and basin_data[y][x+1] < 9
then
basin_data[y][x+1] = current
basins[-current] = basins[-current] + 1
changed = true
end
::continue::
end
end
end
table.sort(basins, function(a, b)
return a > b
end)
print(basins[1] * basins[2] * basins[3])
end
function char_at(s, index)
return string.sub(s, index, index)
end
function replace_char_at(s, with, at)
return string.sub(s, 1, at - 1) .. with .. string.sub(s, at + 1)
end
main()