aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2019-12-02 12:58:09 +0000
committerNat Lasseter <user@4574.co.uk>2019-12-02 12:58:09 +0000
commit423d2bcb74df7290d2d7e8d3d4aaea278cd1e98b (patch)
treeb459d4fb4f3bc622c661d512f9825702c7aaf2f1
parentb1692f98b614e731cd42fc770dff77e2f3c136ff (diff)
Day 02, though it's not going to sclae whell when we add more instructions!
-rw-r--r--day02/Dockerfile7
-rw-r--r--day02/Makefile13
-rwxr-xr-xday02/entrypoint13
-rw-r--r--day02/input1
-rwxr-xr-xday02/part128
-rwxr-xr-xday02/part243
6 files changed, 105 insertions, 0 deletions
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