aoc_rs/
parsing.rs

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
use std::collections::HashSet;

use crate::{direction::DIRECTIONS_90_DEG, point::Point2};

pub fn find_character_first_vec_string(input: Vec<String>, target: char) -> (usize, usize) {
    input
        .iter()
        .enumerate()
        .filter_map(|(row_idx, row)| {
            row.chars().enumerate().find_map(|(col_idx, c)| {
                if c == target {
                    Some((col_idx, row_idx))
                } else {
                    None
                }
            })
        })
        .next()
        .unwrap()
}

pub fn find_character_all_occurences_vec_string(
    input: Vec<String>,
    target: char,
) -> Vec<(usize, usize)> {
    input
        .iter()
        .enumerate()
        .fold(Vec::new(), |mut vec, (row_idx, row)| {
            row.chars().enumerate().for_each(|(col_idx, c)| {
                if c == target {
                    vec.push((col_idx, row_idx))
                }
            });
            vec
        })
}

pub fn find_character_first_grid(input: Vec<Vec<char>>, target: char) -> (usize, usize) {
    input
        .iter()
        .enumerate()
        .filter_map(|(row_idx, row)| {
            row.iter().enumerate().find_map(|(col_idx, c)| {
                if *c == target {
                    Some((col_idx, row_idx))
                } else {
                    None
                }
            })
        })
        .next()
        .unwrap()
}

pub fn find_character_all_occurences_grid(
    input: Vec<Vec<char>>,
    target: char,
) -> Vec<(usize, usize)> {
    input
        .iter()
        .enumerate()
        .fold(Vec::new(), |mut vec, (row_idx, row)| {
            row.iter().enumerate().for_each(|(col_idx, c)| {
                if *c == target {
                    vec.push((col_idx, row_idx))
                }
            });
            vec
        })
}

pub fn find_number_first(input: Vec<Vec<usize>>, target: usize) -> (usize, usize) {
    input
        .iter()
        .enumerate()
        .filter_map(|(row_idx, row)| {
            row.iter().enumerate().find_map(|(col_idx, &n)| {
                if n == target {
                    Some((col_idx, row_idx))
                } else {
                    None
                }
            })
        })
        .next()
        .unwrap()
}

pub fn find_number_all_occurences(input: Vec<Vec<usize>>, target: usize) -> Vec<(usize, usize)> {
    input
        .iter()
        .enumerate()
        .fold(Vec::new(), |mut vec, (row_idx, row)| {
            row.iter().enumerate().for_each(|(col_idx, &n)| {
                if n == target {
                    vec.push((col_idx, row_idx))
                }
            });
            vec
        })
}

pub fn get_regions_from_grid(grid: &[Vec<char>]) -> Vec<(char, Vec<Point2>)> {
    let mut regions = Vec::new();
    let mut seen = HashSet::new();
    let dimensions = (grid[0].len() as isize, grid.len() as isize);
    for (y, r) in grid.iter().enumerate() {
        for (x, c) in r.iter().enumerate() {
            let char_point = Point2::new(x as isize, y as isize);
            if seen.contains(&char_point) {
                continue;
            }
            let mut elements = Vec::from([char_point]);
            seen.insert(char_point);
            let mut queue = Vec::from([char_point]);
            while let Some(point) = queue.pop() {
                for d in DIRECTIONS_90_DEG {
                    let next_pos =
                        Point2::new(point.x + d.to_vector2().x, point.y + d.to_vector2().y);
                    if next_pos.x >= 0
                        && next_pos.x < dimensions.0
                        && next_pos.y >= 0
                        && next_pos.y < dimensions.1
                        && grid[next_pos.y as usize][next_pos.x as usize] == *c
                        && !seen.contains(&next_pos)
                    {
                        seen.insert(next_pos);
                        queue.push(next_pos);
                        elements.push(next_pos);
                    }
                }
            }
            elements.sort_by_key(|p| (p.y, p.x));
            regions.push((*c, elements))
        }
    }
    regions
}