-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.lua
49 lines (37 loc) · 1.02 KB
/
day09.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
require 'util'
DAY = 9
local filename = string.format("inputs/input_%02d.txt", DAY)
local f = assert(io.open(filename, "r"))
-- Function definitions here
local data = {}
local function extrapolate(seq) --Extrapolates the values at both the front and back
if #seq == 0 then return { 0 } end -- Assuming this basecase
local allZero = seq[1] == 0
local next = {}
for i = 2, #seq do
next[#next + 1] = seq[i] - seq[i - 1]
allZero = allZero and seq[i] == 0
end
next = extrapolate(next)
seq[#seq + 1] = next[#next] + seq[#seq]
table.insert(seq, 1, seq[1] - next[1])
return seq
end
for line in f:lines() do
-- Process the file here
local row = {}
for n in split(line, " ") do
row[#row + 1] = tonumber(n)
end
data[#data + 1] = row
end
-- Do everything else here
local part1 = 0
local part2 = 0
for i, v in ipairs(data) do
local e = extrapolate(v)
part1 = part1 + e[#e]
part2 = part2 + e[1]
end
print("Part 1:", part1)
print("Part 2:", part2)