use anyhow::Result; use std::env::args; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::Path; const SUM: usize = 2020; fn read_input>(filename: T) -> Result> { let f = File::open(filename)?; let reader = BufReader::new(f); let values = reader.lines() .map(|v| v.unwrap().parse::().unwrap()).collect(); Ok(values) } fn part1(input: &[usize]) -> Option { for first in input { for second in input { if first + second == SUM { return Some(first * second); } } } None } fn part2(input: &[usize]) -> Option { for first in input { for second in input { for third in input { if first + second + third == SUM { return Some(first * second * third); } } } } None } fn main() { let ( do_part_1, do_part_2 ) = aoc::do_parts(); let filename = match args().nth(1) { Some(f) => f, None => { eprintln!("Missing input filename"); std::process::exit(1); }, }; match read_input(filename) { Ok(input) => { if do_part_1 { match part1(&input) { Some(solution) => println!("Part1, product found to be: {}", solution), None => { eprintln!("Part1, no solution found"); std::process::exit(1); } }; } if do_part_2 { match part2(&input) { Some(solution) => println!("Part2, product found to be: {}", solution), None => { eprintln!("Part2, no solution found"); std::process::exit(1); } }; } }, Err(err) => eprintln!("Could not read input: {}", err), } }