aboutsummaryrefslogtreecommitdiff
path: root/day10/part1.rb
diff options
context:
space:
mode:
Diffstat (limited to 'day10/part1.rb')
-rw-r--r--day10/part1.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/day10/part1.rb b/day10/part1.rb
new file mode 100644
index 0000000..dfad57e
--- /dev/null
+++ b/day10/part1.rb
@@ -0,0 +1,51 @@
+class CPU
+ def initialize
+ @x = 1
+ @cycle = 1
+ @iq = []
+ end
+
+ attr_reader :x, :cycle
+
+ def enq(op, arg = 0)
+ @iq << [
+ arg,
+ case op
+ when 'noop'; 1
+ when 'addx'; 2
+ end
+ ]
+ end
+
+ def done?
+ @iq.empty?
+ end
+
+ def clock
+ istr = @iq[0]
+ istr[1] -= 1
+ if istr[1] == 0
+ @x += istr[0]
+ @iq.shift
+ end
+ @cycle += 1
+ end
+end
+
+cpu = CPU.new
+
+istrs = $stdin.readlines.map(&:strip).map(&:split)
+istrs.each do |istr|
+ cpu.enq(istr[0], istr[1]&.to_i || 0)
+end
+
+strsum = 0
+until cpu.done?
+ c = cpu.cycle
+ x = cpu.x
+
+ strsum += (c * x) if (c - 20) % 40 == 0
+ cpu.clock
+end
+
+puts strsum