From cb2cfa0ac23d7daf7f7eb601783adc5f30a35d67 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Thu, 19 Dec 2019 17:48:09 +0000 Subject: Day19 part1, runs forever. looks like an 'understanding the input' type problem. --- day19/Dockerfile | 7 ++++ day19/Makefile | 14 +++++++ day19/entrypoint | 13 ++++++ day19/input | 1 + day19/part1 | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+) create mode 100644 day19/Dockerfile create mode 100644 day19/Makefile create mode 100755 day19/entrypoint create mode 100644 day19/input create mode 100755 day19/part1 diff --git a/day19/Dockerfile b/day19/Dockerfile new file mode 100644 index 0000000..e5adcb1 --- /dev/null +++ b/day19/Dockerfile @@ -0,0 +1,7 @@ +FROM ruby:2.6.5-slim + +WORKDIR /opt + +COPY . . + +ENTRYPOINT ["./entrypoint"] diff --git a/day19/Makefile b/day19/Makefile new file mode 100644 index 0000000..6608373 --- /dev/null +++ b/day19/Makefile @@ -0,0 +1,14 @@ +DAY = 19 + +.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/day19/entrypoint b/day19/entrypoint new file mode 100755 index 0000000..8982d21 --- /dev/null +++ b/day19/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/day19/input b/day19/input new file mode 100644 index 0000000..ad5bc2f --- /dev/null +++ b/day19/input @@ -0,0 +1 @@ +109,424,203,1,21101,11,0,0,1105,1,282,21102,18,1,0,1105,1,259,2102,1,1,221,203,1,21102,1,31,0,1106,0,282,21101,0,38,0,1106,0,259,20102,1,23,2,21202,1,1,3,21101,0,1,1,21102,57,1,0,1105,1,303,2101,0,1,222,20102,1,221,3,20101,0,221,2,21102,259,1,1,21101,0,80,0,1106,0,225,21102,135,1,2,21101,0,91,0,1105,1,303,2102,1,1,223,21001,222,0,4,21102,259,1,3,21102,1,225,2,21101,0,225,1,21101,118,0,0,1106,0,225,20101,0,222,3,21101,0,12,2,21101,0,133,0,1106,0,303,21202,1,-1,1,22001,223,1,1,21102,1,148,0,1105,1,259,1202,1,1,223,21002,221,1,4,20102,1,222,3,21101,0,17,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,1,195,0,105,1,109,20207,1,223,2,21001,23,0,1,21101,0,-1,3,21101,214,0,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,1202,-4,1,249,21201,-3,0,1,22102,1,-2,2,22102,1,-1,3,21102,250,1,0,1106,0,225,21202,1,1,-4,109,-5,2106,0,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2105,1,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21201,-2,0,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,22102,1,-2,3,21101,0,343,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,22101,0,-4,1,21102,384,1,0,1105,1,303,1106,0,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22102,1,1,-4,109,-5,2106,0,0 diff --git a/day19/part1 b/day19/part1 new file mode 100755 index 0000000..31220e5 --- /dev/null +++ b/day19/part1 @@ -0,0 +1,125 @@ +#!/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 + +input = $stdin.readlines[0].strip.split(",").map(&:to_i) + +count = 0 +50.times do |x| + 50.times do |y| + m = Machine.new(input) + m.buffer_input(x) + m.buffer_input(y) + m.run + count += m.buffered_output.last + end +end + +puts count -- cgit v1.2.1