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 /12/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 '12/1.lua')
| -rw-r--r-- | 12/1.lua | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/12/1.lua b/12/1.lua new file mode 100644 index 0000000..4296caf --- /dev/null +++ b/12/1.lua @@ -0,0 +1,39 @@ +#!/usr/bin/env lua +local heights = {} +for i = string.byte('a'),string.byte('z') do + heights[string.char(i)] = i - string.byte('a') +end +heights.S, heights.E = heights.a, heights.z + +local map = {} +local start, endpos +for line in io.lines() do + map[#map+1] = {} + for char in line:gmatch("(.)") do + table.insert(map[#map],{height = heights[char], wts = math.huge}) + if char == "S" then + start = {#map,#map[#map]} + map[#map][#map[#map]].wts = 0 + elseif char == "E" then + endpos = {#map,#map[#map]} + end + end +end + +local adjacents = {{-1,0},{1,0},{0,-1},{0,1}} -- up, down, left, right +local seen,unseen = {},{start} +while #unseen > 0 do + local vertex = table.remove(unseen,1) + local data = map[vertex[1]][vertex[2]] + for _, offset in pairs(adjacents) do + local row,col = vertex[1] + offset[1], vertex[2] + offset[2] + if map[row] and map[row][col] and (map[row][col].height <= data.height + 1) then + local adata = map[row][col] + if adata.wts > data.wts + 1 then + adata.wts = data.wts + 1 + table.insert(unseen,{row,col}) + end + end + end +end +print(map[endpos[1]][endpos[2]].wts) |
