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 | |
| 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')
| -rw-r--r-- | 12/1.lua | 39 | ||||
| -rw-r--r-- | 12/2.lua | 42 | ||||
| -rwxr-xr-x | 12/ext.lua | 62 | ||||
| -rw-r--r-- | 12/input.txt | 41 | ||||
| -rw-r--r-- | 12/sample.txt | 5 |
5 files changed, 189 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) diff --git a/12/2.lua b/12/2.lua new file mode 100644 index 0000000..e4f2c1f --- /dev/null +++ b/12/2.lua @@ -0,0 +1,42 @@ +#!/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 seen, unseen = {},{} +local 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]} + elseif char == "a" then + table.insert(unseen, {#map,#map[#map]}) + map[#map][#map[#map]].wts = 0 + end + end +end +local adjacents = {{-1,0},{1,0},{0,-1},{0,1}} +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) diff --git a/12/ext.lua b/12/ext.lua new file mode 100755 index 0000000..c1eb1cc --- /dev/null +++ b/12/ext.lua @@ -0,0 +1,62 @@ +-- Override tostring to display more info about the table
+local old_tostring = tostring
+local numtabs = 0
+local printed_tables = {}
+
+local function tostring_helper(el)
+ assert(type(el) == "table", "Tried to call helper with something that was not a table, it was a " .. type(el))
+ local mt = getmetatable(el)
+ if mt and mt.__tostring then
+ return mt.__tostring(el)
+ elseif printed_tables[el] == true then
+ return old_tostring(el)
+ else
+ printed_tables[el] = true
+ numtabs = numtabs + 1
+ local strbuilder = setmetatable({"{"},{__index = table})
+ for k,v in pairs(el) do
+ local key,value
+ if type(k) == "table" then
+ key = tostring_helper(k)
+ else
+ key = old_tostring(k)
+ end
+ if type(v) == "table" then
+ value = tostring_helper(v)
+ else
+ value = old_tostring(v)
+ end
+ strbuilder:insert(string.format("%s%s : %s", string.rep("\t",numtabs), key, value))
+ end
+ strbuilder:insert(string.rep("\t",numtabs - 1) .. "}")
+ numtabs = numtabs - 1
+ return strbuilder:concat("\n")
+ end
+
+end
+function tostring(el)
+ printed_tables = {}
+ if type(el) == "table" then
+ return tostring_helper(el)
+ end
+ return old_tostring(el)
+end
+
+-- Functions to save my hands
+function printf(fmt, ...)
+ print(string.format(fmt,...))
+end
+function errorf(fmt, ...)
+ --Our error isn't actually in this function, it's 1 above us (1) = 2
+ error(string.format(fmt,...),2)
+end
+function assertf(bool, fmt, ...)
+ assert(type(fmt) == "string", "Assertf arg #2 was \"" .. type(fmt) .. "\", expected string")
+ if not bool then
+ args = {fmt}
+ for k,v in ipairs({...}) do
+ table.insert(args,tostring(v))
+ end
+ error(string.format(unpack(args)),2)
+ end
+end
diff --git a/12/input.txt b/12/input.txt new file mode 100644 index 0000000..bac8753 --- /dev/null +++ b/12/input.txt @@ -0,0 +1,41 @@ +abccccccccccccccccccaaaaaaaaacccccccccccccccccccccccccccccccccccccaaaa +abcccccccccccccccaaaaaaaaaaacccccccccccccccccccccccccccccccccccccaaaaa +abcaaccaacccccccccaaaaaaaaaacccccccccccccccccccccaaacccccccccccccaaaaa +abcaaaaaaccccccccaaaaaaaaaaaaacccccccccccccccccccaacccccccccccccaaaaaa +abcaaaaaacccaaacccccaaaaaaaaaaaccccccccccccccccccaaaccccccccccccccccaa +abaaaaaaacccaaaaccccaaaaaacaaaacccccccccccaaaacjjjacccccccccccccccccca +abaaaaaaaaccaaaaccccaaaaaaccccccaccccccccccaajjjjjkkcccccccccccccccccc +abaaaaaaaaccaaacccccccaaaccccccaaccccccccccajjjjjjkkkaaacccaaaccaccccc +abccaaacccccccccccccccaaccccaaaaaaaacccccccjjjjoookkkkaacccaaaaaaccccc +abcccaacccccccccccccccccccccaaaaaaaaccccccjjjjoooookkkkcccccaaaaaccccc +abcccccccaacccccccccccccccccccaaaacccccccijjjoooooookkkkccaaaaaaaccccc +abccaaccaaaccccccccccccccccccaaaaacccccciijjooouuuoppkkkkkaaaaaaaacccc +abccaaaaaaaccccccccccaaaaacccaacaaaccciiiiiooouuuuupppkkklllaaaaaacccc +abccaaaaaacccccccccccaaaaacccacccaaciiiiiiqooouuuuuupppkllllllacaccccc +abcccaaaaaaaacccccccaaaaaaccccaacaiiiiiqqqqoouuuxuuupppppplllllccccccc +abccaaaaaaaaaccaaaccaaaaaaccccaaaaiiiiqqqqqqttuxxxuuuppppppplllccccccc +abccaaaaaaaacccaaaaaaaaaaacccaaaahiiiqqqttttttuxxxxuuuvvpppplllccccccc +abcaaaaaaacccaaaaaaaaaaacccccaaaahhhqqqqtttttttxxxxuuvvvvvqqlllccccccc +abcccccaaaccaaaaaaaaaccccccccacaahhhqqqttttxxxxxxxyyyyyvvvqqlllccccccc +abcccccaaaccaaaaaaaacccccccccccaahhhqqqtttxxxxxxxyyyyyyvvqqqlllccccccc +SbcccccccccccaaaaaaaaaccccccccccchhhqqqtttxxxxEzzzyyyyvvvqqqmmlccccccc +abcccccccccccaaaaaaaacccaacccccccchhhppptttxxxxyyyyyvvvvqqqmmmcccccccc +abccccccccccaaaaaaaaaaccaacccccccchhhpppptttsxxyyyyyvvvqqqmmmccccccccc +abcaacccccccaaaaaaacaaaaaaccccccccchhhppppsswwyyyyyyyvvqqmmmmccccccccc +abaaaacccccccaccaaaccaaaaaaacccccccchhhpppsswwyywwyyyvvqqmmmddcccccccc +abaaaaccccccccccaaaccaaaaaaacccccccchhhpppsswwwwwwwwwvvqqqmmdddccccccc +abaaaacccccccccaaaccaaaaaaccccccccccgggpppsswwwwrrwwwwvrqqmmdddccccccc +abccccccaaaaaccaaaacaaaaaaccccccaacccggpppssswwsrrrwwwvrrqmmdddacccccc +abccccccaaaaaccaaaacccccaaccccaaaaaacggpppssssssrrrrrrrrrnmmdddaaccccc +abcccccaaaaaaccaaaccccccccccccaaaaaacggppossssssoorrrrrrrnnmdddacccccc +abcccccaaaaaaccccccccaaaaccccccaaaaacgggoooossoooonnnrrnnnnmddaaaacccc +abccccccaaaaaccccccccaaaacccccaaaaaccgggoooooooooonnnnnnnnndddaaaacccc +abccccccaaaccccccccccaaaacccccaaaaacccgggoooooooffennnnnnnedddaaaacccc +abcccccccccccccccccccaaacccccccaacccccggggffffffffeeeeeeeeeedaaacccccc +abccccccccccccccccccaaacccccaccaaccccccggfffffffffeeeeeeeeeecaaacccccc +abccccccccccccccccccaaaacccaaaaaaaaaccccfffffffaaaaaeeeeeecccccccccccc +abccccccccaacaaccccaaaaaacaaaaaaaaaaccccccccccaaaccaaaaccccccccccccccc +abccccccccaaaaacccaaaaaaaaaaacaaaaccccccccccccaaaccccaaccccccccccaaaca +abcccccccaaaaaccccaaaaaaaaaaacaaaaacccccccccccaaaccccccccccccccccaaaaa +abcccccccaaaaaacccaaaaaaaaaacaaaaaacccccccccccaaccccccccccccccccccaaaa +abcccccccccaaaaccaaaaaaaaaaaaaaccaaccccccccccccccccccccccccccccccaaaaa diff --git a/12/sample.txt b/12/sample.txt new file mode 100644 index 0000000..86e9cac --- /dev/null +++ b/12/sample.txt @@ -0,0 +1,5 @@ +Sabqponm +abcryxxl +accszExk +acctuvwj +abdefghi |
