aboutsummaryrefslogtreecommitdiff
path: root/day06/part1
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2018-12-06 12:07:00 +0000
committerNat Lasseter <user@4574.co.uk>2018-12-06 12:07:00 +0000
commit08fd489704409e1d7414ff51bdac5d789cabb668 (patch)
treeca14c4a40d7cd90c8507f99c550e2c8297e19467 /day06/part1
parent67d468a613bd6e20b2e9607f350bc0878e2ae8d8 (diff)
[day06] Done
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