use std::{
fs::File,
io::{BufReader, Read},
path::Path,
};
mod part1;
mod part2;
pub fn solve(input: &Path) -> anyhow::Result<()> {
println!("part one orig: {}", part_one_orig(input)?);
println!("part one: {}", part1::part_one(input)?);
println!("part two: {}", part2::part_two(input)?);
Ok(())
}
fn part_one_orig(input: &Path) -> anyhow::Result<i32> {
let mut reader = BufReader::new(File::open(input)?);
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
let mut answer = 0;
while let Some(mul_beg) = buf.find("mul(") {
if let Some(mul_end) = buf[mul_beg..].find(")").map(|i| i + mul_beg) {
let mul_input = &buf.clone()[mul_beg + 4..mul_end];
let mut mul_inputs = mul_input.split(",");
let mut err = false;
let first: i32 = match mul_inputs.next().unwrap_or_default().parse() {
Ok(f) => f,
Err(_) => {
err = true;
0
}
};
let second: i32 = match mul_inputs.next().unwrap_or_default().parse() {
Ok(f) => f,
Err(_) => {
err = true;
0
}
};
if err {
buf.replace_range(mul_beg..mul_beg + 4, "");
} else {
answer += first * second;
buf.replace_range(mul_beg..mul_end + 1, "");
}
}
}
Ok(answer)
}