From ca0ef4147392dc1260ec0d1f513bb24f568dcf1c Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Sat, 7 Dec 2019 09:37:54 +0000 Subject: Day 07 --- day07/Dockerfile | 7 +++ day07/Makefile | 14 ++++++ day07/entrypoint | 13 +++++ day07/input | 1 + day07/part1 | 122 ++++++++++++++++++++++++++++++++++++++++++++ day07/part2 | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 307 insertions(+) create mode 100644 day07/Dockerfile create mode 100644 day07/Makefile create mode 100755 day07/entrypoint create mode 100644 day07/input create mode 100755 day07/part1 create mode 100755 day07/part2 diff --git a/day07/Dockerfile b/day07/Dockerfile new file mode 100644 index 0000000..e5adcb1 --- /dev/null +++ b/day07/Dockerfile @@ -0,0 +1,7 @@ +FROM ruby:2.6.5-slim + +WORKDIR /opt + +COPY . . + +ENTRYPOINT ["./entrypoint"] diff --git a/day07/Makefile b/day07/Makefile new file mode 100644 index 0000000..479fe39 --- /dev/null +++ b/day07/Makefile @@ -0,0 +1,14 @@ +DAY = 07 + +.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/day07/entrypoint b/day07/entrypoint new file mode 100755 index 0000000..8982d21 --- /dev/null +++ b/day07/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/day07/input b/day07/input new file mode 100644 index 0000000..a24bf46 --- /dev/null +++ b/day07/input @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,38,63,72,81,106,187,268,349,430,99999,3,9,101,5,9,9,1002,9,3,9,101,3,9,9,4,9,99,3,9,102,3,9,9,101,4,9,9,1002,9,2,9,1001,9,2,9,1002,9,4,9,4,9,99,3,9,1001,9,3,9,4,9,99,3,9,102,5,9,9,4,9,99,3,9,102,4,9,9,1001,9,2,9,1002,9,5,9,1001,9,2,9,102,3,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99 diff --git a/day07/part1 b/day07/part1 new file mode 100755 index 0000000..10248d5 --- /dev/null +++ b/day07/part1 @@ -0,0 +1,122 @@ +#!/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) + if x.is_a?(Array) then + @buffered_input += x + else + @buffered_input.push(x) + end + 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 + until halt? do + step + end + end +end + +input = $stdin.readlines[0].strip.split(",").map(&:to_i) + +max = 0 +(0..4).to_a.permutation.each do |p| + a = Machine.new(input.dup) + b = Machine.new(input.dup) + c = Machine.new(input.dup) + d = Machine.new(input.dup) + e = Machine.new(input.dup) + + a.buffer_input(p[0]) + b.buffer_input(p[1]) + c.buffer_input(p[2]) + d.buffer_input(p[3]) + e.buffer_input(p[4]) + + a.buffer_input([0] * input.count(3)) + a.run + b.buffer_input(a.buffered_output) + b.run + c.buffer_input(b.buffered_output) + c.run + d.buffer_input(c.buffered_output) + d.run + e.buffer_input(d.buffered_output) + e.run + + s = e.buffered_output.last + max = s if s > max +end + +puts max diff --git a/day07/part2 b/day07/part2 new file mode 100755 index 0000000..73473ff --- /dev/null +++ b/day07/part2 @@ -0,0 +1,150 @@ +#!/usr/bin/env ruby + +class Machine + def initialize(starting_memory = []) + @mem = starting_memory + @pc = 0 + @halt = false + @wait = 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 wait! + @wait = true + end + + def wait? + t = @wait + @wait = false + t + end + + def buffer_input(x) + if x.is_a?(Array) then + @buffered_input += x + else + @buffered_input.push(x) + end + 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] + if [1,2,5,6,7,8].include?(opcode) then + l2w = @mem[@pc + 2] + l2r = p2 ? l2w : @mem[l2w] + end + if [1,2,7,8].include?(opcode) + l3w = @mem[@pc + 3] + l3r = p3 ? l3w : @mem[l3w] + end + + case opcode + when 1 + @mem[l3w] = l1r + l2r + @pc += 4 + when 2 + @mem[l3w] = l1r * l2r + @pc += 4 + when 3 + if @buffered_input.empty? then + wait! + return + end + @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 + until halt? || wait? do + step + end + end +end + +input = $stdin.readlines[0].strip.split(",").map(&:to_i) + +max = 0 +(5..9).to_a.permutation.each do |p| + a = Machine.new(input.dup) + b = Machine.new(input.dup) + c = Machine.new(input.dup) + d = Machine.new(input.dup) + e = Machine.new(input.dup) + + a.buffer_input(p[0]) + b.buffer_input(p[1]) + c.buffer_input(p[2]) + d.buffer_input(p[3]) + e.buffer_input(p[4]) + + first = true + loop do + if first then + first = false + a.buffer_input(0) + else + a.buffer_input(e.buffered_output) + end + a.run + b.buffer_input(a.buffered_output) + b.run + c.buffer_input(b.buffered_output) + c.run + d.buffer_input(c.buffered_output) + d.run + e.buffer_input(d.buffered_output) + e.run + break if e.halt? + end + + s = e.buffered_output.last + max = s if s > max +end + +puts max -- cgit v1.2.1