aboutsummaryrefslogtreecommitdiff
path: root/day05
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2019-12-05 13:44:02 +0000
committerNat Lasseter <user@4574.co.uk>2019-12-05 13:44:02 +0000
commitfdaf6d524f2805fbba25eef2572eee9768f25a40 (patch)
treeadf912a51102c98366eba7952857ab38fccb6ec3 /day05
parente88f1129a40134fb60a2e90b495327f6ccada871 (diff)
Day 05
Diffstat (limited to 'day05')
-rw-r--r--day05/Dockerfile7
-rw-r--r--day05/Makefile14
-rwxr-xr-xday05/entrypoint13
-rw-r--r--day05/input1
-rwxr-xr-xday05/part179
-rwxr-xr-xday05/part294
6 files changed, 208 insertions, 0 deletions
diff --git a/day05/Dockerfile b/day05/Dockerfile
new file mode 100644
index 0000000..e5adcb1
--- /dev/null
+++ b/day05/Dockerfile
@@ -0,0 +1,7 @@
+FROM ruby:2.6.5-slim
+
+WORKDIR /opt
+
+COPY . .
+
+ENTRYPOINT ["./entrypoint"]
diff --git a/day05/Makefile b/day05/Makefile
new file mode 100644
index 0000000..22cc50e
--- /dev/null
+++ b/day05/Makefile
@@ -0,0 +1,14 @@
+DAY = 05
+
+.PHONY: run clean
+
+run: build
+ docker run -it --rm aoc2019day$(DAY)
+
+build: part* input
+ docker build -t aoc2019day$(DAY) .
+ touch build
+
+clean:
+ docker image rm -f aoc2019day$(DAY)
+ rm -f build
diff --git a/day05/entrypoint b/day05/entrypoint
new file mode 100755
index 0000000..8982d21
--- /dev/null
+++ b/day05/entrypoint
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+if [ -x part1 ] ; then
+ echo -ne "Part 1:\n\t"
+ time ./part1 < input
+fi
+if [ -x part1 -a -x part2 ] ; then
+ echo
+fi
+if [ -x part2 ] ; then
+ echo -ne "Part 2:\n\t"
+ time ./part2 < input
+fi
diff --git a/day05/input b/day05/input
new file mode 100644
index 0000000..e3fe016
--- /dev/null
+++ b/day05/input
@@ -0,0 +1 @@
+3,225,1,225,6,6,1100,1,238,225,104,0,2,136,183,224,101,-5304,224,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,1101,72,47,225,1101,59,55,225,1101,46,75,225,1101,49,15,224,101,-64,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,102,9,210,224,1001,224,-270,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,101,14,35,224,101,-86,224,224,4,224,1002,223,8,223,101,4,224,224,1,224,223,223,1102,40,74,224,1001,224,-2960,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1101,10,78,225,1001,39,90,224,1001,224,-149,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,1002,217,50,224,1001,224,-1650,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1102,68,8,225,1,43,214,224,1001,224,-126,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1102,88,30,225,1102,18,80,225,1102,33,28,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,677,677,224,102,2,223,223,1005,224,329,1001,223,1,223,1107,677,226,224,102,2,223,223,1006,224,344,1001,223,1,223,108,226,226,224,102,2,223,223,1005,224,359,1001,223,1,223,1108,677,226,224,102,2,223,223,1006,224,374,101,1,223,223,108,677,226,224,102,2,223,223,1006,224,389,1001,223,1,223,107,226,226,224,102,2,223,223,1005,224,404,1001,223,1,223,8,226,226,224,102,2,223,223,1006,224,419,101,1,223,223,1107,677,677,224,102,2,223,223,1006,224,434,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,449,101,1,223,223,7,677,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,1108,226,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,494,101,1,223,223,7,226,677,224,102,2,223,223,1005,224,509,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,524,101,1,223,223,8,226,677,224,1002,223,2,223,1006,224,539,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,554,101,1,223,223,107,226,677,224,1002,223,2,223,1005,224,569,1001,223,1,223,1108,677,677,224,1002,223,2,223,1006,224,584,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,599,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,614,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,107,677,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,677,224,1002,223,2,223,1005,224,659,1001,223,1,223,1007,226,226,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226
diff --git a/day05/part1 b/day05/part1
new file mode 100755
index 0000000..b021d10
--- /dev/null
+++ b/day05/part1
@@ -0,0 +1,79 @@
+#!/usr/bin/env ruby
+
+class Machine
+ def initialize(starting_memory = [])
+ @mem = starting_memory
+ @pc = 0
+ @halt = false
+ @buffered_input = []
+ @buffered_output = []
+ end
+
+ def mem(start = 0, len = @mem.length)
+ @mem[start...(start + len)]
+ end
+
+ def halt!
+ @halt = true
+ end
+
+ def halt?
+ @halt
+ end
+
+ def buffer_input(x)
+ @buffered_input.push(x)
+ end
+
+ def buffered_output
+ t = @buffered_output
+ @buffered_output = []
+ t
+ end
+
+ def step
+ return if halt?
+
+ opcode = @mem[@pc] % 100
+ p3, p2, p1 = ("%03d" % (@mem[@pc] / 100)).chars.map{|c|c == "1"}
+
+ l1w = @mem[@pc + 1]
+ l1r = p1 ? l1w : @mem[l1w]
+ l2w = @mem[@pc + 2]
+ l2r = p2 ? l2w : @mem[l2w]
+ l3w = @mem[@pc + 3]
+ l3r = p3 ? l3w : @mem[l3w]
+
+ case opcode
+ when 1
+ @mem[l3w] = l1r + l2r
+ @pc += 4
+ when 2
+ @mem[l3w] = l1r * l2r
+ @pc += 4
+ when 3
+ @mem[l1w] = @buffered_input.shift
+ @pc += 2
+ when 4
+ @buffered_output.push(l1r)
+ @pc += 2
+ when 99
+ halt!
+ @pc += 1
+ end
+ end
+
+ def run
+ puts buffered_output
+ until halt? do
+ step
+ puts buffered_output unless @buffered_output.empty?
+ end
+ end
+end
+
+input = $stdin.readlines[0].strip.split(",").map(&:to_i)
+
+m = Machine.new(input)
+m.buffer_input(1)
+m.run
diff --git a/day05/part2 b/day05/part2
new file mode 100755
index 0000000..8263aa7
--- /dev/null
+++ b/day05/part2
@@ -0,0 +1,94 @@
+#!/usr/bin/env ruby
+
+class Machine
+ def initialize(starting_memory = [])
+ @mem = starting_memory
+ @pc = 0
+ @halt = false
+ @buffered_input = []
+ @buffered_output = []
+ end
+
+ def mem(start = 0, len = @mem.length)
+ @mem[start...(start + len)]
+ end
+
+ def halt!
+ @halt = true
+ end
+
+ def halt?
+ @halt
+ end
+
+ def buffer_input(x)
+ @buffered_input.push(x)
+ end
+
+ def buffered_output
+ t = @buffered_output
+ @buffered_output = []
+ t
+ end
+
+ def step
+ return if halt?
+
+ opcode = @mem[@pc] % 100
+
+ if opcode == 99 then
+ halt!
+ @pc += 1
+ return
+ end
+
+ p3, p2, p1 = ("%03d" % (@mem[@pc] / 100)).chars.map{|c|c == "1"}
+
+ l1w = @mem[@pc + 1]
+ l1r = p1 ? l1w : @mem[l1w]
+ l2w = @mem[@pc + 2]
+ l2r = p2 ? l2w : @mem[l2w]
+ l3w = @mem[@pc + 3]
+ l3r = p3 ? l3w : @mem[l3w]
+
+ case opcode
+ when 1
+ @mem[l3w] = l1r + l2r
+ @pc += 4
+ when 2
+ @mem[l3w] = l1r * l2r
+ @pc += 4
+ when 3
+ @mem[l1w] = @buffered_input.shift
+ @pc += 2
+ when 4
+ @buffered_output.push(l1r)
+ @pc += 2
+ when 5
+ @pc = (l1r != 0 ? l2r : @pc + 3)
+ when 6
+ @pc = (l1r == 0 ? l2r : @pc + 3)
+ when 7
+ @mem[l3w] = (l1r < l2r ? 1 : 0)
+ @pc += 4
+ when 8
+ @mem[l3w] = (l1r == l2r ? 1 : 0)
+ @pc += 4
+ end
+ end
+
+ def run
+ puts buffered_output
+ until halt? do
+ step
+ puts buffered_output unless @buffered_output.empty?
+ end
+ end
+end
+
+input = $stdin.readlines[0].strip.split(",").map(&:to_i)
+
+m = Machine.new(input)
+
+m.buffer_input(5)
+m.run