summaryrefslogtreecommitdiff
path: root/10/1.lua
diff options
context:
space:
mode:
authorAlex Pickering <alex@cogarr.net>2025-02-07 12:49:48 -0600
committerAlex Pickering <alex@cogarr.net>2025-02-07 12:49:48 -0600
commit3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 (patch)
tree278876284d07118ecdea5c48cb6453f3122887f0 /10/1.lua
downloadadvent_of_code_2022-master.tar.gz
advent_of_code_2022-master.tar.bz2
advent_of_code_2022-master.zip
inital commitHEADmaster
Diffstat (limited to '10/1.lua')
-rw-r--r--10/1.lua41
1 files changed, 41 insertions, 0 deletions
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)