DEVELOPMENT ENVIRONMENT

~liljamo/aoc2025

aoc2025/crates/day2/src/main.rs -rw-r--r-- 2.9 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#![no_std]

use common::{format_str, 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);
    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(",")
        .map(|mut range| {
            range = range.trim();
            let (first, last) = range
                .split_once("-")
                .map(|(first, last)| (first.parse::<u64>().unwrap(), last.parse::<u64>().unwrap()))
                .unwrap();

            let mut ret = 0;
            for i in first..=last {
                let mut buf = [0; 20];
                let mut i_s = format_str(&mut buf, format_args!("{}", i));
                i_s = i_s.trim_end_matches(char::MIN);

                for j in 0..i_s.len() {
                    let mut amount = 0;
                    let a = i_s
                        .matches(&i_s[0..j])
                        .map(|a| {
                            amount += 1;
                            a.len()
                        })
                        .sum::<usize>();
                    // remove amount check to do those that repeat more times
                    if a == i_s.len() && amount == 2 {
                        //print!(30, "found {}\n", i);
                        ret += i as usize;
                        break;
                    }
                }
            }

            ret
        })
        .sum::<usize>()
}

fn two(input: &str) -> usize {
    input
        .split(",")
        .map(|mut range| {
            range = range.trim();
            let (first, last) = range
                .split_once("-")
                .map(|(first, last)| (first.parse::<u64>().unwrap(), last.parse::<u64>().unwrap()))
                .unwrap();

            let mut ret = 0;
            for i in first..=last {
                let mut buf = [0; 20];
                let mut i_s = format_str(&mut buf, format_args!("{}", i));
                i_s = i_s.trim_end_matches(char::MIN);

                for j in 0..i_s.len() {
                    let a = i_s.matches(&i_s[0..j]).map(|a| a.len()).sum::<usize>();
                    if a == i_s.len() {
                        ret += i as usize;
                        break;
                    }
                }
            }

            ret
        })
        .sum::<usize>()
}

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

    const EXAMPLE_INPUT: &str = "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124";

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

    #[test]
    fn one_a() {
        assert_eq!(one("3131219357-3131417388\n"), 6262562625);
    }
}