From 3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Fri, 7 Feb 2025 12:49:48 -0600 Subject: inital commit --- 10/1.lua | 41 +++++++++++++++++ 10/2.lua | 46 ++++++++++++++++++ 10/ext.lua | 62 +++++++++++++++++++++++++ 10/input.txt | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 10/sample.txt | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 432 insertions(+) create mode 100644 10/1.lua create mode 100644 10/2.lua create mode 100755 10/ext.lua create mode 100644 10/input.txt create mode 100644 10/sample.txt (limited to '10') diff --git a/10/1.lua b/10/1.lua new file mode 100644 index 0000000..b20d193 --- /dev/null +++ b/10/1.lua @@ -0,0 +1,41 @@ +require "ext" + +local cpu = { + r = {x = 1}, + isa = { + addx = { + nargs = 1, + cycles = 2, + f = function(self,v) + self.r.x = self.r.x + tonumber(v) + end + }, + noop = { + nargs = 0, + cycles = 1, + f = function(self) end + } + } +} + +local strength_sum = 0 +local cycle = 1 +for line in io.lines() do + local parts = {} + for part in line:gmatch("(%S+)") do + table.insert(parts,part) + end + local insn = table.remove(parts,1) + local ins = cpu.isa[insn] + assertf(ins, "No instruction %s in %s",insn,line) + for i = cycle,cycle + ins.cycles - 1 do + if (i - 20) % 40 == 0 then + local strength = i * cpu.r.x + strength_sum = strength_sum + strength + break + end + end + cycle = cycle + ins.cycles + ins.f(cpu,table.unpack(parts)) +end +print(strength_sum) diff --git a/10/2.lua b/10/2.lua new file mode 100644 index 0000000..377870d --- /dev/null +++ b/10/2.lua @@ -0,0 +1,46 @@ +require "ext" + +local w, h = 40, 6 +local cpu = { + r = {x = 1}, + isa = { + addx = { + nargs = 1, + cycles = 2, + f = function(self,v) + self.r.x = self.r.x + tonumber(v) + end + }, + noop = { + nargs = 0, + cycles = 1, + f = function(self) end + } + } +} + +local strength_sum = 0 +local cycle = 1 +for line in io.lines() do + local parts = {} + for part in line:gmatch("(%S+)") do + table.insert(parts,part) + end + local insn = table.remove(parts,1) + local ins = cpu.isa[insn] + assertf(ins, "No instruction %s in %s",insn,line) + for i = cycle,cycle + ins.cycles - 1 do + local crtpos = (i-1) % w + if crtpos + 1 == cpu.r.x or crtpos == cpu.r.x or crtpos - 1 == cpu.r.x then + io.write("#") + else + io.write(" ") + end + if i % 40 == 0 then + io.write("\n") + end + end + cycle = cycle + ins.cycles + ins.f(cpu,table.unpack(parts)) +end +print(strength_sum) diff --git a/10/ext.lua b/10/ext.lua new file mode 100755 index 0000000..c1eb1cc --- /dev/null +++ b/10/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/10/input.txt b/10/input.txt new file mode 100644 index 0000000..0abc374 --- /dev/null +++ b/10/input.txt @@ -0,0 +1,137 @@ +noop +addx 22 +addx -17 +addx 1 +addx 4 +addx 17 +addx -16 +addx 4 +addx 1 +addx 21 +addx -17 +addx -10 +noop +addx 17 +addx -1 +addx 5 +addx -1 +noop +addx 4 +addx 1 +noop +addx -37 +addx 5 +addx 27 +addx -22 +addx -2 +addx 2 +addx 5 +addx 2 +addx 5 +noop +noop +addx -2 +addx 5 +addx 16 +addx -11 +addx -2 +addx 2 +addx 5 +addx 2 +addx -8 +addx 9 +addx -38 +addx 5 +addx 20 +addx -16 +addx 8 +addx -5 +addx 1 +addx 4 +noop +noop +addx 5 +addx -2 +noop +noop +addx 18 +noop +addx -8 +addx 2 +addx 7 +addx -2 +noop +noop +noop +noop +noop +addx -35 +noop +addx 32 +addx -26 +addx 12 +addx -8 +addx 3 +noop +addx 2 +addx 16 +addx -24 +addx 11 +addx 3 +addx -17 +addx 17 +addx 5 +addx 2 +addx -15 +addx 22 +addx 3 +noop +addx -40 +noop +addx 2 +noop +addx 3 +addx 13 +addx -6 +addx 10 +addx -9 +addx 2 +addx 22 +addx -15 +addx 8 +addx -7 +addx 2 +addx 5 +addx 2 +addx -32 +addx 33 +addx 2 +addx 5 +addx -39 +addx -1 +addx 3 +addx 4 +addx 1 +addx 4 +addx 21 +addx -20 +addx 2 +addx 12 +addx -4 +noop +noop +noop +noop +noop +addx 4 +noop +noop +noop +addx 6 +addx -27 +addx 31 +noop +noop +noop +noop +noop diff --git a/10/sample.txt b/10/sample.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/10/sample.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop -- cgit v1.2.3-70-g09d2