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