aboutsummaryrefslogtreecommitdiff
path: root/day12
diff options
context:
space:
mode:
Diffstat (limited to 'day12')
-rw-r--r--day12/Dockerfile7
-rw-r--r--day12/Makefile14
-rwxr-xr-xday12/entrypoint13
-rw-r--r--day12/input4
-rwxr-xr-xday12/part143
5 files changed, 81 insertions, 0 deletions
diff --git a/day12/Dockerfile b/day12/Dockerfile
new file mode 100644
index 0000000..e5adcb1
--- /dev/null
+++ b/day12/Dockerfile
@@ -0,0 +1,7 @@
+FROM ruby:2.6.5-slim
+
+WORKDIR /opt
+
+COPY . .
+
+ENTRYPOINT ["./entrypoint"]
diff --git a/day12/Makefile b/day12/Makefile
new file mode 100644
index 0000000..6d578a1
--- /dev/null
+++ b/day12/Makefile
@@ -0,0 +1,14 @@
+DAY = 12
+
+.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/day12/entrypoint b/day12/entrypoint
new file mode 100755
index 0000000..8982d21
--- /dev/null
+++ b/day12/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/day12/input b/day12/input
new file mode 100644
index 0000000..14eda85
--- /dev/null
+++ b/day12/input
@@ -0,0 +1,4 @@
+<x=19, y=-10, z=7>
+<x=1, y=2, z=-3>
+<x=14, y=-4, z=1>
+<x=8, y=7, z=-6>
diff --git a/day12/part1 b/day12/part1
new file mode 100755
index 0000000..2debadf
--- /dev/null
+++ b/day12/part1
@@ -0,0 +1,43 @@
+#!/usr/bin/env ruby
+
+def apply_gravity(moons)
+ (0...moons.count).to_a.combination(2).each do |a, b|
+ 3.times do |i|
+ case moons[a][0][i] <=> moons[b][0][i]
+ when -1
+ moons[a][1][i] += 1
+ moons[b][1][i] -= 1
+ when 1
+ moons[b][1][i] += 1
+ moons[a][1][i] -= 1
+ end
+ end
+ end
+ moons
+end
+
+def update(moons)
+ moons.map do |moon|
+ [moon[0].zip(moon[1]).map(&:sum), moon[1]]
+ end
+end
+
+def calculate_energy(moons)
+ moons.map { |moon|
+ moon[0].map(&:abs).sum * moon[1].map(&:abs).sum
+ }.sum
+end
+
+input = $stdin.readlines.map(&:strip)
+
+moons = input.map do |line|
+ m = line.scan(/-?\d+/)
+ [[m[0].to_i, m[1].to_i, m[2].to_i], [0, 0, 0]]
+end
+
+1000.times do
+ moons = apply_gravity(moons)
+ moons = update(moons)
+end
+
+puts calculate_energy(moons)