aboutsummaryrefslogtreecommitdiff
path: root/day10
diff options
context:
space:
mode:
Diffstat (limited to 'day10')
-rw-r--r--day10/input139
-rw-r--r--day10/part1.rb51
-rw-r--r--day10/part2.rb50
-rw-r--r--day10/test3
-rw-r--r--day10/test2146
5 files changed, 389 insertions, 0 deletions
diff --git a/day10/input b/day10/input
new file mode 100644
index 0000000..57f49a0
--- /dev/null
+++ b/day10/input
@@ -0,0 +1,139 @@
+noop
+noop
+noop
+addx 6
+addx -1
+noop
+addx 5
+noop
+noop
+addx -12
+addx 19
+addx -1
+noop
+addx 4
+addx -11
+addx 16
+noop
+noop
+addx 5
+addx 3
+addx -2
+addx 4
+noop
+noop
+noop
+addx -37
+noop
+addx 3
+addx 2
+addx 5
+addx 2
+addx 10
+addx -9
+noop
+addx 1
+addx 4
+addx 2
+noop
+addx 3
+addx 2
+addx 5
+addx 2
+addx 3
+addx -2
+addx 2
+addx 5
+addx -40
+addx 25
+addx -22
+addx 2
+addx 5
+addx 2
+addx 3
+addx -2
+noop
+addx 23
+addx -18
+addx 2
+noop
+noop
+addx 7
+noop
+noop
+addx 5
+noop
+noop
+noop
+addx 1
+addx 2
+addx 5
+addx -40
+addx 3
+addx 8
+addx -4
+addx 1
+addx 4
+noop
+noop
+noop
+addx -8
+noop
+addx 16
+addx 2
+addx 4
+addx 1
+noop
+addx -17
+addx 18
+addx 2
+addx 5
+addx 2
+addx 1
+addx -11
+addx -27
+addx 17
+addx -10
+addx 3
+addx -2
+addx 2
+addx 7
+noop
+addx -2
+noop
+addx 3
+addx 2
+noop
+addx 3
+addx 2
+noop
+addx 3
+addx 2
+addx 5
+addx 2
+addx -5
+addx -2
+addx -30
+addx 14
+addx -7
+addx 22
+addx -21
+addx 2
+addx 6
+addx 2
+addx -1
+noop
+addx 8
+addx -3
+noop
+addx 5
+addx 1
+addx 4
+noop
+addx 3
+addx -2
+addx 2
+addx -11
+noop
+noop
+noop
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
diff --git a/day10/part2.rb b/day10/part2.rb
new file mode 100644
index 0000000..461f275
--- /dev/null
+++ b/day10/part2.rb
@@ -0,0 +1,50 @@
+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
+
+until cpu.cycle == 241
+ hp = (cpu.cycle - 1) % 40
+ px = (hp - cpu.x).abs < 2
+
+ print px ? ?█ : ?\s
+ puts if hp == 39
+
+ cpu.clock
+end
diff --git a/day10/test b/day10/test
new file mode 100644
index 0000000..f428b7b
--- /dev/null
+++ b/day10/test
@@ -0,0 +1,3 @@
+noop
+addx 3
+addx -5
diff --git a/day10/test2 b/day10/test2
new file mode 100644
index 0000000..37ee8ee
--- /dev/null
+++ b/day10/test2
@@ -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