aboutsummaryrefslogtreecommitdiff
path: root/day10/part1
blob: a7859d15fa2c2f62176fdf1a216af078ed244279 (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
56
57
58
59
60
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