aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2019-12-10 11:05:55 +0000
committerNat Lasseter <user@4574.co.uk>2019-12-10 11:05:55 +0000
commit1bc6e9dd7fa439b7126770d58ac8662aaa0e4441 (patch)
treea611ddcfd31b182c7af84f52850e8a450df9ea0a
parentc8d92acd37d553e98ec312ca172c374138d7798a (diff)
Day 10
-rw-r--r--day10/Dockerfile7
-rw-r--r--day10/Makefile14
-rwxr-xr-xday10/entrypoint13
-rw-r--r--day10/input26
-rwxr-xr-xday10/part161
-rwxr-xr-xday10/part2101
6 files changed, 222 insertions, 0 deletions
diff --git a/day10/Dockerfile b/day10/Dockerfile
new file mode 100644
index 0000000..e5adcb1
--- /dev/null
+++ b/day10/Dockerfile
@@ -0,0 +1,7 @@
+FROM ruby:2.6.5-slim
+
+WORKDIR /opt
+
+COPY . .
+
+ENTRYPOINT ["./entrypoint"]
diff --git a/day10/Makefile b/day10/Makefile
new file mode 100644
index 0000000..645a112
--- /dev/null
+++ b/day10/Makefile
@@ -0,0 +1,14 @@
+DAY = 10
+
+.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/day10/entrypoint b/day10/entrypoint
new file mode 100755
index 0000000..8982d21
--- /dev/null
+++ b/day10/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/day10/input b/day10/input
new file mode 100644
index 0000000..43bbd7c
--- /dev/null
+++ b/day10/input
@@ -0,0 +1,26 @@
+.##.#.#....#.#.#..##..#.#.
+#.##.#..#.####.##....##.#.
+###.##.##.#.#...#..###....
+####.##..###.#.#...####..#
+..#####..#.#.#..#######..#
+.###..##..###.####.#######
+.##..##.###..##.##.....###
+#..#..###..##.#...#..####.
+....#.#...##.##....#.#..##
+..#.#.###.####..##.###.#.#
+.#..##.#####.##.####..#.#.
+#..##.#.#.###.#..##.##....
+#.#.##.#.##.##......###.#.
+#####...###.####..#.##....
+.#####.#.#..#.##.#.#...###
+.#..#.##.#.#.##.#....###.#
+.......###.#....##.....###
+#..#####.#..#..##..##.#.##
+##.#.###..######.###..#..#
+#.#....####.##.###....####
+..#.#.#.########.....#.#.#
+.##.#.#..#...###.####..##.
+##...###....#.##.##..#....
+..##.##.##.#######..#...#.
+.###..#.#..#...###..###.#.
+#..#..#######..#.#..#..#.#
diff --git a/day10/part1 b/day10/part1
new file mode 100755
index 0000000..a7859d1
--- /dev/null
+++ b/day10/part1
@@ -0,0 +1,61 @@
+#!/usr/bin/env ruby
+
+input = $stdin.readlines.map(&:strip).map(&:chars)
+ .map{ |l| l.map{ |c| c == "#" } }
+
+H = input.length
+W = input[0].length
+
+def flip_x(arr)
+ arr.map do |x,y|
+ [-x, y]
+ end
+end
+
+def flip_y(arr)
+ arr.map do |x,y|
+ [x, -y]
+ end
+end
+
+def count(grid, x, y, angles)
+ c = 0
+ angles.each do |dx, dy|
+ tx = x
+ ty = y
+ loop do
+ tx += dx
+ ty += dy
+ break if tx < 0 || tx >= W
+ break if ty < 0 || ty >= H
+ if grid[ty][tx] then
+ c += 1
+ break
+ end
+ end
+ end
+ c
+end
+
+angles = []
+(0...W).each do |x|
+ (0...H).each do |y|
+ angles << [x, y] if x.gcd(y) == 1
+ end
+end
+
+angles += flip_x(angles)
+angles += flip_y(angles)
+angles.uniq!
+
+maxcount = 0
+(0...H).each do |y|
+ (0...W).each do |x|
+ if input[y][x] then
+ c = count(input, x, y, angles)
+ maxcount = c if c > maxcount
+ end
+ end
+end
+
+puts maxcount
diff --git a/day10/part2 b/day10/part2
new file mode 100755
index 0000000..f931a36
--- /dev/null
+++ b/day10/part2
@@ -0,0 +1,101 @@
+#!/usr/bin/env ruby
+
+input = $stdin.readlines.map(&:strip).map(&:chars)
+ .map{ |l| l.map{ |c| c == "#" } }
+
+H = input.length
+W = input[0].length
+
+def count(grid, x, y, angles)
+ c = 0
+ angles.each do |dx, dy|
+ tx = x
+ ty = y
+ loop do
+ tx += dx
+ ty += dy
+ break if tx < 0 || tx >= W
+ break if ty < 0 || ty >= H
+ if grid[ty][tx] then
+ c += 1
+ break
+ end
+ end
+ end
+ c
+end
+
+def rotate(n, arr)
+ arr.map do |x, y|
+ case n
+ when 1
+ [-y, x]
+ when 2
+ [-x, -y]
+ when 3
+ [y, -x]
+ end
+ end
+end
+
+def coords_of(n, grid, x, y, angles)
+ c = 0
+ loop do
+ tx = x
+ ty = y
+ angles.each do |dx, dy|
+ s = 1
+ loop do
+ tx = x + s*dx
+ ty = y + s*dy
+ break if tx < 0 || tx >= W
+ break if ty < 0 || ty >= H
+ if grid[ty][tx] then
+ c += 1
+ return [tx, ty] if c == 200
+ break
+ else
+ s += 1
+ end
+ end
+ end
+ end
+end
+
+angles = []
+(0...W).each do |x|
+ (0...H).each do |y|
+ angles << [x, -y] if x.gcd(y) == 1
+ end
+end
+
+angles.sort! do |a,b|
+ b[0].to_f/b[1] <=> a[0].to_f/a[1]
+end
+
+angles.shift
+
+angles = angles +
+ rotate(1, angles) +
+ rotate(2, angles) +
+ rotate(3, angles)
+
+maxcount = 0
+stationx = nil
+stationy = nil
+(0...H).each do |y|
+ (0...W).each do |x|
+ if input[y][x] then
+ c = count(input, x, y, angles)
+ if c > maxcount
+ maxcount = c
+ stationx = x
+ stationy = y
+ end
+ end
+ end
+end
+
+asteroid200 = coords_of(200, input, stationx, stationy, angles)
+
+puts (100 * asteroid200[0]) + asteroid200[1]