Add solution for 2015 day 12

This commit is contained in:
Patrick Auernig 2021-02-13 05:08:34 +01:00
parent 1b44ce44e5
commit 0928e4a028
4 changed files with 36 additions and 1 deletions

View File

@ -13,7 +13,7 @@
| ✓ | 09 | Rust
| ✓ | 10 | Ruby
| | 11 |
| | 12 |
| ✓ | 12 | Ruby
| | 13 |
| | 14 |
| | 15 |

View File

@ -0,0 +1 @@
3.0.0

File diff suppressed because one or more lines are too long

33
2015/day-12/main.rb Executable file
View 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) })