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 /10/2.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 '10/2.lua')
| -rw-r--r-- | 10/2.lua | 46 |
1 files changed, 46 insertions, 0 deletions
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) |
