Advent of Code 2015 Day 2
Day 2: I Was Told There Would Be No Math
https://adventofcode.com/2015/day/2
The elves would like to order the exact amount of wrapping paper they need to wrap all presents.
Each present is a perfectly rectangular box.
Today’s input is a list with the dimensions of each gift.
An example input looks like this:
29x13x2611x11x1427x2x5
Each line has the form of LxWxH
.
For the first present in the list above:
- length: 29
- width: 13
- height: 26
Those numbers are unitless. Let’s say Santa uses the imperial system to measure presents. They’re measured in feet.
Part 1
The required amount of wrapping paper for one present:
- Enough to cover all sides
- Some extra: the area of the smallest side
The question asks how many square foot of wrapping paper is needed to wrap all presents.
Main code for part 1
pub fn part_1(input: &str) -> i32 {input.lines().map(|line| {let mut nums = line.split('x').map(|s| s.parse::<i32>().unwrap());// dimensionslet l = nums.next().unwrap();let w = nums.next().unwrap();let h = nums.next().unwrap();// sideslet lw = l * w;let wh = w * h;let lh = l * h;let smallest = lw.min(wh).min(lh);2 * lw + 2 * wh + 2 * lh + smallest}).sum()}
Part 2
The elves need to order ribbon too.
The required amount of ribbon for one present:
- Enough to wrap around the smallest perimeter
- Some extra for a bow: the cubic feet of volume for the present
The question asks how many feet of ribbon is needed to wrap all presents with a bow.
A minor change from part 1: I sorted the dimensions to be able to use the 2 smallest ones.
Main code for part 2
pub fn part_2(input: &str) -> i32 {input.lines().map(|line| {let mut nums = line.split('x').map(|s| s.parse::<i32>().unwrap()).sorted_unstable();// dimensions from small to biglet dim1 = nums.next().unwrap();let dim2 = nums.next().unwrap();let dim3 = nums.next().unwrap();2 * dim1 + 2 * dim2 + dim1 * dim2 * dim3}).sum()}
Final code
1use itertools::Itertools;23pub fn part_1(input: &str) -> i32 {4 input5 .lines()6 .map(|line| {7 let mut nums = line.split('x').map(|s| s.parse::<i32>().unwrap());8 // dimensions9 let l = nums.next().unwrap();10 let w = nums.next().unwrap();11 let h = nums.next().unwrap();12 // sides13 let lw = l * w;14 let wh = w * h;15 let lh = l * h;16 let smallest = lw.min(wh).min(lh);1718 2 * lw + 2 * wh + 2 * lh + smallest19 })20 .sum()21}2223pub fn part_2(input: &str) -> i32 {24 input25 .lines()26 .map(|line| {27 let mut nums = line.split('x').map(|s| s.parse::<i32>().unwrap()).sorted_unstable();28 // dimensions from small to big29 let dim1 = nums.next().unwrap();30 let dim2 = nums.next().unwrap();31 let dim3 = nums.next().unwrap();3233 2 * dim1 + 2 * dim2 + dim1 * dim2 * dim334 })35 .sum()36}
Series navigation for: Advent of Code 2015