aboutsummaryrefslogtreecommitdiff
path: root/day10/part1
diff options
context:
space:
mode:
Diffstat (limited to 'day10/part1')
-rwxr-xr-xday10/part161
1 files changed, 61 insertions, 0 deletions
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