summaryrefslogtreecommitdiff
path: root/2022/day6/src/lib.rs
blob: 76acb533ed51b1301d2b1946c889dcb5c6fc03bb (plain)
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
pub fn part(input: &str, m: usize) -> usize {
    let n = input.len();

    for i in 0..n {
        let s = &input[i..i + m];
        let mut check = false;
        if s.trim().len() < m {
            break;
        }

        'o: for j in 0..m {
            for k in 0..m {
                if j == k {
                    continue;
                }

                if s.chars().nth(j) == s.chars().nth(k) {
                    check = true;
                    break 'o;
                }
            }
        }

        if !check {
            return i + m;
        }
    }

    0
}

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

    #[test]
    fn test_example() {
        let input = include_str!("../example.txt");
        let result = part(input, 4);

        assert_eq!(result, 5);
    }

    #[test]
    fn test_input() {
        let input = include_str!("../input.txt");
        let result = part(input, 4);

        assert_eq!(result, 1779);
    }

    #[test]
    fn test_example2() {
        let input = include_str!("../example.txt");
        let result = part(input, 14);

        assert_eq!(result, 23);
    }

    #[test]
    fn test_input2() {
        let input = include_str!("../input.txt");
        let result = part(input, 14);

        assert_eq!(result, 23);
    }
}