Compare commits
2 Commits
922aec35c1
...
e1623ac0fa
| Author | SHA1 | Date | |
|---|---|---|---|
|
e1623ac0fa
|
|||
|
eb7dc5805f
|
@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
|
|||||||
## Progress
|
## Progress
|
||||||
|
|
||||||
<!-- calendar-start -->
|
<!-- calendar-start -->
|
||||||
#### Stars: 0/24
|
#### Stars: 2/24
|
||||||
|
|
||||||
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|
||||||
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|
||||||
|1<br>|2<br>|3<br>|4<br>|5<br>|6<br>|7<br>|
|
|1<br>:star::star:|2<br>|3<br>|4<br>|5<br>|6<br>|7<br>|
|
||||||
|8<br>|9<br>|10<br>|11<br>|12<br>|||
|
|8<br>|9<br>|10<br>|11<br>|12<br>|||
|
||||||
<!-- calendar-end -->
|
<!-- calendar-end -->
|
||||||
|
|||||||
10
res/examples/day01.txt
Normal file
10
res/examples/day01.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"day01": {
|
"day01": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day02": {
|
"day02": {
|
||||||
"puzzle1": false,
|
"puzzle1": false,
|
||||||
|
|||||||
25
src/day01/puzzle1.lua
Normal file
25
src/day01/puzzle1.lua
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local password = 0
|
||||||
|
local cursor = 50
|
||||||
|
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
local dir = line:sub(1, 1)
|
||||||
|
local dist = tonumber(line:sub(2))
|
||||||
|
if dir == "R" then
|
||||||
|
cursor = cursor + dist
|
||||||
|
else
|
||||||
|
cursor = cursor - dist
|
||||||
|
end
|
||||||
|
cursor = cursor % 100
|
||||||
|
if cursor == 0 then
|
||||||
|
password = password + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return password
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
31
src/day01/puzzle2.lua
Normal file
31
src/day01/puzzle2.lua
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local password = 0
|
||||||
|
local cursor = 50
|
||||||
|
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
local dir = line:sub(1, 1)
|
||||||
|
local dist = tonumber(line:sub(2))
|
||||||
|
if dir == "R" then
|
||||||
|
cursor = cursor + dist
|
||||||
|
if cursor > 99 then
|
||||||
|
password = password + math.floor(cursor / 100)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
cursor = cursor - dist
|
||||||
|
if cursor <= 0 then
|
||||||
|
if cursor ~= -dist then
|
||||||
|
password = password + 1
|
||||||
|
end
|
||||||
|
password = password + math.floor(-cursor / 100)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
cursor = cursor % 100
|
||||||
|
end
|
||||||
|
return password
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
Reference in New Issue
Block a user