Compare commits
6 Commits
448dbf0ff9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
472634054f
|
|||
|
39c03d9920
|
|||
|
0add2ecd03
|
|||
|
d9ac0c7a9f
|
|||
|
bd36baa1ec
|
|||
|
80f797d4b9
|
@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
|
||||
## Progress
|
||||
|
||||
<!-- calendar-start -->
|
||||
#### Stars: 8/24
|
||||
#### Stars: 14/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>|6<br>|7<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>|9<br>|10<br>|11<br>|12<br>|||
|
||||
<!-- calendar-end -->
|
||||
|
||||
11
res/examples/day05.txt
Normal file
11
res/examples/day05.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
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.......
|
||||
...............
|
||||
.......^.......
|
||||
...............
|
||||
......^.^......
|
||||
...............
|
||||
.....^.^.^.....
|
||||
...............
|
||||
....^.^...^....
|
||||
...............
|
||||
...^.^...^.^...
|
||||
...............
|
||||
..^...^.....^..
|
||||
...............
|
||||
.^.^.^.^.^...^.
|
||||
...............
|
||||
@@ -16,16 +16,16 @@
|
||||
"puzzle2": true
|
||||
},
|
||||
"day05": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day06": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day07": {
|
||||
"puzzle1": false,
|
||||
"puzzle2": false
|
||||
"puzzle1": true,
|
||||
"puzzle2": true
|
||||
},
|
||||
"day08": {
|
||||
"puzzle1": false,
|
||||
|
||||
39
src/day05/puzzle1.lua
Normal file
39
src/day05/puzzle1.lua
Normal file
@@ -0,0 +1,39 @@
|
||||
local strings = require "cc.strings"
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.isFresh(ranges, i)
|
||||
for _, range in ipairs(ranges) do
|
||||
if range[1] <= i and i <= range[2] then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local lines = strings.split(input, "\n")
|
||||
local ranges = {}
|
||||
|
||||
local bIngr = false
|
||||
local totalFresh = 0
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
if line == "" then
|
||||
bIngr = true
|
||||
else
|
||||
if bIngr then
|
||||
local ingr = tonumber(line)
|
||||
if puzzle1.isFresh(ranges, ingr) then
|
||||
totalFresh = totalFresh + 1
|
||||
end
|
||||
else
|
||||
local min, max = line:match("(%d+)%-(%d+)")
|
||||
table.insert(ranges, {tonumber(min), tonumber(max)})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return totalFresh
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
47
src/day05/puzzle2.lua
Normal file
47
src/day05/puzzle2.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
local strings = require "cc.strings"
|
||||
local puzzle2 = {}
|
||||
|
||||
function puzzle2.solve(input)
|
||||
local lines = strings.split(input, "\n")
|
||||
local bounds = {}
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
if line == "" then
|
||||
break
|
||||
end
|
||||
local min, max = line:match("(%d+)%-(%d+)")
|
||||
min = tonumber(min)
|
||||
max = tonumber(max)
|
||||
table.insert(bounds, {i=min, type="start"})
|
||||
table.insert(bounds, {i=max, type="end"})
|
||||
end
|
||||
|
||||
table.sort(bounds, function (a, b)
|
||||
if a.i == b.i then
|
||||
return a.type == "start" and b.type == "end"
|
||||
end
|
||||
return a.i < b.i
|
||||
end)
|
||||
|
||||
local totalFresh = 0
|
||||
local min = 0
|
||||
local balance = 0
|
||||
|
||||
for _, bound in ipairs(bounds) do
|
||||
if bound.type == "start" then
|
||||
if balance == 0 then
|
||||
min = bound.i
|
||||
end
|
||||
balance = balance + 1
|
||||
else
|
||||
balance = balance - 1
|
||||
if balance == 0 then
|
||||
totalFresh = totalFresh + bound.i - min + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return totalFresh
|
||||
end
|
||||
|
||||
return puzzle2
|
||||
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
|
||||
Reference in New Issue
Block a user