From 08fd489704409e1d7414ff51bdac5d789cabb668 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Thu, 6 Dec 2018 12:07:00 +0000 Subject: [day06] Done --- day06/Dockerfile | 7 +++++++ day06/Makefile | 17 +++++++++++++++++ day06/entrypoint | 13 +++++++++++++ day06/input | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ day06/part1 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ day06/part2 | 30 ++++++++++++++++++++++++++++++ 6 files changed, 172 insertions(+) create mode 100644 day06/Dockerfile create mode 100644 day06/Makefile create mode 100755 day06/entrypoint create mode 100644 day06/input create mode 100755 day06/part1 create mode 100755 day06/part2 (limited to 'day06') diff --git a/day06/Dockerfile b/day06/Dockerfile new file mode 100644 index 0000000..5e0a8b7 --- /dev/null +++ b/day06/Dockerfile @@ -0,0 +1,7 @@ +FROM ruby:2.5-slim + +WORKDIR /opt + +COPY . . + +ENTRYPOINT ["./entrypoint"] diff --git a/day06/Makefile b/day06/Makefile new file mode 100644 index 0000000..c3e5f10 --- /dev/null +++ b/day06/Makefile @@ -0,0 +1,17 @@ +DAY = 06 + +.PHONY: run clean push + +run: build + docker run -it --rm aoc2018day$(DAY) + +build: part* input + docker build -t aoc2018day$(DAY) . + touch build + +clean: + rm -f build + +push: build + docker tag aoc2018day$(DAY) advent.4574.co.uk/aoc2018day$(DAY) + docker push advent.4574.co.uk/aoc2018day$(DAY) diff --git a/day06/entrypoint b/day06/entrypoint new file mode 100755 index 0000000..8982d21 --- /dev/null +++ b/day06/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/day06/input b/day06/input new file mode 100644 index 0000000..8dcb9fa --- /dev/null +++ b/day06/input @@ -0,0 +1,50 @@ +267, 196 +76, 184 +231, 301 +241, 76 +84, 210 +186, 243 +251, 316 +265, 129 +142, 124 +107, 134 +265, 191 +216, 226 +67, 188 +256, 211 +317, 166 +110, 41 +347, 332 +129, 91 +217, 327 +104, 57 +332, 171 +257, 287 +230, 105 +131, 209 +110, 282 +263, 146 +113, 217 +193, 149 +280, 71 +357, 160 +356, 43 +321, 123 +272, 70 +171, 49 +288, 196 +156, 139 +268, 163 +188, 141 +156, 182 +199, 242 +330, 47 +89, 292 +351, 329 +292, 353 +290, 158 +167, 116 +268, 235 +124, 139 +116, 119 +142, 259 diff --git a/day06/part1 b/day06/part1 new file mode 100755 index 0000000..db6ad25 --- /dev/null +++ b/day06/part1 @@ -0,0 +1,55 @@ +#!/usr/bin/env ruby + +def manhattan_distance(x, y, p) + return (x - p.first).abs + (y - p.last).abs +end + +def closest_points(x, y, points) + dists = points.map{|p|manhattan_distance(x, y, p)} + min = dists.min + return (0...dists.length).each.select{|i|dists[i] == min} +end + +def edge_points(grid) + xa = grid.first + xz = grid.last + ya = grid.map(&:first).flatten + yz = grid.map(&:last).flatten + return (xa + xz + ya + yz).compact.uniq +end + +input = $stdin.readlines.map(&:chomp).map{|i|i.split(", ").map(&:to_i)} + +xoff = input.map(&:first).min - 1 +yoff = input.map(&:last).min - 1 + +input.each do |p| + p[0] -= xoff + p[1] -= yoff +end + +wid = input.map(&:first).max + 1 +hei = input.map(&:last).max + 1 + +grid = Array.new(wid) { Array.new(hei, nil) } + +(0...wid).each do |x| + (0...hei).each do |y| + cps = closest_points(x, y, input) + if cps.length == 1 + grid[x][y] = cps.first + end + end +end + +eps = edge_points(grid) +candidates = (0...input.length).to_a - eps + +grid = grid.flatten.compact +counts = [] + +candidates.each do |c| + counts << grid.count(c) +end + +puts counts.compact.max diff --git a/day06/part2 b/day06/part2 new file mode 100755 index 0000000..dc74890 --- /dev/null +++ b/day06/part2 @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +def manhattan_distance(x, y, p) + return (x - p.first).abs + (y - p.last).abs +end + +input = $stdin.readlines.map(&:chomp).map{|i|i.split(", ").map(&:to_i)} +MAX_DISTANCE = 10000 + +xoff = input.map(&:first).min - 1 +yoff = input.map(&:last).min - 1 + +input.each do |p| + p[0] -= xoff + p[1] -= yoff +end + +wid = input.map(&:first).max + 1 +hei = input.map(&:last).max + 1 + +grid = Array.new(wid) { Array.new(hei, false) } + +(0...wid).each do |x| + (0...hei).each do |y| + dist_sum = input.map{|p|manhattan_distance(x, y, p)}.sum + grid[x][y] = true if dist_sum < MAX_DISTANCE + end +end + +puts grid.flatten.count(true) -- cgit v1.2.1