-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay01.rb
More file actions
32 lines (25 loc) · 938 Bytes
/
Day01.rb
File metadata and controls
32 lines (25 loc) · 938 Bytes
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
# (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 Day01
def self.part01(path, debug)
spaceliner = {}
prices = []
File.open(path, "r") do |file|
file.each_line do |line|
chunks = line.downcase.split(/[:\s]+/)
chunks[2] = chunks[2].to_i
chunks[2] *= -1 if %w[discount rebate].include?(chunks[1])
spaceliner[chunks[0]] ||= {}
spaceliner[chunks[0]][chunks[1]] ||= 0
spaceliner[chunks[0]][chunks[1]] += chunks[2]
end
end
spaceliner.each do |key, value|
prices.push({ spaceliner: key, price: value.values.sum })
end
prices.sort_by! { |element| element[:price] }
prices.first[:price]
end
end