Compare commits
10 Commits
bd36baa1ec
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
fbc2bc41e8
|
|||
|
d0483124ac
|
|||
|
250be0f242
|
|||
|
9f10ee83be
|
|||
|
c516532183
|
|||
|
3dedc1da03
|
|||
|
472634054f
|
|||
|
39c03d9920
|
|||
|
0add2ecd03
|
|||
|
d9ac0c7a9f
|
@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
|
||||
## Progress
|
||||
|
||||
<!-- calendar-start -->
|
||||
#### Stars: 10/24
|
||||
#### Stars: 20/24
|
||||
|
||||
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|
||||
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|
||||
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>:star::star:|5<br>:star::star:|6<br>|7<br>|
|
||||
|8<br>|9<br>|10<br>|11<br>|12<br>|||
|
||||
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>:star::star:|5<br>:star::star:|6<br>:star::star:|7<br>:star::star:|
|
||||
|8<br>:star::star:|9<br>:star:|10<br>:star:|11<br>:star::star:|12<br>|||
|
||||
<!-- calendar-end -->
|
||||
|
||||
4
res/examples/day06.txt
Normal file
4
res/examples/day06.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
123 328 51 64
|
||||
45 64 387 23
|
||||
6 98 215 314
|
||||
* + * +
|
||||
16
res/examples/day07.txt
Normal file
16
res/examples/day07.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
.......S.......
|
||||
...............
|
||||
.......^.......
|
||||
...............
|
||||
......^.^......
|
||||
...............
|
||||
.....^.^.^.....
|
||||
...............
|
||||
....^.^...^....
|
||||
...............
|
||||
...^.^...^.^...
|
||||
...............
|
||||
..^...^.....^..
|
||||
...............
|
||||
.^.^.^.^.^...^.
|
||||
...............
|
||||
20
res/examples/day08.txt
Normal file
20
res/examples/day08.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
162,817,812
|
||||
57,618,57
|
||||
906,360,560
|
||||
592,479,940
|
||||
352,342,300
|
||||
466,668,158
|
||||
542,29,236
|
||||
431,825,988
|
||||
739,650,466
|
||||
52,470,668
|
||||
216,146,977
|
||||
819,987,18
|
||||
117,168,530
|
||||
805,96,715
|
||||
346,949,466
|
||||
970,615,88
|
||||
941,993,340
|
||||
862,61,35
|
||||
984,92,344
|
||||
425,690,689
|
||||
8
res/examples/day09.txt
Normal file
8
res/examples/day09.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
7,1
|
||||
11,1
|
||||
11,7
|
||||
9,7
|
||||
9,5
|
||||
2,5
|
||||
2,3
|
||||
7,3
|
||||
3
res/examples/day10.txt
Normal file
3
res/examples/day10.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}
|
||||
[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2}
|
||||
[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}
|
||||
13
res/examples/day11.txt
Normal file
13
res/examples/day11.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
svr: aaa bbb
|
||||
aaa: fft
|
||||
fft: ccc
|
||||
bbb: tty
|
||||
tty: ccc
|
||||
ccc: ddd eee
|
||||
ddd: hub
|
||||
hub: fff
|
||||
eee: dac
|
||||
dac: fff
|
||||
fff: ggg hhh
|
||||
ggg: out
|
||||
hhh: out
|
||||
10
res/examples/day11_1.txt
Normal file
10
res/examples/day11_1.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
aaa: you hhh
|
||||
you: bbb ccc
|
||||
bbb: ddd eee
|
||||
ccc: ddd eee fff
|
||||
ddd: ggg
|
||||
eee: out
|
||||
fff: out
|
||||
ggg: out
|
||||
hhh: ccc fff iii
|
||||
iii: out
|
||||
@@ -20,28 +20,28 @@
|
||||
"puzzle2": true
|
||||
},
|
||||
"day06": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day07": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day08": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day09": {
|
||||
"puzzle1": false,
|
||||
"puzzle1": true,
|
||||
"puzzle2": false
|
||||
},
|
||||
"day10": {
|
||||
"puzzle1": false,
|
||||
"puzzle1": true,
|
||||
"puzzle2": false
|
||||
},
|
||||
"day11": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day12": {
|
||||
"puzzle1": false,
|
||||
|
||||
39
src/day06/puzzle1.lua
Normal file
39
src/day06/puzzle1.lua
Normal file
@@ -0,0 +1,39 @@
|
||||
local strings = require "cc.strings"
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local lines = strings.split(input, "\n")
|
||||
local problems = {}
|
||||
local n = #lines
|
||||
|
||||
local ops = strings.split(lines[n], "%s+")
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
local j = 1
|
||||
for num in line:gmatch("(%d+)") do
|
||||
local op = ops[j]
|
||||
if i == 1 then
|
||||
if op == "+" then
|
||||
table.insert(problems, 0)
|
||||
else
|
||||
table.insert(problems, 1)
|
||||
end
|
||||
end
|
||||
local val = tonumber(num)
|
||||
if op == "+" then
|
||||
problems[j] = problems[j] + val
|
||||
else
|
||||
problems[j] = problems[j] * val
|
||||
end
|
||||
j = j + 1
|
||||
end
|
||||
end
|
||||
|
||||
local total = 0
|
||||
for _, val in ipairs(problems) do
|
||||
total = total + val
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
58
src/day06/puzzle2.lua
Normal file
58
src/day06/puzzle2.lua
Normal file
@@ -0,0 +1,58 @@
|
||||
local strings = require "cc.strings"
|
||||
local puzzle2 = {}
|
||||
|
||||
function puzzle2.parseNumbers(lines)
|
||||
local numbers = {}
|
||||
local n = #lines
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
if i ~= n then
|
||||
for j=1, #line do
|
||||
local c = line:sub(j, j)
|
||||
if #numbers < j then
|
||||
table.insert(numbers, {})
|
||||
end
|
||||
if c ~= " " then
|
||||
table.insert(numbers[j], c)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local problems = {{}}
|
||||
for _, num in ipairs(numbers) do
|
||||
if #num == 0 then
|
||||
table.insert(problems, {})
|
||||
else
|
||||
local value = tonumber(table.concat(num, ""))
|
||||
table.insert(problems[#problems], value)
|
||||
end
|
||||
end
|
||||
return problems
|
||||
end
|
||||
|
||||
function puzzle2.solve(input)
|
||||
local lines = strings.split(input, "\n")
|
||||
local values = puzzle2.parseNumbers(lines)
|
||||
local ops = strings.split(lines[#lines], "%s+")
|
||||
|
||||
local total = 0
|
||||
for p, numbers in ipairs(values) do
|
||||
local op = ops[p]
|
||||
local value = 0
|
||||
if op == "*" then
|
||||
value = 1
|
||||
end
|
||||
for _, num in ipairs(numbers) do
|
||||
if op == "+" then
|
||||
value = value + num
|
||||
else
|
||||
value = value * num
|
||||
end
|
||||
end
|
||||
total = total + value
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
return puzzle2
|
||||
34
src/day07/puzzle1.lua
Normal file
34
src/day07/puzzle1.lua
Normal file
@@ -0,0 +1,34 @@
|
||||
local utils = require "utils"
|
||||
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local lines = utils.splitLines(input)
|
||||
|
||||
local beams = {}
|
||||
local totalSplits = 0
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
local newBeams = {}
|
||||
if i == 1 then
|
||||
newBeams[line:find("S")] = true
|
||||
else
|
||||
for j=1, #line do
|
||||
if beams[j] then
|
||||
if line:sub(j, j) == "^" then
|
||||
totalSplits = totalSplits + 1
|
||||
newBeams[j - 1] = true
|
||||
newBeams[j + 1] = true
|
||||
else
|
||||
newBeams[j] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
beams = newBeams
|
||||
end
|
||||
|
||||
return totalSplits
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
37
src/day07/puzzle2.lua
Normal file
37
src/day07/puzzle2.lua
Normal file
@@ -0,0 +1,37 @@
|
||||
local utils = require "utils"
|
||||
|
||||
local puzzle2 = {}
|
||||
|
||||
function puzzle2.solve(input)
|
||||
local lines = utils.splitLines(input)
|
||||
|
||||
local beams = {}
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
local newBeams = {}
|
||||
if i == 1 then
|
||||
newBeams[line:find("S")] = 1
|
||||
else
|
||||
for j=1, #line do
|
||||
if beams[j] then
|
||||
if line:sub(j, j) == "^" then
|
||||
newBeams[j - 1] = (newBeams[j - 1] or 0) + beams[j]
|
||||
newBeams[j + 1] = (newBeams[j + 1] or 0) + beams[j]
|
||||
else
|
||||
newBeams[j] = (newBeams[j] or 0) + beams[j]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
beams = newBeams
|
||||
end
|
||||
|
||||
local finalBeams = 0
|
||||
for _, v in pairs(beams) do
|
||||
finalBeams = finalBeams + v
|
||||
end
|
||||
|
||||
return finalBeams
|
||||
end
|
||||
|
||||
return puzzle2
|
||||
82
src/day08/puzzle1.lua
Normal file
82
src/day08/puzzle1.lua
Normal file
@@ -0,0 +1,82 @@
|
||||
local utils = require "utils"
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.dist(box1, box2)
|
||||
return math.sqrt(
|
||||
(box2.x - box1.x) ^ 2 +
|
||||
(box2.y - box1.y) ^ 2 +
|
||||
(box2.z - box1.z) ^ 2
|
||||
)
|
||||
end
|
||||
|
||||
function puzzle1.pretty(box)
|
||||
return ("%d,%d,%d"):format(box.x, box.y, box.z)
|
||||
end
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local boxes = {}
|
||||
|
||||
local lines = utils.splitLines(input)
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
local x, y, z = line:match("(%d+),(%d+),(%d+)")
|
||||
table.insert(boxes, {x=x, y=y, z=z, i=i, circuit=i})
|
||||
end
|
||||
|
||||
local dists = {}
|
||||
local circuits = {}
|
||||
|
||||
for i, box1 in ipairs(boxes) do
|
||||
circuits[i] = i
|
||||
for j=i+1, #boxes do
|
||||
local box2 = boxes[j]
|
||||
table.insert(dists, {
|
||||
i=i,
|
||||
j=j,
|
||||
dist=puzzle1.dist(box1, box2)
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
table.sort(dists, function (a, b)
|
||||
return a.dist < b.dist
|
||||
end)
|
||||
|
||||
local N = 1000
|
||||
if #boxes < 100 then
|
||||
N = 10
|
||||
end
|
||||
|
||||
for i, d in ipairs(dists) do
|
||||
if i > N then
|
||||
break
|
||||
end
|
||||
local circ1 = circuits[d.i]
|
||||
local circ2 = circuits[d.j]
|
||||
|
||||
for k, v in pairs(circuits) do
|
||||
if v == circ2 then
|
||||
circuits[k] = circ1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local circuitSizes = {}
|
||||
|
||||
for _, c in pairs(circuits) do
|
||||
circuitSizes[c] = (circuitSizes[c] or 0) + 1
|
||||
end
|
||||
|
||||
local sizes = {}
|
||||
|
||||
for _, s in pairs(circuitSizes) do
|
||||
table.insert(sizes, s)
|
||||
end
|
||||
|
||||
table.sort(sizes)
|
||||
local n = #sizes
|
||||
|
||||
return sizes[n] * sizes[n - 1] * sizes[n - 2]
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
52
src/day08/puzzle2.lua
Normal file
52
src/day08/puzzle2.lua
Normal file
@@ -0,0 +1,52 @@
|
||||
local utils = require "utils"
|
||||
local puzzle1 = require(SRC_PATH .. "/day08/puzzle1")
|
||||
local puzzle2 = {}
|
||||
|
||||
function puzzle2.solve(input)
|
||||
local boxes = {}
|
||||
|
||||
local lines = utils.splitLines(input)
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
local x, y, z = line:match("(%d+),(%d+),(%d+)")
|
||||
table.insert(boxes, {x=x, y=y, z=z, i=i, circuit=i})
|
||||
end
|
||||
|
||||
local dists = {}
|
||||
local circuits = {}
|
||||
|
||||
for i, box1 in ipairs(boxes) do
|
||||
circuits[i] = i
|
||||
for j=i+1, #boxes do
|
||||
local box2 = boxes[j]
|
||||
table.insert(dists, {
|
||||
i=i,
|
||||
j=j,
|
||||
dist=puzzle1.dist(box1, box2)
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
table.sort(dists, function (a, b)
|
||||
return a.dist < b.dist
|
||||
end)
|
||||
|
||||
for _, d in ipairs(dists) do
|
||||
local circ1 = circuits[d.i]
|
||||
local circ2 = circuits[d.j]
|
||||
|
||||
local connex = true
|
||||
for k, v in pairs(circuits) do
|
||||
if v == circ2 then
|
||||
circuits[k] = circ1
|
||||
elseif v ~= circ1 then
|
||||
connex = false
|
||||
end
|
||||
end
|
||||
if connex then
|
||||
return boxes[d.i].x * boxes[d.j].x
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return puzzle2
|
||||
30
src/day09/puzzle1.lua
Normal file
30
src/day09/puzzle1.lua
Normal file
@@ -0,0 +1,30 @@
|
||||
local utils = require "utils"
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.area(tile1, tile2)
|
||||
local dx = math.abs(tile2.x - tile1.x) + 1
|
||||
local dy = math.abs(tile2.y - tile1.y) + 1
|
||||
return dx * dy
|
||||
end
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local lines = utils.splitLines(input)
|
||||
local tiles = {}
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
local x, y = line:match("(%d+),(%d+)")
|
||||
table.insert(tiles, {x=tonumber(x), y=tonumber(y)})
|
||||
end
|
||||
|
||||
local maxArea = 0
|
||||
for i, tile1 in ipairs(tiles) do
|
||||
for j=i+1, #tiles do
|
||||
local tile2 = tiles[j]
|
||||
local area = puzzle1.area(tile1, tile2)
|
||||
maxArea = math.max(maxArea, area)
|
||||
end
|
||||
end
|
||||
return maxArea
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
7
src/day09/puzzle2.lua
Normal file
7
src/day09/puzzle2.lua
Normal file
@@ -0,0 +1,7 @@
|
||||
local puzzle2 = {}
|
||||
|
||||
function puzzle2.solve(input)
|
||||
return 0
|
||||
end
|
||||
|
||||
return puzzle2
|
||||
88
src/day10/puzzle1.lua
Normal file
88
src/day10/puzzle1.lua
Normal file
@@ -0,0 +1,88 @@
|
||||
local strings = require "cc.strings"
|
||||
local utils = require "utils"
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.parseMachine(line)
|
||||
local lightsStr = line:match("%[([.#]+)%]")
|
||||
local buttonsStr = strings.split(line:match(" (.+) "), " ")
|
||||
|
||||
local lightsBools = {}
|
||||
local lightsVal = 0
|
||||
for i=1, #lightsStr do
|
||||
local c = lightsStr:sub(i, i)
|
||||
lightsBools[i] = c == "#"
|
||||
if c == "#" then
|
||||
local pow = bit.blshift(1, i - 1)
|
||||
lightsVal = lightsVal + pow
|
||||
end
|
||||
end
|
||||
|
||||
local buttons = {}
|
||||
|
||||
for _, btnStr in ipairs(buttonsStr) do
|
||||
local lights = {}
|
||||
local value = 0
|
||||
for n in btnStr:gmatch("%d+") do
|
||||
local v = tonumber(n)
|
||||
table.insert(lights, v)
|
||||
value = value + bit.blshift(1, v)
|
||||
end
|
||||
table.insert(buttons, {
|
||||
lights=lights,
|
||||
value=value,
|
||||
presses=0
|
||||
})
|
||||
end
|
||||
|
||||
return {
|
||||
lightsBools=lightsBools,
|
||||
lightsVal=lightsVal,
|
||||
buttons=buttons
|
||||
}
|
||||
end
|
||||
|
||||
function puzzle1.startMachine(target, btns)
|
||||
local queue = {{value=0, presses=0, used=0}}
|
||||
local cache = {}
|
||||
while #queue ~= 0 do
|
||||
local test = table.remove(queue, 1)
|
||||
local key = tostring(test.value) .. "-" .. tostring(test.used)
|
||||
if not cache[key] then
|
||||
cache[key] = true
|
||||
if test.value == target then
|
||||
return test.presses
|
||||
end
|
||||
for i=(test.presses + 1), #btns do
|
||||
local pow = bit.blshift(1, i - 1)
|
||||
if bit.band(test.used, pow) == 0 then
|
||||
table.insert(queue, {
|
||||
value=bit.bxor(test.value, btns[i].value),
|
||||
presses=test.presses + 1,
|
||||
used=test.used + pow
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
print("Could not find valid button combination")
|
||||
return -1
|
||||
end
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local lines = utils.splitLines(input)
|
||||
local machines = {}
|
||||
for _, line in ipairs(lines) do
|
||||
table.insert(machines, puzzle1.parseMachine(line))
|
||||
end
|
||||
|
||||
local totalPresses = 0
|
||||
|
||||
for i, machine in ipairs(machines) do
|
||||
print("Machine " .. tostring(i) .. "/" .. tostring(#machines))
|
||||
totalPresses = totalPresses + puzzle1.startMachine(machine.lightsVal, machine.buttons)
|
||||
end
|
||||
|
||||
return totalPresses
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
7
src/day10/puzzle2.lua
Normal file
7
src/day10/puzzle2.lua
Normal file
@@ -0,0 +1,7 @@
|
||||
local puzzle2 = {}
|
||||
|
||||
function puzzle2.solve(input)
|
||||
return 0
|
||||
end
|
||||
|
||||
return puzzle2
|
||||
36
src/day11/puzzle1.lua
Normal file
36
src/day11/puzzle1.lua
Normal file
@@ -0,0 +1,36 @@
|
||||
local strings = require "cc.strings"
|
||||
local utils = require "utils"
|
||||
local puzzle1 = {}
|
||||
|
||||
local cache = {}
|
||||
|
||||
function puzzle1.countPaths(edges, startDev, endDev)
|
||||
local key = startDev .. "-" .. endDev
|
||||
if cache[key] then
|
||||
return cache[key]
|
||||
end
|
||||
if startDev == endDev then
|
||||
return 1
|
||||
end
|
||||
local outs = edges[startDev]
|
||||
local total = 0
|
||||
for _, out in ipairs(outs) do
|
||||
total = total + puzzle1.countPaths(edges, out, endDev)
|
||||
end
|
||||
cache[key] = total
|
||||
return total
|
||||
end
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local lines = utils.splitLines(input)
|
||||
local outputs = {}
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
local device, outs = line:match("(.+): (.+)")
|
||||
outputs[device] = strings.split(outs, " ")
|
||||
end
|
||||
|
||||
return puzzle1.countPaths(outputs, "you", "out")
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
49
src/day11/puzzle2.lua
Normal file
49
src/day11/puzzle2.lua
Normal file
@@ -0,0 +1,49 @@
|
||||
local strings = require "cc.strings"
|
||||
local utils = require "utils"
|
||||
local puzzle2 = {}
|
||||
|
||||
local cache = {}
|
||||
|
||||
function puzzle2.countPaths(edges, startDev, endDev, dac, fft)
|
||||
dac = dac or false
|
||||
fft = fft or false
|
||||
local key = startDev .. "-" .. endDev .. "-" .. tostring(dac) .. "-" .. tostring(fft)
|
||||
if cache[key] then
|
||||
return cache[key]
|
||||
end
|
||||
if startDev == endDev then
|
||||
if dac and fft then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
local outs = edges[startDev]
|
||||
local total = 0
|
||||
for _, out in ipairs(outs) do
|
||||
local dac2 = dac
|
||||
local fft2 = fft
|
||||
if out == "dac" then
|
||||
dac2 = true
|
||||
elseif out == "fft" then
|
||||
fft2 = true
|
||||
end
|
||||
total = total + puzzle2.countPaths(edges, out, endDev, dac2, fft2)
|
||||
end
|
||||
cache[key] = total
|
||||
return total
|
||||
end
|
||||
|
||||
function puzzle2.solve(input)
|
||||
local lines = utils.splitLines(input)
|
||||
local outputs = {}
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
local device, outs = line:match("(.+): (.+)")
|
||||
outputs[device] = strings.split(outs, " ")
|
||||
end
|
||||
|
||||
return puzzle2.countPaths(outputs, "svr", "out")
|
||||
end
|
||||
|
||||
return puzzle2
|
||||
Reference in New Issue
Block a user