Skip to content

Commit

Permalink
Day 17, Part 2: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 19, 2024
1 parent 8be98e6 commit a13fa32
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
66 changes: 35 additions & 31 deletions src/day17/day17.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,7 @@ local bit = require("bit")
local bxor = bit.bxor
local pow = math.pow

--- @param input string
M.solve = function(input)
local prog = {}
local prog_count = 0
local reg_A, reg_B, reg_C

local parse = function()
local i = 1
for line in input:gmatch("[^\r\n]+") do
if i == 1 then
local num = line:match("Register A: (%d+)")
reg_A = tonumber(num)
elseif i == 2 then
local num = line:match("Register B: (%d+)")
reg_B = tonumber(num)
elseif i == 3 then
local num = line:match("Register C: (%d+)")
reg_C = tonumber(num)
else
for num in line:gmatch("(%d+)") do
prog[prog_count] = tonumber(num)
prog_count = prog_count + 1
end
end
i = i + 1
end
end
parse()

local function run_prog(prog, prog_count, reg_A, reg_B, reg_C)
local function combo_operand(operand)
-- Combo operands 0 through 3 represent literal values 0 through 3.
if operand >= 0 and operand <= 3 then
Expand Down Expand Up @@ -116,9 +88,41 @@ M.solve = function(input)
ip = ip + 2
::continue::
end
local part1 = table.concat(output, ",")
return output
end

--- @param input string
M.solve = function(input)
local parse = function()
local prog = {}
local prog_count = 0
local reg_A, reg_B, reg_C
local i = 1
for line in input:gmatch("[^\r\n]+") do
if i == 1 then
local num = line:match("Register A: (%d+)")
reg_A = tonumber(num)
elseif i == 2 then
local num = line:match("Register B: (%d+)")
reg_B = tonumber(num)
elseif i == 3 then
local num = line:match("Register C: (%d+)")
reg_C = tonumber(num)
else
for num in line:gmatch("(%d+)") do
prog[prog_count] = tonumber(num)
prog_count = prog_count + 1
end
end
i = i + 1
end
return prog, prog_count, reg_A, reg_B, reg_C
end
local prog, prog_count, original_reg_A, reg_B, reg_C = parse()
local part1 = table.concat(run_prog(prog, prog_count, original_reg_A, reg_B, reg_C), ",")

return part1, 0
local part2 = 0
return part1, part2
end

return M
14 changes: 12 additions & 2 deletions src/day17/day17_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ Register C: 0
Program: 0,1,5,4,3,0
]]
local part1, part2 = day17.solve(input)
local part1, _ = day17.solve(input)
it("part1", function()
assert.are.equal("4,6,3,5,6,3,5,2,1,0", part1)
end)
end)

describe("example 2", function()
local input = [[Register A: 2024
Register B: 0
Register C: 0
Program: 0,3,5,4,3,0
]]
local _, part2 = day17.solve(input)
it("part2", function()
assert.are.equal(0, part2)
assert.are.equal(117440, part2)
end)
end)
end)

0 comments on commit a13fa32

Please sign in to comment.