-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_restroom_redoubt.rb
61 lines (53 loc) · 1.38 KB
/
14_restroom_redoubt.rb
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
height = if harg = ARGV.find { |x| x.start_with?('-h') }
ARGV.delete(harg)
Integer(harg[2..])
else
103
end
width = if warg = ARGV.find { |x| x.start_with?('-w') }
ARGV.delete(warg)
Integer(warg[2..])
else
101
end
verbose = ARGV.delete('-v')
def tree?(robots, height, width, t)
h = {}
robots.none? { |px, py, vx, vy|
y = (py + vy * t) % height
x = (px + vx * t) % width
k = y * width + x
h[k].tap { h[k] = true }
}
end
robots = ARGF.map { |line|
line.scan(/-?\d+/).map { Integer(_1, 10) }.freeze
}.freeze
quadrant = robots.map { |px, py, vx, vy|
[(px + vx * 100) % width <=> (width / 2), (py + vy * 100) % height <=> (height / 2)]
}.tally.freeze
p quadrant if verbose
puts quadrant[[-1, -1]] * quadrant[[-1, 1]] * quadrant[[1, -1]] * quadrant[[1, 1]]
exit(0) if robots.size <= 12
yt = height.times.max_by { |t|
ys = robots.map { |_, py, _, vy| (py + vy * t) % height }
ys.tally.values.max
}
xt = width.times.max_by { |t|
xs = robots.map { |px, _, vx, _| (px + vx * t) % width }
xs.tally.values.max
}
p [[yt, height], [xt, width]] if verbose
puts t = yt.step(by: height).find { |t| t % width == xt }
if verbose
poses = robots.to_h { |px, py, vx, vy|
y = (py + vy * t) % height
x = (px + vx * t) % width
[y * width + x, true]
}.freeze
height.times { |y|
puts width.times.map { |x|
poses[y * width + x] ? ?# : ' '
}.join
}
end