NickyMeulemanNime
Metadata
  • Date

  • By

    • Nicky Meuleman
  • Tagged

  • 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

  • Newer post

    Advent of Code 2015 Day 4

Table of contents
  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

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:

input.txt
29x13x26
11x11x14
27x2x5

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);
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
1use itertools::Itertools;
2
3pub fn part_1(input: &str) -> i32 {
4 input
5 .lines()
6 .map(|line| {
7 let mut nums = line.split('x').map(|s| s.parse::<i32>().unwrap());
8 // dimensions
9 let l = nums.next().unwrap();
10 let w = nums.next().unwrap();
11 let h = nums.next().unwrap();
12 // sides
13 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 + smallest
19 })
20 .sum()
21}
22
23pub fn part_2(input: &str) -> i32 {
24 input
25 .lines()
26 .map(|line| {
27 let mut nums = line.split('x').map(|s| s.parse::<i32>().unwrap()).sorted_unstable();
28 // dimensions from small to big
29 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 * dim3
34 })
35 .sum()
36}

Series navigation for: Advent of Code 2015

1. Advent of Code 2015 Day 1

Designed and developed by Nicky Meuleman

Built with Gatsby. Hosted on Netlify.