Compare commits
4 Commits
0add2ecd03
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
c516532183
|
|||
|
3dedc1da03
|
|||
|
472634054f
|
|||
|
39c03d9920
|
@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
|
||||
## Progress
|
||||
|
||||
<!-- calendar-start -->
|
||||
#### Stars: 12/24
|
||||
#### Stars: 16/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>:star::star:|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>|10<br>|11<br>|12<br>|||
|
||||
<!-- calendar-end -->
|
||||
|
||||
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
|
||||
@@ -24,12 +24,12 @@
|
||||
"puzzle2": true
|
||||
},
|
||||
"day07": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day08": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day09": {
|
||||
"puzzle1": false,
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user