Skip to content

Commit 5ed7005

Browse files
committed
Year 2015 Day 15
1 parent 4eb4575 commit 5ed7005

File tree

8 files changed

+86
-0
lines changed

8 files changed

+86
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,4 @@ pie
250250
| 12 | [JSAbacusFramework.io](https://adventofcode.com/2015/day/12) | [Source](src/year2015/day12.rs) | 80 |
251251
| 13 | [Knights of the Dinner Table](https://adventofcode.com/2015/day/13) | [Source](src/year2015/day13.rs) | 39 |
252252
| 14 | [Reindeer Olympics](https://adventofcode.com/2015/day/14) | [Source](src/year2015/day14.rs) | 28 |
253+
| 15 | [Science for Hungry People](https://adventofcode.com/2015/day/15) | [Source](src/year2015/day15.rs) | 41 |

benches/benchmark.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ mod year2015 {
5050
benchmark!(year2015, day12);
5151
benchmark!(year2015, day13);
5252
benchmark!(year2015, day14);
53+
benchmark!(year2015, day15);
5354
}
5455

5556
mod year2019 {

input/year2015/day15.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Sugar: capacity 3, durability 0, flavor 0, texture -3, calories 2
2+
Sprinkles: capacity -3, durability 3, flavor 0, texture 0, calories 9
3+
Candy: capacity -1, durability 0, flavor 4, texture 0, calories 1
4+
Chocolate: capacity 0, durability 0, flavor -2, texture 2, calories 8

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ pub mod year2015 {
174174
pub mod day12;
175175
pub mod day13;
176176
pub mod day14;
177+
pub mod day15;
177178
}
178179

179180
/// # Rescue Santa from deep space with a solar system adventure.

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ fn all_solutions() -> Vec<Solution> {
9090
solution!(year2015, day12),
9191
solution!(year2015, day13),
9292
solution!(year2015, day14),
93+
solution!(year2015, day15),
9394
// 2019
9495
solution!(year2019, day01),
9596
solution!(year2019, day02),

src/year2015/day15.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//! # Science for Hungry People
2+
//!
3+
//! Brute force solution trying every possible combination of ingredients. The loop conditions are
4+
//! calculated so that the ingredients always sum to 100. Solves part one and two simultaneously.
5+
//!
6+
//! As an optimization we check halfway through the loops to see if any ingredient will never be
7+
//! be greater than zero to skip large numbers of combinations.
8+
use crate::util::iter::*;
9+
use crate::util::parse::*;
10+
use std::array::from_fn;
11+
12+
type Ingredient = [i32; 5];
13+
type Input = (i32, i32);
14+
15+
pub fn parse(input: &str) -> Input {
16+
let recipe: Vec<Ingredient> = input.iter_signed().chunk::<5>().collect();
17+
let mut part_one = 0;
18+
let mut part_two = 0;
19+
20+
for a in 0..101 {
21+
let first: Ingredient = from_fn(|i| a * recipe[0][i]);
22+
23+
for b in 0..(101 - a) {
24+
let second: Ingredient = from_fn(|i| first[i] + b * recipe[1][i]);
25+
26+
// Check if any ingredient can never be greater than zero.
27+
let check: Ingredient =
28+
from_fn(|i| second[i] + recipe[2][i].max(recipe[3][i]) * (100 - a - b));
29+
if check.iter().any(|&n| n <= 0) {
30+
continue;
31+
}
32+
33+
for c in 0..(101 - a - b) {
34+
let d = 100 - a - b - c;
35+
let third: Ingredient = from_fn(|i| second[i] + c * recipe[2][i]);
36+
let fourth: Ingredient = from_fn(|i| third[i] + d * recipe[3][i]);
37+
38+
let score = fourth.iter().take(4).map(|&n| n.max(0)).product();
39+
let calories = fourth[4];
40+
41+
part_one = part_one.max(score);
42+
if calories == 500 {
43+
part_two = part_two.max(score);
44+
}
45+
}
46+
}
47+
}
48+
49+
(part_one, part_two)
50+
}
51+
52+
pub fn part1(input: &Input) -> i32 {
53+
input.0
54+
}
55+
56+
pub fn part2(input: &Input) -> i32 {
57+
input.1
58+
}

tests/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod year2015 {
4343
mod day12_test;
4444
mod day13_test;
4545
mod day14_test;
46+
mod day15_test;
4647
}
4748

4849
mod year2019 {

tests/year2015/day15_test.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use aoc::year2015::day15::*;
2+
3+
const EXAMPLE: &str = "\
4+
Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
5+
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3
6+
Filler 1: capacity -200, durability -300, flavor -600, texture -300, calories 0
7+
Filler 2: capacity -200, durability -300, flavor -600, texture -300, calories 0";
8+
9+
#[test]
10+
fn part1_test() {
11+
let input = parse(EXAMPLE);
12+
assert_eq!(part1(&input), 62842880);
13+
}
14+
15+
#[test]
16+
fn part2_test() {
17+
let input = parse(EXAMPLE);
18+
assert_eq!(part2(&input), 57600000);
19+
}

0 commit comments

Comments
 (0)