-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.lua
More file actions
72 lines (58 loc) · 1.72 KB
/
day13.lua
File metadata and controls
72 lines (58 loc) · 1.72 KB
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
require 'util'
DAY = 13
local filename = string.format("inputs/input_%02d.txt", DAY)
local f = assert(io.open(filename, "r"))
-- Function definitions here
local data = {}
local function getSymettryIndex(chunk, change)
-- For every possible index we bubble out, make the change we need to, and then return if success
for i = 1, #chunk - 1 do
local changed = change and true or false
local correct = true
for d = 0, #chunk do
local l1 = chunk[i - d]
local l2 = chunk[i + d + 1]
if l1 == nil or l2 == nil then break end
if l1 ~= l2 then
if changed then
correct = false
break
end
for j = 1, #l1 do
if l1:sub(j, j) ~= l2:sub(j, j) then
if changed then
correct = false
break
end
changed = true
end
end
end
end
if changed and correct then
return i
end
end
return 0
end
local chunk = {}
for line in f:lines() do
-- Process the file here
if #line == 0 then
data[#data+1] = chunk
chunk = {}
else
chunk[#chunk+1] = line
end
end
-- Add the last chunk
data[#data+1] = chunk
-- Do everything else here
local part1 = 0
local part2 = 0
for _, chunk in ipairs(data) do
part1 = part1 + getSymettryIndex(chunk, true) * 100 + getSymettryIndex(get_columns(chunk), true)
part2 = part2 + getSymettryIndex(chunk) * 100 + getSymettryIndex(get_columns(chunk))
end
print("Part 1:", part1)
print("Part 2:", part2)