use std::fs; use itertools::Itertools; fn main() -> Result<(), Box> { let input = fs::read_to_string("input")?; let input = parse_number_list(input)?; let numbers = find_2020_numbers(input, 3)?; println!("{}", numbers.iter().product::()); Ok(()) } fn parse_number_list(input: String) -> Result, std::num::ParseIntError> { input.lines().map(|line| line.parse::()).collect() } fn find_2020_numbers(input: Vec, sum_number: usize) -> Result, String> { input .into_iter() .combinations(sum_number) .find(|i| i.iter().sum::() == 2020) .ok_or("Could not find 2020.".to_string()) } #[cfg(test)] mod tests { use super::*; use indoc::indoc; #[test] fn test_first_example() -> Result<(), Box> { let example = indoc!( " 1721 979 366 299 675 1456 " ); let example = parse_number_list(example.to_string())?; let numbers = find_2020_numbers(example, 2)?; assert_eq!(numbers.iter().product::(), 514579); Ok(()) } #[test] fn test_second_example() -> Result<(), Box> { let example = indoc!( " 1721 979 366 299 675 1456 " ); let example = parse_number_list(example.to_string())?; let numbers = find_2020_numbers(example, 3)?; assert_eq!(numbers.iter().to_owned().product::(), 241861950); Ok(()) } }