-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3231160
commit 3160670
Showing
9 changed files
with
167 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
43 | ||
3 | ||
4 | ||
10 | ||
21 | ||
44 | ||
4 | ||
6 | ||
47 | ||
41 | ||
34 | ||
17 | ||
17 | ||
44 | ||
36 | ||
31 | ||
46 | ||
9 | ||
27 | ||
38 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const EGGNOG_LITERS: usize = 150; | ||
|
||
fn parse_file(input: String) -> Vec<usize> { | ||
let mut inputs: Vec<usize> = vec![]; | ||
for line in input.lines() { | ||
let Ok(value) = line.parse::<usize>() else { | ||
panic!("Invalid number: {}", line) | ||
}; | ||
inputs.push(value); | ||
} | ||
|
||
inputs | ||
} | ||
|
||
fn build_combinations( | ||
liters: usize, | ||
containers_left: Vec<usize>, | ||
containers_build: Vec<usize>, | ||
) -> Vec<Vec<usize>> { | ||
let mut solutions: Vec<Vec<usize>> = vec![]; | ||
let container_size: usize = containers_build.iter().sum::<usize>(); | ||
if container_size >= liters || containers_left.is_empty() { | ||
if container_size == liters { | ||
solutions.push(containers_build); | ||
} | ||
return solutions; | ||
} | ||
for idx in 0..containers_left.len() { | ||
let next_containers_left: Vec<usize> = containers_left[idx + 1..].to_owned(); | ||
let mut next_containers_build: Vec<usize> = containers_build.clone(); | ||
next_containers_build.push(containers_left[idx]); | ||
let mut new_solutions = build_combinations(liters, next_containers_left, next_containers_build); | ||
solutions.append(&mut new_solutions); | ||
} | ||
|
||
solutions | ||
} | ||
|
||
fn build_solutions(liters: usize, containers: &Vec<usize>) -> Vec<Vec<usize>> { | ||
let mut solutions: Vec<Vec<usize>> = vec![]; | ||
let mut start: usize = 0; | ||
loop { | ||
if containers[start..].iter().sum::<usize>() < liters { | ||
return solutions; | ||
} | ||
let containers_left: Vec<usize> = containers[start + 1..].to_owned(); | ||
let containers_build: Vec<usize> = vec![containers[start]]; | ||
let mut new_solutions = build_combinations(liters, containers_left, containers_build); | ||
solutions.append(&mut new_solutions); | ||
start += 1; | ||
} | ||
} | ||
|
||
pub fn day_17_v1(input: impl Into<String>) -> usize { | ||
let containers: Vec<usize> = parse_file(input.into()); | ||
let solutions = build_solutions(EGGNOG_LITERS, &containers); | ||
|
||
solutions.len() | ||
} | ||
|
||
pub fn day_17_v2(input: impl Into<String>) -> usize { | ||
let containers: Vec<usize> = parse_file(input.into()); | ||
let mut solutions = build_solutions(EGGNOG_LITERS, &containers); | ||
let Some(min_solution) = solutions.iter().map(|s| s.len()).min() else { | ||
panic!("No minimum size: {:?}", solutions) | ||
}; | ||
solutions.retain(|s| s.len() == min_solution); | ||
|
||
solutions.len() | ||
} | ||
|
||
solvable!(day_17, day_17_v1, day_17_v2, usize); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn works_with_samples_v1() { | ||
let sample: Vec<usize> = vec![20, 15, 10, 5, 5]; | ||
let solutions = build_solutions(25, &sample); | ||
assert_eq!(solutions.len(), 4); | ||
} | ||
|
||
#[test] | ||
fn works_with_samples_v2() { | ||
let sample: Vec<usize> = vec![20, 15, 10, 5, 5]; | ||
let mut solutions = build_solutions(25, &sample); | ||
let Some(min_solution) = solutions.iter().map(|s| s.len()).min() else { | ||
panic!("No minimum size: {:?}", solutions) | ||
}; | ||
solutions.retain(|s| s.len() == min_solution); | ||
assert_eq!(solutions.len(), 3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters