DEVELOPMENT ENVIRONMENT

~liljamo/aoc2024

ref: e7712b210e6f57dad3ec2170862538c4ee3b118e aoc2024/src/day2/mod.rs -rw-r--r-- 2.7 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
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
101
102
103
104
105
106
107
108
109
110
use std::{
    fs::File,
    io::{BufRead, BufReader},
    path::Path,
};

pub fn solve(input: &Path) -> anyhow::Result<()> {
    println!("part one: {}", part_one(input)?);
    println!("part two: {}", part_two(input)?);

    Ok(())
}

fn part_one(input: &Path) -> anyhow::Result<i32> {
    let reader = BufReader::new(File::open(input)?);

    let mut reports: Vec<Vec<i32>> = vec![];
    for line in reader.lines() {
        reports.push(
            line?
                .split_whitespace()
                .map(|n| -> i32 { n.parse().unwrap() })
                .collect(),
        );
    }
    let mut safe = 0;
    for r in reports {
        let mut s = 0;
        let mut safes: i32 = 0;
        let mut last_level = None;
        for current in &r {
            if last_level.is_none() {
                last_level = Some(current);
                continue;
            }
            let last = last_level.unwrap();
            let diff = (current - last).abs();
            if diff < 4 && diff > 0 {
                match current.cmp(last) {
                    std::cmp::Ordering::Greater => safes += 1,
                    std::cmp::Ordering::Less => safes -= 1,
                    _ => {}
                }
                if safes.abs() == (r.len() as i32) - 1 {
                    s = 1;
                }
            }
            last_level = Some(current);
        }
        safe += s;
    }

    Ok(safe)
}

fn part_two(input: &Path) -> anyhow::Result<i32> {
    let reader = BufReader::new(File::open(input)?);

    let mut reports: Vec<Vec<i32>> = vec![];
    for line in reader.lines() {
        reports.push(
            line?
                .split_whitespace()
                .map(|n| -> i32 { n.parse().unwrap() })
                .collect(),
        );
    }
    let mut safe = 0;
    for r in reports {
        let mut s = 0;
        for i in 0..r.len() {
            let mut a = r.clone();
            a.remove(i);
            if is_safe_two(a) == 1 {
                s = 1;
                break;
            }
        }

        safe += s;
    }

    Ok(safe)
}

fn is_safe_two(r: Vec<i32>) -> i32 {
    let mut s = 0;
    let mut safes: i32 = 0;
    let mut last_level = None;
    for current in &r {
        if last_level.is_none() {
            last_level = Some(current);
            continue;
        }
        let last = last_level.unwrap();
        let diff = (current - last).abs();
        if diff < 4 && diff > 0 {
            match current.cmp(last) {
                std::cmp::Ordering::Greater => safes += 1,
                std::cmp::Ordering::Less => safes -= 1,
                _ => {}
            }
            if safes.abs() == (r.len() as i32) - 1 {
                s = 1;
            }
        }
        last_level = Some(current);
    }
    s
}