-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay05.rb
More file actions
47 lines (35 loc) · 1.18 KB
/
Day05.rb
File metadata and controls
47 lines (35 loc) · 1.18 KB
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
# (c) Manuel Alejandro Gómez Nicasio <az-dev@outlook.com>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
class Day05
def self.part01(path, debug)
distances = {}
routes = []
rovers = []
File.open(path, "r") do |file|
destinations = file.gets.split.map(&:to_sym)
file.each_line do |line|
break if line.strip == ""
chunks = line.split
distances[chunks[0].to_sym] = {}
chunks.drop(1).each_with_index do |element, index|
distances[chunks[0].to_sym][destinations[index]] = element.to_i
end
end
file.each_line do |line|
routes.push line.split(/:|->/).drop(1).map(&:strip).map(&:to_sym)
end
end
routes.each do |route|
distance = 0
prev = route[0]
route.drop(1).each do |element|
distance += distances[prev][element]
prev = element
end
rovers.push distance
end
rovers.sum
end
end