Add solution for 2015 day 02

This commit is contained in:
Patrick Auernig 2021-02-07 16:48:46 +01:00
parent 64b6e6d8a5
commit 45659f2ca8
4 changed files with 1058 additions and 1 deletions

View File

@ -2,7 +2,7 @@
## Progress
- [x] Day 01
- [ ] Day 02
- [x] Day 02
- [ ] Day 03
- [ ] Day 04
- [ ] Day 05

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
2x3x4
1x1x10

55
2015/day-02/main.rb Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env ruby
require "pathname"
INPUTS = [
Pathname("inputs/test.txt"),
Pathname("inputs/puzzle.txt"),
].freeze
def parse_input(data)
data.chomp.split("x").map(&:to_i)
end
def sides(length, width, height)
[length * width, width * height, height * length]
end
def solve_part_1(input)
input.lines.sum do |line|
sides = sides(*parse_input(line))
smallest = sides.min
sides.sum { |x| x * 2 } + smallest
end
end
def solve_part_2(input)
input.lines.sum do |line|
length, width, height = parse_input(line)
sides = sides(length, width, height)
circumference =
case sides.map.with_index.min.last
when 0 then length * 2 + width * 2
when 1 then width * 2 + height * 2
when 2 then height * 2 + length * 2
end
circumference + length * width * height
end
end
def main(files)
files.each do |file|
puts "File: #{file}"
content = file.read
result = solve_part_1(content.chomp)
puts " Wrapping Paper: #{result}"
puts " -" * 15
result = solve_part_2(content.chomp)
puts " Ribbon Length: #{result}"
puts " -" * 15
end
end
main(INPUTS)