DEVELOPMENT ENVIRONMENT

~liljamo/aoc2025

ref: 7e407c3dd7d76869cca56c095caad7d8207feb40 aoc2025/crates/day1/src/main.rs -rw-r--r-- 1.2 KiB
7e407c3dJonni Liljamo feat: init with day1 part one 3 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
#![no_std]

use common::{print, read};

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

fn one(input: &str) -> u16 {
    let mut zeros: u16 = 0;
    let mut pos: i16 = 50;
    for ins in input.split("\n") {
        // Last entry is all unused bytes.
        if ins.len() > 10 {
            continue;
        }

        let (direction, amount_s) = ins.split_at(1);
        let amount = amount_s.parse::<i16>().unwrap();

        match direction {
            "R" => {
                pos += amount;
                while pos > 99 {
                    pos -= 100;
                }
            }
            "L" => {
                pos -= amount;
                while pos < 0 {
                    pos += 100;
                }
            }
            _ => unreachable!(),
        }

        if pos == 0 {
            zeros += 1;
        }
    }

    zeros
}

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

    const EXAMPLE_INPUT: &str = r#"L68
L30
R48
L5
R60
L55
L1
L99
R14
L82"#;

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