Skip to content

Instantly share code, notes, and snippets.

@jameshulse
Created December 2, 2021 06:23
Show Gist options
  • Save jameshulse/24feefb239b7c3de369c11019282f627 to your computer and use it in GitHub Desktop.
Save jameshulse/24feefb239b7c3de369c11019282f627 to your computer and use it in GitHub Desktop.
AOC 2021 day1
use itertools::Itertools;
use std::fs;
fn main() {
let lines = fs::read_to_string("input.txt").expect("Couldn't read input file.");
let values: Vec<u32> = lines
.lines()
.map(|val| val.parse::<u32>().expect("Invalid value in input file!"))
.collect();
let result = find_matching_combinations(&values, 3);
match result {
Some(val) => {
println!("Found the combination: {:?}", val);
println!("The result is: {:?}", val.iter().product::<u32>());
}
None => println!("Couldn't find matching combination"),
}
}
fn find_matching_combinations(input: &Vec<u32>, combo_size: usize) -> Option<Vec<u32>> {
return input
.iter()
.cloned()
.combinations(combo_size)
.find(|x| x.iter().cloned().sum::<u32>() == 2020)
.clone();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_find_two_combos() {
let values = vec![1721, 979, 366, 299, 675, 1456];
let result = find_matching_combinations(&values, 2);
assert_eq!(result.unwrap(), vec![1721, 299])
}
#[test]
fn test_find_three_combos() {
let values = vec![1721, 979, 366, 299, 675, 1456];
let result = find_matching_combinations(&values, 3);
assert_eq!(result.unwrap(), vec![979, 366, 675]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment