From 423d2bcb74df7290d2d7e8d3d4aaea278cd1e98b Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Mon, 2 Dec 2019 12:58:09 +0000 Subject: Day 02, though it's not going to sclae whell when we add more instructions! --- day02/Dockerfile | 7 +++++++ day02/Makefile | 13 +++++++++++++ day02/entrypoint | 13 +++++++++++++ day02/input | 1 + day02/part1 | 28 ++++++++++++++++++++++++++++ day02/part2 | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 day02/Dockerfile create mode 100644 day02/Makefile create mode 100755 day02/entrypoint create mode 100644 day02/input create mode 100755 day02/part1 create mode 100755 day02/part2 diff --git a/day02/Dockerfile b/day02/Dockerfile new file mode 100644 index 0000000..e5adcb1 --- /dev/null +++ b/day02/Dockerfile @@ -0,0 +1,7 @@ +FROM ruby:2.6.5-slim + +WORKDIR /opt + +COPY . . + +ENTRYPOINT ["./entrypoint"] diff --git a/day02/Makefile b/day02/Makefile new file mode 100644 index 0000000..347e7e5 --- /dev/null +++ b/day02/Makefile @@ -0,0 +1,13 @@ +DAY = 02 + +.PHONY: run clean + +run: build + docker run -it --rm aoc2019day$(DAY) + +build: part* input + docker build -t aoc2019day$(DAY) . + touch build + +clean: + rm -f build diff --git a/day02/entrypoint b/day02/entrypoint new file mode 100755 index 0000000..8982d21 --- /dev/null +++ b/day02/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/day02/input b/day02/input new file mode 100644 index 0000000..ac16973 --- /dev/null +++ b/day02/input @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,19,5,23,2,9,23,27,1,5,27,31,1,5,31,35,1,35,13,39,1,39,9,43,1,5,43,47,1,47,6,51,1,51,13,55,1,55,9,59,1,59,13,63,2,63,13,67,1,67,10,71,1,71,6,75,2,10,75,79,2,10,79,83,1,5,83,87,2,6,87,91,1,91,6,95,1,95,13,99,2,99,13,103,1,103,9,107,1,10,107,111,2,111,13,115,1,10,115,119,1,10,119,123,2,13,123,127,2,6,127,131,1,13,131,135,1,135,2,139,1,139,6,0,99,2,0,14,0 diff --git a/day02/part1 b/day02/part1 new file mode 100755 index 0000000..460b3b5 --- /dev/null +++ b/day02/part1 @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby + +$input = $stdin.readlines[0].strip.split(",").map(&:to_i) +$pc = 0 + +def handle_code + case $input[$pc] + when 1 + $input[$input[$pc+3]] = $input[$input[$pc+1]] + $input[$input[$pc+2]] + return true + when 2 + $input[$input[$pc+3]] = $input[$input[$pc+1]] * $input[$input[$pc+2]] + return true + when 99 + return false + end +end + +$input[1] = 12 +$input[2] = 2 + +loop do + continue = handle_code + break unless continue + $pc += 4 +end + +puts $input[0] diff --git a/day02/part2 b/day02/part2 new file mode 100755 index 0000000..aadb860 --- /dev/null +++ b/day02/part2 @@ -0,0 +1,43 @@ +#!/usr/bin/env ruby + +Input = $stdin.readlines[0].strip.split(",").map(&:to_i) + +noun = 0 +verb = 0 + +loop do + $input = Input.dup + $input[1] = noun + $input[2] = verb + + $pc = 0 + + def handle_code + case $input[$pc] + when 1 + $input[$input[$pc+3]] = $input[$input[$pc+1]] + $input[$input[$pc+2]] + return true + when 2 + $input[$input[$pc+3]] = $input[$input[$pc+1]] * $input[$input[$pc+2]] + return true + when 99 + return false + end + end + + loop do + continue = handle_code + break unless continue + $pc += 4 + end + + break if $input[0] == 19690720 + + verb += 1 + if verb == 100 + verb = 0 + noun += 1 + end +end + +puts (100 * noun) + verb -- cgit v1.2.1