diff options
| author | Alex Pickering <alex@cogarr.net> | 2025-02-07 12:49:48 -0600 |
|---|---|---|
| committer | Alex Pickering <alex@cogarr.net> | 2025-02-07 12:49:48 -0600 |
| commit | 3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 (patch) | |
| tree | 278876284d07118ecdea5c48cb6453f3122887f0 /14/1.lua | |
| download | advent_of_code_2022-3555be54c2abb8d5ece008a60dbdfbde0ffbddd7.tar.gz advent_of_code_2022-3555be54c2abb8d5ece008a60dbdfbde0ffbddd7.tar.bz2 advent_of_code_2022-3555be54c2abb8d5ece008a60dbdfbde0ffbddd7.zip | |
Diffstat (limited to '14/1.lua')
| -rw-r--r-- | 14/1.lua | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/14/1.lua b/14/1.lua new file mode 100644 index 0000000..f774175 --- /dev/null +++ b/14/1.lua @@ -0,0 +1,83 @@ +require "ext" +local cave = {} +local lowest = 0 +for line in io.lines() do + print("line:",line) + local cursor = nil + for pair in line:gmatch("([0-9,]+)") do + local col,row = pair:match("(%d+),(%d+)") + col,row = tonumber(col),tonumber(row) + if cursor then + for rown = math.min(cursor[1],row), math.max(cursor[1],row) do + for coln = math.min(cursor[2],col), math.max(cursor[2],col) do + cave[rown] = cave[rown] or {} + printf("Setting %d %d to true",rown,coln) + cave[rown][coln] = true + end + end + end + lowest = math.max(row,lowest) + cursor = {row,col} + end +end + +local sand = {} +function print_puzzle() + for row = 0, 500 do + for col = 0,100 do + local data = cave[row] and cave[row][col] + if data == "s" then + io.write("O") + elseif data then + io.write("#") + else + io.write(".") + end + end + io.write("\n") + end + for _,row in pairs(cave) do + for _,col in pairs(row) do + end + end +end +print_puzzle() +print("lowest:",lowest) +local floor = lowest + 2 + +local sandpoint = {0,500} +local movement = { + {1,0}, + {1,-1}, + {1,1} +} +local x = 0 +while true do + for k,v in pairs(sandpoint) do + sand[k] = v + end + local could_move = false + repeat + could_move = false + for _,move in ipairs(movement) do + local drow, dcol = sand[1] + move[1], sand[2] + move[2] + if not cave[drow] or cave[drow][dcol] == nil then + could_move = true + sand[1] = drow + sand[2] = dcol + break + end + end + until not could_move or sand[1] = floor + if not could_move then + cave[sand[1]] = cave[sand[1]] or {} + cave[sand[1]][sand[2]] = "s" + end + if sand[1] == 500 then + print("sand stopped falling") + break + end + x = x + 1 +end +print_puzzle() +print(x) |
