Day 2 - Advent of Code 2015
Mix.install([:kino, :benchee])
:ok
Links
Prompt
--- Day 2: I Was Told There Would Be No Math ---
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), 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
2x3x4requires2*6 + 2*12 + 2*8 = 52square feet of wrapping paper plus6square feet of slack, for a total of58square feet. - A present with dimensions
1x1x10requires2*1 + 2*10 + 2*10 = 42square feet of wrapping paper plus1square foot of slack, for a total of43square feet.
All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order?
To begin, get your puzzle input.
--- 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
2x3x4requires2+2+3+3 = 10feet of ribbon to wrap the present plus2*3*4 = 24feet of ribbon for the bow, for a total of34feet. - A present with dimensions
1x1x10requires1+1+1+1 = 4feet of ribbon to wrap the present plus1*1*10 = 10feet of ribbon for the bow, for a total of14feet.
How many total feet of ribbon should they order?
Although it hasn't changed, you can still get your puzzle input.
Input
input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"20x3x11\n15x27x5\n6x29x7\n30x15x9\n19x29x21\n10x4x15\n1x26x4\n1x5x18\n10x15x23\n10x14x20\n3x5x18\n29x23x30\n7x4x10\n22x24x29\n30x1x2\n19x2x5\n11x9x22\n23x15x10\n11x11x10\n30x28x5\n22x5x4\n6x26x20\n16x12x30\n10x20x5\n25x14x24\n16x17x22\n11x28x26\n1x11x10\n1x24x15\n13x17x21\n30x3x13\n20x25x17\n22x12x5\n22x20x24\n9x2x14\n6x18x8\n27x28x24\n11x17x1\n1x4x12\n5x20x13\n24x23x23\n22x1x25\n18x19x5\n5x23x13\n8x16x4\n20x21x9\n1x7x11\n8x30x17\n3x30x9\n6x16x18\n22x25x27\n9x20x26\n16x21x23\n5x24x17\n15x17x15\n26x15x10\n22x16x3\n20x24x24\n8x18x10\n23x19x16\n1x21x24\n23x23x9\n14x20x6\n25x5x5\n16x3x1\n29x29x20\n11x4x26\n10x23x24\n29x25x16\n27x27x22\n9x7x22\n6x21x18\n25x11x19\n14x13x3\n15x28x17\n14x3x12\n29x8x19\n30x14x20\n20x23x4\n8x16x5\n4x11x18\n20x8x24\n21x13x21\n14x26x29\n27x4x17\n27x4x25\n5x28x6\n23x24x11\n29x22x5\n30x20x6\n23x2x10\n11x4x7\n27x23x6\n10x20x19\n8x20x22\n5x29x22\n16x13x2\n2x11x14\n6x12x4\n3x13x6\n16x5x18\n25x3x28\n21x1x5\n20x16x19\n28x30x27\n26x7x18\n25x27x24\n11x19x7\n21x19x17\n2x12x27\n20x5x14\n8x5x8\n6x24x8\n7x28x20\n3x20x28\n5x20x30\n13x29x1\n26x29x5\n19x28x25\n5x19x11\n11x20x22\n4x23x1\n19x25x12\n3x10x6\n3x14x10\n28x16x12\n23x12x2\n23x12x19\n20x28x10\n9x10x25\n16x21x16\n1x18x20\n9x4x26\n3x25x8\n17x16x28\n9x28x16\n27x3x12\n17x24x12\n13x21x10\n7x17x13\n6x10x9\n7x29x25\n11x19x30\n1x24x5\n20x16x23\n24x28x21\n6x29x19\n25x2x19\n12x5x26\n25x29x12\n16x28x22\n26x26x15\n9x13x5\n10x29x7\n1x24x16\n22x2x2\n6x16x13\n3x12x28\n4x12x13\n14x27x21\n14x23x26\n7x5x18\n8x30x27\n15x9x18\n26x16x5\n3x29x17\n19x7x18\n16x18x1\n26x15x30\n24x30x21\n13x20x7\n4x12x10\n27x20x11\n28x29x21\n20x14x30\n28x12x3\n19x1x8\n4x8x6\n21x14x2\n27x19x21\n17x24x14\n15x18x11\n18x7x26\n25x28x29\n27x26x9\n18x12x17\n24x28x25\n13x24x14\n26x9x28\n9x3x30\n9x2x9\n8x1x29\n18x30x10\n18x14x5\n26x8x30\n12x1x1\n30x5x28\n26x17x21\n10x10x10\n20x7x27\n13x17x6\n21x13x17\n2x16x8\n7x9x9\n15x26x4\n11x28x25\n10x6x19\n21x6x29\n15x5x6\n28x9x16\n14x3x10\n12x29x5\n22x19x19\n25x15x22\n30x6x28\n11x23x13\n20x25x14\n26x1x13\n6x14x15\n16x25x17\n28x4x13\n10x24x25\n4x13x10\n9x15x16\n15x24x6\n22x9x19\n11x11x8\n4x19x12\n24x5x4\n27x12x13\n7x27x16\n2x6x9\n29x27x15\n18x26x23\n19x16x15\n14x5x25\n9x16x30\n4x6x4\n13x10x10\n1x8x29\n23x5x17\n19x20x20\n11x27x24\n27x15x5\n15x11x12\n21x11x3\n1x13x22\n17x8x8\n13x14x14\n17x22x7\n9x5x8\n2x6x3\n25x9x15\n11x8x13\n9x25x12\n3x16x12\n12x16x8\n16x24x17\n4x6x26\n22x29x11\n14x17x19\n28x2x27\n24x22x19\n22x20x30\n23x28x4\n16x12x14\n22x24x22\n29x1x28\n26x29x16\n3x25x30\n27x3x13\n22x24x26\n25x3x2\n7x24x2\n10x5x3\n28x8x29\n25x6x4\n12x17x14\n24x3x5\n23x27x7\n26x23x30\n11x10x19\n23x7x11\n26x14x15\n14x3x25\n12x24x14\n2x14x12\n9x12x16\n9x2x28\n3x8x2\n22x6x9\n2x30x2\n25x1x9\n20x11x2\n14x11x12\n7x14x12\n24x8x26\n13x21x23\n18x17x23\n13x6x17\n20x20x19\n13x17x29\n7x24x24\n23x8x6\n19x10x28\n3x8x21\n15x20x18\n11x27x1\n11x24x28\n13x20x11\n18x19x22\n27x22x12\n28x3x2\n13x4x29\n26x5x6\n14x29x25\n7x4x7\n5x17x7\n2x8x1\n22x30x24\n22x21x28\n1x28x13\n11x20x4\n25x29x19\n9x23x4\n30x6x11\n25x18x10\n28x10x24\n3x5x20\n19x28x10\n27x19x2\n26x20x4\n19x21x6\n2x12x30\n8x26x27\n11x27x10\n14x13x17\n4x3x21\n2x20x21\n22x30x3\n2x23x2\n3x16x12\n22x28x22\n3x23x29\n8x25x15\n9x30x4\n10x11x1\n24x8x20\n10x7x27\n7x22x4\n27x13x17\n5x28x5\n30x15x13\n10x8x17\n8x21x5\n8x17x26\n25x16x4\n9x7x25\n13x11x20\n6x30x9\n15x14x12\n30x1x23\n5x20x24\n22x7x6\n26x11x23\n29x7x5\n13x24x28\n22x20x10\n18x3x1\n15x19x23\n28x28x20\n7x26x2\n9x12x20\n15x4x6\n1x17x21\n3x22x17\n9x4x20\n25x19x5\n9x11x22\n14x1x17\n14x5x16\n30x5x18\n19x6x12\n28x16x22\n13x4x25\n29x23x18\n1x27x3\n12x14x4\n10x25x19\n15x19x30\n11x30x4\n11x22x26\n13x25x2\n17x13x27\n11x30x24\n15x1x14\n17x18x4\n26x11x3\n16x22x28\n13x20x9\n1x18x3\n25x11x12\n20x21x1\n22x27x4\n8x28x23\n7x13x27\n17x9x26\n27x27x20\n11x20x12\n26x21x11\n29x14x12\n27x25x1\n28x29x25\n21x23x28\n5x18x18\n19x5x4\n7x6x30\n27x8x11\n12x24x12\n16x25x22\n26x11x29\n25x22x17\n15x23x23\n17x9x6\n30x10x16\n21x3x5\n18x27x2\n28x21x14\n16x18x17\n4x18x2\n9x1x14\n9x1x9\n5x27x12\n8x16x30\n3x19x19\n16x26x24\n1x6x9\n15x14x3\n11x7x19\n8x19x3\n17x26x26\n6x18x11\n19x12x4\n29x20x16\n20x17x23\n6x6x5\n20x30x19\n18x25x18\n2x26x2\n3x1x1\n14x25x18\n3x1x6\n11x14x18\n17x23x27\n25x29x9\n6x25x20\n20x10x9\n17x5x18\n29x14x8\n14x25x26\n10x15x29\n23x19x11\n22x2x2\n4x5x5\n13x23x25\n19x13x19\n20x18x6\n30x7x28\n26x18x17\n29x18x10\n30x29x1\n12x26x24\n18x17x26\n29x28x15\n3x12x20\n24x10x8\n30x15x6\n28x23x15\n14x28x11\n10x27x19\n14x8x21\n24x1x23\n1x3x27\n6x15x6\n8x25x26\n13x10x25\n6x9x8\n10x29x29\n26x23x5\n14x24x1\n25x6x22\n17x11x18\n1x27x26\n18x25x23\n20x15x6\n2x21x28\n2x10x13\n12x25x14\n2x14x23\n30x5x23\n29x19x21\n29x10x25\n14x22x16\n17x11x26\n1" <> ...
Solution
defmodule Day02 do
defdelegate parse(input), to: __MODULE__.Input
def part1(input) do
input
|> parse()
|> Stream.map(&present_sqft/1)
|> Enum.sum()
end
def part2(input) do
input
|> parse()
|> Stream.map(&ribbon_ft/1)
|> Enum.sum()
end
def present_sqft([l, w, h]) do
[a, b, c] = sides = [l * w, l * h, w * h]
2 * a + 2 * b + 2 * c + Enum.min(sides)
end
def ribbon_ft(dimensions) do
dimensions
|> Enum.sort()
|> then(fn [x, y, _] ->
2 * x + 2 * y + Enum.product(dimensions)
end)
end
defmodule Input do
def parse(input) when is_binary(input) do
input
|> String.split("\n")
|> parse()
end
def parse(input) when is_list(input) do
Stream.map(input, &parse_line/1)
end
def parse_line(line) do
line
|> String.split("x")
|> Enum.map(&String.to_integer/1)
end
end
end
{:module, Day02, <<70, 79, 82, 49, 0, 0, 12, ...>>,
{:module, Day02.Input, <<70, 79, 82, ...>>, {:parse_line, 1}}}
How many total square feet of wrapping paper should they order?
Your puzzle answer was 1606483.
Day02.part1(input)
1606483
How many total feet of ribbon should they order??
Your puzzle answer was answer two.
Day02.part2(input)
3842356
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
Tests
ExUnit.start(auto_run: false)
defmodule Day02Test do
use ExUnit.Case, async: false
setup_all do
[
input: "2x3x4\n1x1x10"
]
end
describe "part1/1" do
test "returns expected value", %{input: input} do
assert Day02.present_sqft([2, 3, 4]) == 58
assert Day02.present_sqft([1, 1, 10]) == 43
assert Day02.part1(input) == 58 + 43
end
end
describe "part2/1" do
test "returns expected value", %{input: input} do
assert Day02.ribbon_ft([2, 3, 4]) == 34
assert Day02.ribbon_ft([1, 1, 10]) == 14
assert Day02.part2(input) == 34 + 14
end
end
end
ExUnit.run()
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures
Randomized with seed 558068
%{excluded: 0, failures: 0, skipped: 0, total: 2}