use std::error::Error;
use std::path::Path;
-fn possible_values(operands: &[u64]) -> Vec<u64> {
+fn solve(input: &str, with_concat: bool) -> u64 {
+ input.lines()
+ .map(|l| {
+ let (tot, ops) = l.split_once(":").unwrap();
+ let ops = ops.trim().split(" ")
+ .map(|v| v.parse::<u64>().unwrap())
+ .collect::<Vec<u64>>();
+ (tot.parse::<u64>().unwrap(), ops)
+ })
+ .map(|(tot, ops)| {
+ let values = possible_values(&ops, with_concat);
+ if values.contains(&tot) {
+ tot
+ }
+ else {
+ 0
+ }
+ })
+ .sum()
+}
+
+fn possible_values(operands: &[u64], with_concat: bool) -> Vec<u64> {
let mut res: Vec<u64> = Vec::new();
for o in operands {
if res.is_empty() {
for t in tmp {
res.push(t + o);
res.push(t * o);
+ if with_concat {
+ let value = t.to_string() + &o.to_string();
+ res.push(value.parse::<u64>().unwrap());
+ }
}
}
}
fn run_part1(input: &str) -> Result<u64, Box<dyn Error>> {
println!("Running {} - part 1", get_day());
- let res = input.lines()
- .map(|l| {
- let (tot, ops) = l.split_once(":").unwrap();
- let ops = ops.trim().split(" ")
- .map(|v| v.parse::<u64>().unwrap())
- .collect::<Vec<u64>>();
- (tot.parse::<u64>().unwrap(), ops)
- })
- .map(|(tot, ops)| {
- let values = possible_values(&ops);
- if values.contains(&tot) {
- tot
- }
- else {
- 0
- }
- })
- .sum();
-
- Ok(res)
+ Ok(solve(input, false))
}
fn run_part2(input: &str) -> Result<u64, Box<dyn Error>> {
println!("Running {} - part 2", get_day());
- Ok(0)
+ Ok(solve(input, true))
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
#[test]
fn test_part2() {
- assert_eq!(0, run_part2(TEXT_INPUT).unwrap());
+ assert_eq!(11387, run_part2(TEXT_INPUT).unwrap());
}
}