|
| 1 | +use aoc; |
| 2 | + |
| 3 | +fn solve(raw: String) -> () { |
| 4 | + let rows = raw.trim().split("\n").collect::<Vec<&str>>(); |
| 5 | + |
| 6 | + let height = rows.len(); |
| 7 | + let width = rows[0].len(); |
| 8 | + |
| 9 | + let grid = rows |
| 10 | + .iter() |
| 11 | + .flat_map(|row| { |
| 12 | + row.split("") |
| 13 | + .filter(|x| !x.is_empty()) |
| 14 | + .map(|c| parse_num::<usize>(c)) |
| 15 | + }) |
| 16 | + .collect::<Vec<usize>>(); |
| 17 | + |
| 18 | + let adj = calc_adj(height, width); |
| 19 | + |
| 20 | + let mut basins_origins: Vec<usize> = vec![]; |
| 21 | + |
| 22 | + for (index, value) in grid.iter().enumerate() { |
| 23 | + if adj[index].iter().map(|&m| grid[m]).all(|m| m > *value) { |
| 24 | + basins_origins.push(index); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + println!( |
| 29 | + "Part One: {}", |
| 30 | + basins_origins |
| 31 | + .iter() |
| 32 | + .map(|&origin| grid[origin] + 1) |
| 33 | + .sum::<usize>() |
| 34 | + ); |
| 35 | + |
| 36 | + let mut basin_sizes: Vec<usize> = vec![]; |
| 37 | + |
| 38 | + for origin in basins_origins.iter() { |
| 39 | + let distances = bfs(&adj, height, width, *origin, &grid); |
| 40 | + let size = distances.iter().filter(|&x| *x > 0).count() + 1; |
| 41 | + |
| 42 | + basin_sizes.push(size); |
| 43 | + } |
| 44 | + |
| 45 | + basin_sizes.sort_by(|a, b| b.cmp(&a)); |
| 46 | + |
| 47 | + println!( |
| 48 | + "Part Two: {}", |
| 49 | + basin_sizes[..3].iter().fold(1, |prev, curr| prev * curr) |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +fn main() { |
| 54 | + let input = aoc::get_input(2021, 9); |
| 55 | + |
| 56 | + // let input = std::fs::read_to_string("").expect("Error reading input"); |
| 57 | + |
| 58 | + solve(input); |
| 59 | +} |
| 60 | + |
| 61 | +// Utilities |
| 62 | +#[allow(dead_code)] |
| 63 | +fn normal(x: usize, y: usize, width: usize) -> usize { |
| 64 | + x + y * width |
| 65 | +} |
| 66 | + |
| 67 | +#[allow(dead_code)] |
| 68 | +fn rev_normal(norm: usize, width: usize) -> (usize, usize) { |
| 69 | + (norm % width, norm / width) |
| 70 | +} |
| 71 | + |
| 72 | +#[allow(dead_code)] |
| 73 | +fn parse_num<T: std::str::FromStr>(str: &str) -> T { |
| 74 | + match str.trim().parse::<T>() { |
| 75 | + Ok(n) => n, |
| 76 | + _ => panic!("Error parsing"), |
| 77 | + } |
| 78 | +} |
| 79 | +#[allow(dead_code)] |
| 80 | +fn to_int(bin: &str) -> u32 { |
| 81 | + match u32::from_str_radix(bin, 2) { |
| 82 | + Ok(n) => n, |
| 83 | + _ => panic!("Error parsing binary to integer"), |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[allow(dead_code)] |
| 88 | +fn string_vec<T: std::string::ToString>(vec: &Vec<T>, separator: &str) -> String { |
| 89 | + vec.iter() |
| 90 | + .map(|x| x.to_string()) |
| 91 | + .collect::<Vec<String>>() |
| 92 | + .join(separator) |
| 93 | +} |
| 94 | + |
| 95 | +type Adj = Vec<Vec<usize>>; |
| 96 | + |
| 97 | +fn norm(x: usize, y: usize, width: usize) -> usize { |
| 98 | + x + y * width |
| 99 | +} |
| 100 | + |
| 101 | +fn calc_adj(height: usize, width: usize) -> Adj { |
| 102 | + let mut adj = vec![vec![]; height * width]; |
| 103 | + |
| 104 | + for y in 0..height { |
| 105 | + for x in 0..width { |
| 106 | + let index = norm(x, y, width); |
| 107 | + if x > 0 { |
| 108 | + adj[index].push(norm(x - 1, y, width)); |
| 109 | + } |
| 110 | + if y + 1 < height { |
| 111 | + adj[index].push(norm(x, y + 1, width)); |
| 112 | + } |
| 113 | + if x + 1 < width { |
| 114 | + adj[index].push(norm(x + 1, y, width)); |
| 115 | + } |
| 116 | + if y > 0 { |
| 117 | + adj[index].push(norm(x, y - 1, width)); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + adj |
| 123 | +} |
| 124 | + |
| 125 | +fn bfs(adj: &Adj, height: usize, width: usize, root: usize, nodes: &Vec<usize>) -> Vec<usize> { |
| 126 | + let mut distances = vec![0; height * width]; |
| 127 | + |
| 128 | + let mut visited = vec![false; height * width]; |
| 129 | + |
| 130 | + use std::collections::VecDeque; |
| 131 | + let mut q = VecDeque::new(); |
| 132 | + |
| 133 | + visited[root] = true; |
| 134 | + |
| 135 | + q.push_back(root); |
| 136 | + // distance to self is zero |
| 137 | + distances[root] = 0; |
| 138 | + |
| 139 | + loop { |
| 140 | + let next = q.pop_front(); |
| 141 | + |
| 142 | + match next { |
| 143 | + None => { |
| 144 | + break; |
| 145 | + } |
| 146 | + Some(elem) => { |
| 147 | + for &vec in adj[elem].iter() { |
| 148 | + if visited[vec] { |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + visited[vec] = true; |
| 153 | + |
| 154 | + if nodes[vec] == 9usize { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + distances[vec] = distances[elem] + 1; |
| 159 | + q.push_back(vec); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + distances |
| 166 | +} |
0 commit comments