-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.lua
190 lines (168 loc) · 4.11 KB
/
day14.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'util'
DAY = 14
local filename = string.format("inputs/input_%02d.txt", DAY)
local f = assert(io.open(filename, "r"))
-- Function definitions here
local data = {}
local function spin(data)
-- North, West, South, East
-- N
for col = 1, #data[1] do
local flr = 1
for row = 1, #data do
local rock = data[row][col]
if rock == "O" then
data[row][col] = "."
data[flr][col] = "O"
flr = flr + 1
elseif rock == "#" then
flr = row + 1
end
end
end
-- W
for row = 1, #data do
local flr = 1
for col = 1, #data[row] do
local rock = data[row][col]
if rock == "O" then
data[row][col] = "."
data[row][flr] = "O"
flr = flr + 1
elseif rock == "#" then
flr = col + 1
end
end
end
--S
for col = 1, #data[1] do
local flr = #data
for row = #data, 1, -1 do
local rock = data[row][col]
if rock == "O" then
data[row][col] = "."
data[flr][col] = "O"
flr = flr - 1
elseif rock == "#" then
flr = row - 1
end
end
end
--E
for row = 1, #data do
local flr = #data[row]
for col = #data[row], 1, -1 do
local rock = data[row][col]
if rock == "O" then
data[row][col] = "."
data[row][flr] = "O"
flr = flr - 1
elseif rock == "#" then
flr = col - 1
end
end
end
end
local function hash(data)
local res = ""
for i, v in ipairs(data) do
for j, k in ipairs(v) do
res = res .. k
end
end
return res
end
local function unHash(str, w, h)
local board = {}
local row = 1
local col = 1
for c in chars(str) do
if not board[row] then board[row] = {} end
board[row][col] = c
col = col + 1
if col > w then
col = 1
row = row + 1
end
end
return board
end
local function getNorthLoad(data)
local res = 0
local countRound = 0
for col = 1, #data[1] do
countRound = 0
for row = #data, 1, -1 do
local rock = data[row][col]
if rock == "O" then
countRound = countRound + 1
elseif rock == "#" then
if countRound ~= 0 then
local load = arithSum(#data - row - countRound + 1, #data - row)
res = res + load
countRound = 0
end
end
end
if countRound ~= 0 then
local load = arithSum(#data - countRound + 1, #data)
res = res + load
end
end
return res
end
local function getLoad(data)
local res = 0
for d = #data, 1, -1 do
for col = 1, #data do
if data[#data - d + 1][col] == "O" then
res = res + d
end
end
end
return res
end
for line in f:lines() do
-- Process the file here
local row = {}
for c in chars(line) do
row[#row + 1] = c
end
data[#data + 1] = row
end
-- Do everything else here
local part1 = 0
local part2 = 0
-- Work our way from the bottom to the top for each column
part1 = getNorthLoad(data)
-- Cycle detection
local cycle = {}
local next = {}
local h
for _ = 1,1000000000 do
h = hash(data)
if next[h] then
break -- We found a cycle!!!!
else
spin(data)
next[h] = hash(data)
cycle[#cycle+1] = h
end
end
-- Index of where the cycle starts
local start = 0
for i, v in ipairs(cycle) do
if v == h then start = i end
end
-- How long the cycle is
local cLen = 0
local s = h
while true do
cLen = cLen + 1
h = next[h]
if h == s then break end
end
local dataHash = cycle[start + (1000000000 - start) % cLen + 1]
part2 = getLoad(unHash(dataHash, #data[1], #data))
print("Part 1:", part1)
print("Part 2:", part2)