DEVELOPMENT ENVIRONMENT

~liljamo/aoc2024

ref: e7712b210e6f57dad3ec2170862538c4ee3b118e aoc2024/src/day3/mod.rs -rw-r--r-- 1.5 KiB
e7712b21Jonni Liljamo chore: clippy, because i can't just keep ignoring it a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)
}