NickyMeulemanNime

• ### By

• Nicky Meuleman

• ### Part of series

• 1. Advent of Code 2015 Day 1
• 2. Advent of Code 2015 Day 2
• 3. Advent of Code 2015 Day 3
• 4. Advent of Code 2015 Day 4
• ### Older post

Advent of Code 2015 Day 3

Advent of Code 2015 Day 4

1. Day 2: I Was Told There Would Be No Math
2. Part 1
1. Main code for part 1
3. Part 2
1. Main code for part 2
4. Final code

# Advent of Code 2015 Day 2

## Day 2: I Was Told There Would Be No Math

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:

input.txt
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

day_02.rs
pub fn part_1(input: &str) -> i32 {    input        .lines()        .map(|line| {            let mut nums = line.split('x').map(|s| s.parse::<i32>().unwrap());            // dimensions            let l = nums.next().unwrap();            let w = nums.next().unwrap();            let h = nums.next().unwrap();            // sides            let lw = l * w;            let wh = w * h;            let lh = l * h;            let smallest = lw.min(wh).min(lh);.css-13aqjzy{display:inline-block;}
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

day_01.rs
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 big        let dim1 = nums.next().unwrap();        let dim2 = nums.next().unwrap();        let dim3 = nums.next().unwrap();            2 * dim1 + 2 * dim2 + dim1 * dim2 * dim3    })    .sum()}

## Final code

day_02.rs
.css-1mjim83{display:inline-block;width:2ch;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:0.3;margin-right:0.5rem;}1use itertools::Itertools;2
3pub 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);17
18            2 * lw + 2 * wh + 2 * lh + smallest19        })20        .sum()21}22
23pub 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();32    33        2 * dim1 + 2 * dim2 + dim1 * dim2 * dim334    })35    .sum()36}