aboutsummaryrefslogtreecommitdiff
path: root/day06/part1
blob: db6ad25271cf29001199a51bb5c5f967c07b3a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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