-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsticks.cr
33 lines (27 loc) · 914 Bytes
/
sticks.cr
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
require "../src/stick"
require "../src/topological_sort"
if ARGV.empty?
puts "usage: #{PROGRAM_NAME} input"
exit(1)
end
raw_lines = File.read_lines(ARGV[0])
sticks = raw_lines[1..-1].each_with_object({} of UInt32 => Stick) { |l, h|
raw_id, raw_vals = l.split(':')
vals = raw_vals.split(',').map(&.to_f)
id = raw_id.to_u32
h[id] = Stick.new(id, vals[0], vals[1], vals[2], vals[3])
}
# `Hash(UInt32, Array(UInt32)).new { |h, k| h[k] = [] of UInt32 }` is a possibility,
# but we have to be careful because each stick must have an entry!
# Otherwise, the sticks with [] blockers will not be in the hash,
# and `paths_from` won't be able to make the paths!
covers = {} of UInt32 => Array(UInt32)
sticks.each { |id1, stick1|
covers[id1] = [] of UInt32
sticks.each { |id2, stick2|
next if id2 == id1
covers[id1] << id2 if stick1.blocked_by?(stick2)
}
}
puts covers
puts paths_from(covers)