aboutsummaryrefslogtreecommitdiff
path: root/day11
diff options
context:
space:
mode:
Diffstat (limited to 'day11')
-rw-r--r--day11/Dockerfile7
-rw-r--r--day11/Makefile14
-rwxr-xr-xday11/entrypoint13
-rw-r--r--day11/input1
-rwxr-xr-xday11/part1164
-rwxr-xr-xday11/part2195
6 files changed, 394 insertions, 0 deletions
diff --git a/day11/Dockerfile b/day11/Dockerfile
new file mode 100644
index 0000000..e5adcb1
--- /dev/null
+++ b/day11/Dockerfile
@@ -0,0 +1,7 @@
+FROM ruby:2.6.5-slim
+
+WORKDIR /opt
+
+COPY . .
+
+ENTRYPOINT ["./entrypoint"]
diff --git a/day11/Makefile b/day11/Makefile
new file mode 100644
index 0000000..7b89f8b
--- /dev/null
+++ b/day11/Makefile
@@ -0,0 +1,14 @@
+DAY = 11
+
+.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/day11/entrypoint b/day11/entrypoint
new file mode 100755
index 0000000..8982d21
--- /dev/null
+++ b/day11/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/day11/input b/day11/input
new file mode 100644
index 0000000..8a5faf7
--- /dev/null
+++ b/day11/input
@@ -0,0 +1 @@
+3,8,1005,8,327,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,28,1006,0,42,2,1104,11,10,1006,0,61,2,1005,19,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,102,1,8,65,1006,0,4,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,89,1,1108,10,10,1,1103,11,10,1,109,18,10,1006,0,82,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,126,2,109,7,10,1,104,3,10,1006,0,64,2,1109,20,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,163,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,185,2,1109,12,10,2,103,16,10,1,107,11,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,219,1,1005,19,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,245,2,1002,8,10,1,2,9,10,1006,0,27,1006,0,37,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,281,1006,0,21,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,306,101,1,9,9,1007,9,1075,10,1005,10,15,99,109,649,104,0,104,1,21102,1,847069852568,1,21101,344,0,0,1105,1,448,21101,0,386979963688,1,21101,355,0,0,1105,1,448,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,46346031251,1,1,21101,0,402,0,1105,1,448,21102,1,29195594775,1,21101,0,413,0,1105,1,448,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,868498428772,1,21101,0,436,0,1106,0,448,21102,718170641172,1,1,21102,1,447,0,1105,1,448,99,109,2,21202,-1,1,1,21102,40,1,2,21102,1,479,3,21102,1,469,0,1105,1,512,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,474,475,490,4,0,1001,474,1,474,108,4,474,10,1006,10,506,1101,0,0,474,109,-2,2106,0,0,0,109,4,2102,1,-1,511,1207,-3,0,10,1006,10,529,21101,0,0,-3,22101,0,-3,1,22101,0,-2,2,21101,0,1,3,21101,548,0,0,1106,0,553,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,576,2207,-4,-2,10,1006,10,576,21202,-4,1,-4,1106,0,644,22101,0,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,1,595,0,1105,1,553,21201,1,0,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,614,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,636,22102,1,-1,1,21102,1,636,0,106,0,511,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
diff --git a/day11/part1 b/day11/part1
new file mode 100755
index 0000000..4962e2e
--- /dev/null
+++ b/day11/part1
@@ -0,0 +1,164 @@
+#!/usr/bin/env ruby
+
+class Machine
+ def initialize(starting_memory = [])
+ @mem = starting_memory
+ @pc = 0
+ @halt = false
+ @buffered_input = []
+ @buffered_output = []
+ @relative_base = 0
+ 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)
+ if x.is_a?(Array) then
+ @buffered_input += x
+ else
+ @buffered_input.push(x)
+ end
+ end
+
+ def buffered_output(flush = false)
+ t = @buffered_output
+ @buffered_output = [] if flush
+ 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
+
+ l1w, l1r = get(p1, 1)
+ l2w, l2r = get(p2, 2) if [1,2,5,6,7,8].include?(opcode)
+ l3w, l3r = get(p3, 3) if [1,2,7,8].include?(opcode)
+
+ 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
+ when 9
+ @relative_base += l1r
+ @pc += 2
+ end
+ end
+
+ def run
+ until halt? do
+ step
+ end
+ end
+
+ private
+
+ def get(p, a)
+ w = case p
+ when "0"
+ @mem[@pc + a]
+ when "2"
+ @relative_base + @mem[@pc + a]
+ end
+ r = case p
+ when "0"
+ @mem[@mem[@pc + a]]
+ when "1"
+ @mem[@pc + a]
+ when "2"
+ @mem[@relative_base + @mem[@pc + a]]
+ end
+ [w, r || 0]
+ end
+end
+
+def turn(d, t)
+ if t.zero? then
+ case d
+ when [0, -1]
+ [-1, 0]
+ when [-1, 0]
+ [0, 1]
+ when [0, 1]
+ [1, 0]
+ when [1, 0]
+ [0, -1]
+ end
+ else
+ case d
+ when [0, -1]
+ [1, 0]
+ when [-1, 0]
+ [0, -1]
+ when [0, 1]
+ [-1, 0]
+ when [1, 0]
+ [0, 1]
+ end
+ end
+end
+
+def move(l, d)
+ [l[0] + d[0], l[1] + d[1]]
+end
+
+input = $stdin.readlines[0].strip.split(",").map(&:to_i)
+
+m = Machine.new(input)
+grid = Hash.new{0}
+
+l = [0, 0]
+d = [0, -1]
+loop do
+ m.buffer_input(grid[l])
+ until m.halt? || m.buffered_output.length == 2 do
+ m.step
+ end
+ if m.halt? then
+ break
+ else
+ p, t = m.buffered_output(true)
+ grid[l] = p
+ d = turn(d, t)
+ l = move(l, d)
+ end
+end
+
+puts grid.keys.count
diff --git a/day11/part2 b/day11/part2
new file mode 100755
index 0000000..0f9dde2
--- /dev/null
+++ b/day11/part2
@@ -0,0 +1,195 @@
+#!/usr/bin/env ruby
+
+class Machine
+ def initialize(starting_memory = [])
+ @mem = starting_memory
+ @pc = 0
+ @halt = false
+ @buffered_input = []
+ @buffered_output = []
+ @relative_base = 0
+ 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)
+ if x.is_a?(Array) then
+ @buffered_input += x
+ else
+ @buffered_input.push(x)
+ end
+ end
+
+ def buffered_output(flush = false)
+ t = @buffered_output
+ @buffered_output = [] if flush
+ 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
+
+ l1w, l1r = get(p1, 1)
+ l2w, l2r = get(p2, 2) if [1,2,5,6,7,8].include?(opcode)
+ l3w, l3r = get(p3, 3) if [1,2,7,8].include?(opcode)
+
+ 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
+ when 9
+ @relative_base += l1r
+ @pc += 2
+ end
+ end
+
+ def run
+ until halt? do
+ step
+ end
+ end
+
+ private
+
+ def get(p, a)
+ w = case p
+ when "0"
+ @mem[@pc + a]
+ when "2"
+ @relative_base + @mem[@pc + a]
+ end
+ r = case p
+ when "0"
+ @mem[@mem[@pc + a]]
+ when "1"
+ @mem[@pc + a]
+ when "2"
+ @mem[@relative_base + @mem[@pc + a]]
+ end
+ [w, r || 0]
+ end
+end
+
+def turn(d, t)
+ if t.zero? then
+ case d
+ when [0, -1]
+ [-1, 0]
+ when [-1, 0]
+ [0, 1]
+ when [0, 1]
+ [1, 0]
+ when [1, 0]
+ [0, -1]
+ end
+ else
+ case d
+ when [0, -1]
+ [1, 0]
+ when [-1, 0]
+ [0, -1]
+ when [0, 1]
+ [-1, 0]
+ when [1, 0]
+ [0, 1]
+ end
+ end
+end
+
+def move(l, d)
+ [l[0] + d[0], l[1] + d[1]]
+end
+
+def make2darray(hash)
+ dx = -hash.keys.map(&:first).min
+ dy = -hash.keys.map(&:last).min
+ maxx = hash.keys.map(&:first).max + dx
+ maxy = hash.keys.map(&:last).max + dy
+ array = Array.new(maxy + 1) { Array.new(maxx + 1, 0) }
+ hash.each do |l, p|
+ array[l.last + dy][l.first + dx] = p
+ end
+ array
+end
+
+input = $stdin.readlines[0].strip.split(",").map(&:to_i)
+
+m = Machine.new(input)
+grid = Hash.new{0}
+grid[[0,0]] = 1
+
+l = [0, 0]
+d = [0, -1]
+loop do
+ m.buffer_input(grid[l])
+ until m.halt? || m.buffered_output.length == 2 do
+ m.step
+ end
+ if m.halt? then
+ break
+ else
+ p, t = m.buffered_output(true)
+ grid[l] = p
+ d = turn(d, t)
+ l = move(l, d)
+ end
+end
+
+grid = make2darray(grid)
+
+grid.each_slice(2) do |u, l|
+ w = u.length
+ l = Array.new(w, 0) if l.nil?
+ w.times do |i|
+ case [u[i], l[i]]
+ when [0,0]
+ print " "
+ when [0,1]
+ print "▄"
+ when [1,0]
+ print "▀"
+ when [1,1]
+ print "█"
+ end
+ end
+ puts
+end