From 3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Fri, 7 Feb 2025 12:49:48 -0600 Subject: inital commit --- 25/1.lua | 74 ++++++++++++++++++++++++++++++++++++ 25/ext.lua | 62 ++++++++++++++++++++++++++++++ 25/input.txt | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 25/sample.txt | 13 +++++++ 4 files changed, 268 insertions(+) create mode 100644 25/1.lua create mode 100755 25/ext.lua create mode 100644 25/input.txt create mode 100644 25/sample.txt (limited to '25') diff --git a/25/1.lua b/25/1.lua new file mode 100644 index 0000000..53d8794 --- /dev/null +++ b/25/1.lua @@ -0,0 +1,74 @@ +require "ext" + +local snafu = { + ["0"] = 0, + ["1"] = 1, + ["2"] = 2, + ["-"] = -1, + ["="] = -2, +} +local snafu_rev = {} +for k,v in pairs(snafu) do + snafu_rev[v] = k +end + +local function decode(s) + local t = {} + for c in s:gmatch("(.)") do + table.insert(t,c) + end + local cursor = 1 + local n = 0 + while #t > 0 do + local val = table.remove(t) + n = n + (snafu[val] * cursor) + cursor = cursor * 5 + end + return n +end + +-- 4 8 9 0 +local function encode(n,location) + --print("Encoding",n) + if snafu_rev[n] then + --print("Snafu of",n,"is",snafu_rev[n]) + return snafu_rev[n] + end + local rem = n % 5 + if rem == 0 or rem == 1 or rem == 2 then + return encode(math.floor(n/5)) .. snafu_rev[rem] + elseif rem == 3 or rem == 4 then + return encode(math.floor(n/5)+1) .. snafu_rev[rem - 5] + end + +end + +local sum = 0 +for line in io.lines() do + sum = sum + decode(line) +end +print(encode(sum)) +--[[ +print(encode(6)) +local encodings = { + [1]="1", + [2]="2", + [3]="1=", + [4]="1-", + [5]="10", + [6]="11", + [7]="12", + [8]="2=", + [9]="2-", + [10]="20", + [15]="1=0", + [20]="1-0", + [2022]="1=11-2", + [12345]="1-0---0", + [314159265]="1121-1110-1=0", +} +for k,v in pairs(encodings) do + local fe = encode(k) + print("Finished encoding of",k,"is",fe) + assert(fe == v) +end]] diff --git a/25/ext.lua b/25/ext.lua new file mode 100755 index 0000000..c1eb1cc --- /dev/null +++ b/25/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/25/input.txt b/25/input.txt new file mode 100644 index 0000000..d5b8e5d --- /dev/null +++ b/25/input.txt @@ -0,0 +1,119 @@ +2=--111020=02-=222 +20111==-2001-10 +1211=0020- +1==2==0-20=1- +10-=00220--0==1===2 +20 +2==--1---=221 +102=1 +2=1-==-1-211 +2-2100=1000-01-00= +12=0-- +12=-0=0=11= +20=1111=22-21-1-=2 +12-00-==1 +21=12222==2-11- +1-02 +11101=12- +2002=0--= +111-20100100=122=2 +10= +1-===0-12-021==0 +22202212--2-12 +20=1-1 +1=1 +10=-11=0-=12 +111--21=000 +1022-0=1=02=- +1==2==000=-202--2020 +1==10-==10200001=- +101=10--01=102 +1-220-2=-000222- +1=221=2--=1-20 +2--=--0=-=01210- +1021=21=0=110121=- +1-11=212=-2-11- +10=1=0120-0-012-1 +1-0-=11 +11=2== +10-1-- +1-110--=1-=2-- +21212===010=0-1 +2-2= +1-10100 +2--0-2---1=002--2= +12-212=-2=2021 +22211=-=-2 +1-2112 +1=101-111=-1121 +21 +1=10-0=2002-22-2 +1=-2==12=01222- +112221---=12==0011 +1--1==01 +102-0- +2-2=0== +20= +1=-=-0---00 +1-=00==0--0 +1-----2- +1=0 +2--2-==0--=2=2=1=22 +20=== +2=-21=012=2 +20-=11= +1=--102=- +10-=--=1 +1--1-210 +1011-11-= +2=-0112-=2-01002-- +111=1-==10=0101--= +1=-20002--=-22-210 +1---=0-02-100 +2122001012001=02- +1==--0-0000- +1= +22== +1010=10--100 +2111 +1=2=- +1-2=2121-1----=-2 +1==11=-201=--=1 +1=2= +112=1--1-=1 +1-112=2010--1 +2121-0--1010==- +1=0=0 +112=-00=1-=-=-01 +112=10--20-201=2 +21-210-1 +1==02==120=0= +1==-0 +2=10-2=2 +11=0=--2-0--- +122=21-=22=2 +111 +1002-102=1-01 +1-1-20-12-- +1=1-=020- +100=201-1000 +2=- +1==111-2001 +1==222=122-=0 +1-00=211-2 +12=-0 +1-0=22-02===11-1- +1--201-120-01 +10 +20=- +2=2==00--2=121- +11022120=-=- +100 +11--2--=12000=1 +201=2==210- +1200221-=2=-22220 +10220=2-021-1= +1-=0202=0122-2 +2012-121-02--011 +22=122 +1==-==-120-2-22 diff --git a/25/sample.txt b/25/sample.txt new file mode 100644 index 0000000..027aeec --- /dev/null +++ b/25/sample.txt @@ -0,0 +1,13 @@ +1=-0-2 +12111 +2=0= +21 +2=01 +111 +20012 +112 +1=-1= +1-12 +12 +1= +122 -- cgit v1.2.3-70-g09d2