From 1bc6e9dd7fa439b7126770d58ac8662aaa0e4441 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 10 Dec 2019 11:05:55 +0000 Subject: Day 10 --- day10/Dockerfile | 7 ++++ day10/Makefile | 14 ++++++++ day10/entrypoint | 13 +++++++ day10/input | 26 ++++++++++++++ day10/part1 | 61 +++++++++++++++++++++++++++++++++ day10/part2 | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 222 insertions(+) create mode 100644 day10/Dockerfile create mode 100644 day10/Makefile create mode 100755 day10/entrypoint create mode 100644 day10/input create mode 100755 day10/part1 create mode 100755 day10/part2 (limited to 'day10') 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] -- cgit v1.2.1