advent-of-code

advent of code attempts
git clone git://bvnf.space/advent-of-code.git
Log | Files | Refs

commit 322415223efdef2e6ea97254c2b5cf1539c45677
parent 90cc428eeedc019c1fa9e758e4fb13c0db4a5fb8
Author: aabacchus <ben@bvnf.space>
Date:   Thu, 15 Dec 2022 11:03:56 +0000

22.15.1 in Lua

Diffstat:
A2022/15/a.lua | 49+++++++++++++++++++++++++++++++++++++++++++++++++
A2022/15/example | 14++++++++++++++
A2022/15/input | 32++++++++++++++++++++++++++++++++
3 files changed, 95 insertions(+), 0 deletions(-)

diff --git a/2022/15/a.lua b/2022/15/a.lua @@ -0,0 +1,49 @@ +local f = assert(io.open(arg[1], "r")) + +if (arg[1] == "example") then + ROW = 10 +elseif (arg[1] == "input") then + ROW = 2000000 +end + +local nodes = {} +local minx, miny, maxx, maxy +for line in f:lines() do + local prev = nil + for x,y in line:gmatch("x=(-?%d+), y=(-?%d+)") do + x = tonumber(x) + y = tonumber(y) + minx = math.min(x, minx and minx or x) + maxx = math.max(x, maxx and maxx or x) + miny = math.min(y, miny and miny or y) + maxy = math.max(y, maxy and maxy or y) + + if (prev) then + table.insert(nodes, {x=prev.x, y=prev.y, bx=x, by=y}) + end + prev = {x=x,y=y}; + end +end + +print(minx,maxx) +print(miny,maxy) + +local cntr = {} +for _,v in ipairs(nodes) do + local d = math.abs(v.bx - v.x) + math.abs(v.by - v.y) + dy = math.abs(v.y - ROW) + if (dy <= d) then + for i=v.x-(d-dy),v.x+(d-dy) do + if not (v.bx == i and v.by == ROW) then -- can't have two beacons in the same place + cntr[i] = 1 + end + end + end +end + +local c = 0 +for _ in pairs(cntr) do + c = c + 1 +end + +print(c) diff --git a/2022/15/example b/2022/15/example @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 diff --git a/2022/15/input b/2022/15/input @@ -0,0 +1,32 @@ +Sensor at x=2832148, y=322979: closest beacon is at x=3015667, y=-141020 +Sensor at x=1449180, y=3883502: closest beacon is at x=2656952, y=4188971 +Sensor at x=2808169, y=1194666: closest beacon is at x=3015667, y=-141020 +Sensor at x=1863363, y=2435968: closest beacon is at x=2166084, y=2883057 +Sensor at x=3558230, y=2190936: closest beacon is at x=3244164, y=2592191 +Sensor at x=711491, y=2444705: closest beacon is at x=617239, y=2988377 +Sensor at x=2727148, y=2766272: closest beacon is at x=2166084, y=2883057 +Sensor at x=2857938, y=3988086: closest beacon is at x=2968511, y=4098658 +Sensor at x=1242410, y=2270153: closest beacon is at x=214592, y=2000000 +Sensor at x=3171784, y=2523127: closest beacon is at x=3244164, y=2592191 +Sensor at x=2293378, y=71434: closest beacon is at x=3015667, y=-141020 +Sensor at x=399711, y=73420: closest beacon is at x=1152251, y=-158441 +Sensor at x=3677529, y=415283: closest beacon is at x=3015667, y=-141020 +Sensor at x=207809, y=2348497: closest beacon is at x=214592, y=2000000 +Sensor at x=60607, y=3403420: closest beacon is at x=617239, y=2988377 +Sensor at x=3767729, y=3136725: closest beacon is at x=4171278, y=3348370 +Sensor at x=3899632, y=3998969: closest beacon is at x=4171278, y=3348370 +Sensor at x=394783, y=1541278: closest beacon is at x=214592, y=2000000 +Sensor at x=1193642, y=642631: closest beacon is at x=1152251, y=-158441 +Sensor at x=122867, y=2661904: closest beacon is at x=214592, y=2000000 +Sensor at x=551012, y=3787568: closest beacon is at x=617239, y=2988377 +Sensor at x=3175715, y=2975144: closest beacon is at x=3244164, y=2592191 +Sensor at x=402217, y=2812449: closest beacon is at x=617239, y=2988377 +Sensor at x=879648, y=1177329: closest beacon is at x=214592, y=2000000 +Sensor at x=1317218, y=2978309: closest beacon is at x=617239, y=2988377 +Sensor at x=3965126, y=1743931: closest beacon is at x=3244164, y=2592191 +Sensor at x=2304348, y=3140055: closest beacon is at x=2166084, y=2883057 +Sensor at x=3380135, y=3650709: closest beacon is at x=2968511, y=4098658 +Sensor at x=49224, y=1914296: closest beacon is at x=214592, y=2000000 +Sensor at x=3096228, y=2457233: closest beacon is at x=3244164, y=2592191 +Sensor at x=1415660, y=6715: closest beacon is at x=1152251, y=-158441 +Sensor at x=2616280, y=3548378: closest beacon is at x=2656952, y=4188971