1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! # [Day 2: I Was Told There Would Be No Math](https://adventofcode.com/2015/day/2)
//!
//! The elves are running low on wrapping paper, and so they need to submit an order
//! for more.
//! They have a list of the dimensions (length `l`, width `w`, and height `h`) of
//! each present,
//! and only want to order exactly as much as they need.
//!
//! Fortunately, every present is a box (a perfect
//! [right rectangular prism](https://en.wikipedia.org/wiki/Cuboid#Rectangular_cuboid)),
//! which makes calculating the required wrapping paper for each gift a
//! little easier:
//! find the surface area of the box, which is `2*l*w + 2*w*h + 2*h*l`.
//! The elves also need a little extra paper for each present: the area of the
//! smallest side.
//!
//! For example:
//!
//! -   A present with dimensions `2x3x4` requires `2*6 + 2*12 + 2*8 = 52` square
//!     feet of wrapping paper plus `6` square feet of slack, for a total of `58`
//!     square feet.
//! -   A present with dimensions `1x1x10` requires `2*1 + 2*10 + 2*10 = 42` square
//!     feet of wrapping paper plus `1` square foot of slack, for a total of `43`
//!     square feet.
//!
//! **All numbers in the elves' list are in feet. How many total square feet of
//! wrapping paper should they order?**
//!
//! # Part Two
//!
//! The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry
//! about the length they need to order, which they would again like to be exact.
//!
//! The ribbon required to wrap a present is the shortest distance around its sides, or the smallest
//! perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet
//! of ribbon required for the perfect bow is equal to the cubic feet of volume of the present.
//! Don't ask how they tie the bow, though; they'll never tell.
//!
//! For example:
//!
//! - A present with dimensions `2x3x4` requires `2+2+3+3 = 10` feet of ribbon to wrap the
//! present plus `2*3*4 = 24` feet of ribbon for the bow, for a total of `34` feet.
//! - A present with dimensions `1x1x10` requires `1+1+1+1 = 4` feet of ribbon to wrap the present
//! plus `1*1*10 = 10` feet of ribbon for the bow, for a total of `14` feet.
//!
//! **How many total feet of ribbon should they order?**

#[aoc_generator(day2)]
fn parse_input(input: &str) -> Result<Vec<Present>, std::num::ParseIntError> {
    input
        .lines()
        .map(|l| {
            // l = 2x3x4
            l.split('x')
                .map(|f| f.parse::<u32>())
                .collect::<Result<Vec<u32>, std::num::ParseIntError>>()
                .map(|mut p| {
                    p.sort_unstable();
                    Present {
                        l: p[0],
                        w: p[1],
                        h: p[2],
                    }
                })
        })
        .collect()
}

/// Part 1: How many total square feet of wrapping paper should they order?
#[aoc(day2, part1)]
fn part1(presents: &[Present]) -> u32 {
    presents
        .iter()
        .map(|present| present.paper_required())
        .sum()
}

/// Part 2: How many total feet of ribbon should they order?
#[aoc(day2, part2)]
fn part2(presents: &[Present]) -> u32 {
    presents
        .iter()
        .map(|present| present.ribbon_required())
        .sum()
}

struct Present {
    l: u32,
    w: u32,
    h: u32,
}

impl Present {
    fn paper_required(&self) -> u32 {
        3 * self.l * self.w + 2 * self.w * self.h + 2 * self.h * self.l
    }
    fn ribbon_required(&self) -> u32 {
        2 * (self.l + self.w) + (self.l * self.w) * self.h
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn part1_examples() {
        let present234 = Present { l: 2, w: 3, h: 4 };
        /* A present with dimensions `2x3x4` requires `2*6 + 2*12 + 2*8 = 52` square
        feet of wrapping paper plus `6` square feet of slack, for a total of `58`
        square feet. */
        assert_eq!(present234.paper_required(), 58);

        /* A present with dimensions `1x1x10` requires `2*1 + 2*10 + 2*10 = 42` square
        feet of wrapping paper plus `1` square foot of slack, for a total of `43`
        square feet. */
        let present1110 = Present { l: 1, w: 1, h: 10 };
        assert_eq!(present1110.paper_required(), 43);
    }

    #[test]
    fn part2_examples() {
        let present234 = Present { l: 2, w: 3, h: 4 };
        /* A present with dimensions `2x3x4` requires `2+2+3+3 = 10` feet of ribbon to wrap the
        present plus `2*3*4 = 24` feet of ribbon for the bow, for a total of `34` feet. */
        assert_eq!(present234.ribbon_required(), 34);

        /* A present with dimensions `1x1x10` requires `1+1+1+1 = 4` feet of ribbon to wrap the present
        plus `1*1*10 = 10` feet of ribbon for the bow, for a total of `14` feet. */
        let present1110 = Present { l: 1, w: 1, h: 10 };
        assert_eq!(present1110.ribbon_required(), 14);
    }
}