From c10f57e05b79ac82d8fc55c534a0e665b3e13344 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Thu, 12 Dec 2019 10:47:43 +0000 Subject: Day 12, Part 1 only --- day12/Dockerfile | 7 +++++++ day12/Makefile | 14 ++++++++++++++ day12/entrypoint | 13 +++++++++++++ day12/input | 4 ++++ day12/part1 | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 day12/Dockerfile create mode 100644 day12/Makefile create mode 100755 day12/entrypoint create mode 100644 day12/input create mode 100755 day12/part1 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 @@ + + + + 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) -- cgit v1.2.1