DEVELOPMENT ENVIRONMENT

~liljamo/aoc2024

ref: 26815a6480e3fed9b1b8d27f514afa69a6f57c2e aoc2024/src/day10/mod.rs -rw-r--r-- 3.6 KiB
26815a64Jonni Liljamo feat: day11 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::{
    collections::HashMap,
    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 navigate_one(
    map: &[Vec<u8>],
    h: u8,
    p: (&usize, &usize),
    nines: &mut HashMap<(usize, usize), ()>,
) {
    let mut around_us = vec![];
    if *p.0 > 0 {
        let (y, x) = (p.0 - 1, *p.1);
        around_us.push((map[y][x], y, x));
    }
    if let Some(row) = map.get(p.0 + 1) {
        let (y, x) = (p.0 + 1, *p.1);
        around_us.push((row[x], y, x));
    }
    if *p.1 > 0 {
        let (y, x) = (*p.0, p.1 - 1);
        around_us.push((map[y][x], y, x));
    }
    if let Some(height) = map[*p.0].get(p.1 + 1) {
        let (y, x) = (*p.0, p.1 + 1);
        around_us.push((*height, y, x));
    }

    for (n, y, x) in around_us {
        if h == 8 && n == 9 {
            nines.insert((y, x), ());
        } else if n == h + 1 {
            navigate_one(map, h + 1, (&y, &x), nines);
        }
    }
}

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

    let mut map: Vec<Vec<u8>> = vec![];
    for line in reader.lines() {
        map.push(
            line?
                .chars()
                .map(|c| -> u8 { c.to_string().parse().unwrap() })
                .collect(),
        );
    }
    let trailheads: Vec<(usize, usize)> = map
        .iter()
        .enumerate()
        .flat_map(|(y, row)| -> Vec<(usize, usize)> {
            row.iter()
                .enumerate()
                .filter_map(|(x, n)| if *n == 0 { Some((y, x)) } else { None })
                .collect()
        })
        .collect();

    let mut answer = 0;
    for (head_y, head_x) in &trailheads {
        let mut nines: HashMap<(usize, usize), ()> = HashMap::new();
        navigate_one(&map, 0, (head_y, head_x), &mut nines);
        answer += nines.len();
    }

    Ok(answer)
}

fn navigate_two(map: &[Vec<u8>], h: u8, p: (&usize, &usize)) -> i32 {
    let mut nines = 0;
    let mut around_us = vec![];
    if *p.0 > 0 {
        let (y, x) = (p.0 - 1, *p.1);
        around_us.push((map[y][x], y, x));
    }
    if let Some(row) = map.get(p.0 + 1) {
        let (y, x) = (p.0 + 1, *p.1);
        around_us.push((row[x], y, x));
    }
    if *p.1 > 0 {
        let (y, x) = (*p.0, p.1 - 1);
        around_us.push((map[y][x], y, x));
    }
    if let Some(height) = map[*p.0].get(p.1 + 1) {
        let (y, x) = (*p.0, p.1 + 1);
        around_us.push((*height, y, x));
    }

    for (n, y, x) in around_us {
        if h == 8 && n == 9 {
            nines += 1;
        } else if n == h + 1 {
            nines += navigate_two(map, h + 1, (&y, &x));
        }
    }
    nines
}

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

    let mut map: Vec<Vec<u8>> = vec![];
    for line in reader.lines() {
        map.push(
            line?
                .chars()
                .map(|c| -> u8 { c.to_string().parse().unwrap() })
                .collect(),
        );
    }
    let trailheads: Vec<(usize, usize)> = map
        .iter()
        .enumerate()
        .flat_map(|(y, row)| -> Vec<(usize, usize)> {
            row.iter()
                .enumerate()
                .filter_map(|(x, n)| if *n == 0 { Some((y, x)) } else { None })
                .collect()
        })
        .collect();

    let mut answer = 0;
    for (head_y, head_x) in &trailheads {
        answer += navigate_two(&map, 0, (head_y, head_x));
    }

    Ok(answer)
}