diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-12-15 21:47:10 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-12-15 21:47:10 +0100 |
commit | 67dff579e91c13ef40ecb17d14497d145861c5e7 (patch) | |
tree | 3ec7cccea75ba25aa0da34f528811d28f6c9b21a /2023/day11 | |
parent | 49ecb2db619bb32c712e619ec66833a917b5ffc3 (diff) |
add day15 pt1
Diffstat (limited to '2023/day11')
-rw-r--r-- | 2023/day11/src/lib.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/2023/day11/src/lib.rs b/2023/day11/src/lib.rs index 938de38..66c95c4 100644 --- a/2023/day11/src/lib.rs +++ b/2023/day11/src/lib.rs @@ -67,6 +67,32 @@ pub fn part1(input: &str) -> u32 { pub fn part2(input: &str) -> u32 { let mut res: u32 = 0; + let mut m: Vec<Vec<i16>> = input + .trim_end() + .split('\n') + .map(|y| y.chars().map(|x| if x == '#' { 1 } else { 0 }).collect()) + .collect(); + + expand(&mut m, 1); + let mut hs: Vec<(usize, usize)> = vec![]; + let rows = m.len(); + let cols = m[0].len(); + for i in 0..rows { + for j in 0..cols { + if m[i][j] > 0 { + hs.push((i, j)); + } + } + } + + let n = hs.len(); + for i in 0..n { + for j in i + 1..n { + res += ((hs[j].1 as i16 - hs[i].1 as i16).abs() + + (hs[j].0 as i16 - hs[i].0 as i16).abs()) as u32; + } + } + res } |