Skip to content

Commit

Permalink
Day 8, Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 8, 2024
1 parent f78e026 commit 26a0212
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
| 5 | Lua | 3.81ms |
| 6 | Lua | 5400ms |
| 7 | Lua | 230ms |
| 8 | Lua | 3.31ms |
74 changes: 48 additions & 26 deletions src/day08/day08.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,83 @@
local M = {}

local Point2D = require("point2d")
local pprint = require("pprint")

--- @param input string
M.solve = function(input)
local matrix = {}
--- @type table<number, Point2D[]>
local antennas = {}
local col_count
local col_count = input:find("\n") - 1
local row_count = 0
for line in input:gmatch("[^\r\n]+") do
row_count = row_count + 1
if not col_count then
col_count = string.len(line)
end
local row = {}
for y = 1, col_count do
local c = string.sub(line, y, y)
if c:match("[0-9A-Za-z]") then
if c ~= "." then
local t = antennas[c] or {}
table.insert(t, Point2D:new(row_count, y))
antennas[c] = t
end
table.insert(row, c)
end
table.insert(matrix, row)
end

local is_within_bounds = function(p)
return p.x >= 1 and p.x <= col_count and p.y >= 1 and p.y <= row_count
end

local antinodes = {}
for name, points in pairs(antennas) do
local n = #points
for i = 1, n do
local p = points[i]
for j = 1, n do
if j ~= i then
local q = points[j]
local delta = q - p
local anti = q + delta
if is_within_bounds(anti) then
antinodes[anti:hash()] = true
local part1 = function()
local antinodes = {}
for _, points in pairs(antennas) do
local n = #points
for i = 1, n do
local p = points[i]
for j = 1, n do
if j ~= i then
local q = points[j]
local delta = q - p
local anti = q + delta
if is_within_bounds(anti) then
antinodes[anti:hash()] = true
end
end
end
end
end

local count = 0
for _, _ in pairs(antinodes) do
count = count + 1
end
return count
end

local part1, part2 = 0, 0
for k, _ in pairs(antinodes) do
part1 = part1 + 1
local part2 = function()
local antinodes = {}
for _, points in pairs(antennas) do
local n = #points
for i = 1, n do
local p = points[i]
for j = 1, n do
if j ~= i then
local q = points[j]
local delta = q - p
local anti = q
repeat
antinodes[anti:hash()] = true
anti = anti + delta
until not is_within_bounds(anti)
end
end
end
end

local count = 0
for _, _ in pairs(antinodes) do
count = count + 1
end
return count
end

return part1, part2
return part1(), part2()
end

return M
2 changes: 1 addition & 1 deletion src/day08/day08_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ describe("day08", function()
]]
local part1, part2 = day08.solve(input)
assert.are.equal(14, part1)
assert.are.equal(0, part2)
assert.are.equal(34, part2)
end)
end)

0 comments on commit 26a0212

Please sign in to comment.