DEVELOPMENT ENVIRONMENT

~liljamo/aoc2025

ref: 44396ade0adbb061dc948f1eb7be84149f6ab230 aoc2025/crates/day3/src/main.rs -rw-r--r-- 1.6 KiB
44396adeJonni Liljamo feat: day3 part one 2 days 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
56
57
58
59
60
61
62
63
64
65
#![no_std]

use core::fmt::Write;

use common::{print, read};

fn main() {
    let buf = [0u8; 65000];
    read(buf.as_ptr(), buf.len());
    let mut input = str::from_utf8(&buf).unwrap();
    input = input.trim_end_matches(char::MIN);
    input = input.trim();
    let result_one = one(input);
    let result_two = two(input);
    print!(64, "one: {}\ntwo: {}\n", result_one, result_two);
}

fn one(input: &str) -> usize {
    input
        .split("\n")
        .map(|bank| {
            let mut first = char::MIN;
            let mut first_index = 0;
            let mut second = char::MIN;
            let mut second_index = 0;

            for (i, c) in bank.chars().enumerate() {
                if c > first && i != bank.len() - 1 {
                    first = c;
                    first_index = i;

                    second = char::MIN;
                    second_index = 0;
                } else if c > second && i > first_index {
                    second = c;
                    second_index = i;
                }
            }

            let a = &bank[first_index..first_index + 1];
            let b = &bank[second_index..second_index + 1];

            let mut buf = [0; 2];
            let c = common::format_str(&mut buf, format_args!("{}{}", a, b));

            c.parse::<usize>().unwrap()
        })
        .sum::<usize>()
}

#[cfg(test)]
mod test {
    use super::*;

    const EXAMPLE_INPUT: &str = r#"987654321111111
811111111111119
234234234234278
818181911112111"#;

    #[test]
    fn example() {
        assert_eq!(one(EXAMPLE_INPUT), 357);
        assert_eq!(two(EXAMPLE_INPUT), 3121910778619);
    }
}