#[path = "../lib.rs"] mod lib; fn concat(a: u64, b: u64) -> u64 { let mut tmp = b; let mut result = a; while tmp > 0 { tmp /= 10; result *= 10; } return result + b; } fn check(numbers: &Vec, value: u64, part2: bool) -> bool { let mut a = vec![numbers[0]]; for x in numbers.iter().skip(1) { let mut b = vec![]; for old in a.iter() { let v1 = old + x; if v1 <= value { b.push(v1); } let v2 = old * x; if v2 <= value { b.push(v2); } if part2 { let v3 = concat(*old, *x); if v3 <= value { b.push(v3); } } } a = b; } return a.iter().any(|v| *v == value); } fn main() { let mut sum1 = 0; let mut sum2 = 0; for line in lib::iter_input() { let (a, b) = line.split_once(": ").unwrap(); let value = a.parse::().unwrap(); let numbers = b.split(' ').map(|s| s.parse::().unwrap()).collect(); if check(&numbers, value, false) { sum1 += value; } if check(&numbers, value, true) { sum2 += value; } } println!("part1: {}", sum1); println!("part2: {}", sum2); }