Add solution for 2015 day 12
This commit is contained in:
parent
1b44ce44e5
commit
0928e4a028
@ -13,7 +13,7 @@
|
||||
| ✓ | 09 | Rust
|
||||
| ✓ | 10 | Ruby
|
||||
| | 11 |
|
||||
| | 12 |
|
||||
| ✓ | 12 | Ruby
|
||||
| | 13 |
|
||||
| | 14 |
|
||||
| | 15 |
|
||||
|
1
2015/day-12/.ruby-version
Normal file
1
2015/day-12/.ruby-version
Normal file
@ -0,0 +1 @@
|
||||
3.0.0
|
1
2015/day-12/inputs/puzzle.json
Normal file
1
2015/day-12/inputs/puzzle.json
Normal file
File diff suppressed because one or more lines are too long
33
2015/day-12/main.rb
Executable file
33
2015/day-12/main.rb
Executable file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "pathname"
|
||||
require "json"
|
||||
|
||||
def sum_of_numbers(json)
|
||||
case json
|
||||
when Array then json.sum { |e| sum_of_numbers(e) }
|
||||
when Hash then json.sum { |(_, v)| sum_of_numbers(v) }
|
||||
when Numeric then json
|
||||
else 0
|
||||
end
|
||||
end
|
||||
|
||||
def sum_of_numbers_nored(json)
|
||||
case json
|
||||
when Array then json.sum { |e| sum_of_numbers_nored(e) }
|
||||
when Hash then json.value?("red") ? 0 : json.sum { |(_, v)| sum_of_numbers_nored(v) }
|
||||
when Numeric then json
|
||||
else 0
|
||||
end
|
||||
end
|
||||
|
||||
def main(file_paths)
|
||||
file_paths.each do |file_path|
|
||||
json = JSON.parse(file_path.read)
|
||||
puts "#{file_path} / solution 1: #{sum_of_numbers(json)}"
|
||||
puts "#{file_path} / solution 2: #{sum_of_numbers_nored(json)}"
|
||||
end
|
||||
end
|
||||
|
||||
main(ARGV.map { |x| Pathname(x) })
|
Loading…
Reference in New Issue
Block a user