diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-12-11 17:47:38 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-12-11 17:47:38 +0100 |
commit | 49ecb2db619bb32c712e619ec66833a917b5ffc3 (patch) | |
tree | d69fb0b384e9810c834f7e93057a8995489ccbad | |
parent | c54e7dbf8a1624867b0f8bfd2e28fdbd481f4588 (diff) |
day11 pt1
-rw-r--r-- | 2023/Cargo.toml | 2 | ||||
-rw-r--r-- | 2023/day11/Cargo.toml | 8 | ||||
-rw-r--r-- | 2023/day11/example.txt | 10 | ||||
-rw-r--r-- | 2023/day11/input.txt | 140 | ||||
-rw-r--r-- | 2023/day11/src/lib.rs | 104 |
5 files changed, 263 insertions, 1 deletions
diff --git a/2023/Cargo.toml b/2023/Cargo.toml index d85ec98..effa9b6 100644 --- a/2023/Cargo.toml +++ b/2023/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["day1", "day2", "day3", "day4", "day5", "day6", "day7", "day7_2", "day8", "day9"] +members = ["day1", "day2", "day3", "day4", "day5", "day6", "day7", "day7_2", "day8", "day9", "day11"] diff --git a/2023/day11/Cargo.toml b/2023/day11/Cargo.toml new file mode 100644 index 0000000..8f5b9a5 --- /dev/null +++ b/2023/day11/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day11" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023/day11/example.txt b/2023/day11/example.txt new file mode 100644 index 0000000..986aad4 --- /dev/null +++ b/2023/day11/example.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... diff --git a/2023/day11/input.txt b/2023/day11/input.txt new file mode 100644 index 0000000..6ba87be --- /dev/null +++ b/2023/day11/input.txt @@ -0,0 +1,140 @@ +.......................................#..........................................................#......................................#.. +.....................#................................#..................................................................................... +....#.....#...................#...............#....................#.....................#.....................#....................#....... +............................................................................................................................................ +.................#......................................................#......#..............#....................#........................ +......................................#..................................................................................................... +.......................................................#...................................................#..............#................. +.............................................................#..........................#................................................... +............#..............................#.................................#.................................................#............ +#.................................#.............#................#....................................................................#..... +....................................................................................#................#...................................... +....#..........#............#...........................................#..................#..................#...................#......... +......................................#...................#.................................................................#.............#. +........#....................................#.......#...................................................................................... +........................................................................................#..........#........................................ +..#................................................................#........................................................................ +..............................#...............................................#.....................................#............#.......... +...................#........................................................................................................................ +...........#......................#........#................................................................................................ +...............................................................#.................#.........................#................................ +......................................................#.....................................#............................................... +........#............................................................................#................................#.........#........... +................#...........#............................................................................................................... +.....................#........................#........................#.....................................#............................#. +....#........................................................#................#.............................................#............... +............................................................................................................................................ +.....................................................................................................#.............#...................#.... +..........................#...........#.........................................................................................#........... +#......................................................#.........................................#............#........#.................... +.........#.....................................#....................................#....................................................... +.................................#..................................#......................................................#........#....... +.........................................................................................................#.................................. +......#.............#....................................................#.................................................................. +..........................#........................................................................................#........................ +.#.......................................#...................#...............#........................#..................#.................. +..................................#..............#...........................................................#.................#............ +.........#.........................................................#........................#............................................... +....................................................................................#....................................................... +.....#........#............................#...............................#.........................................#...................... +......................................................................#..................................#.................................. +............................#..................#........#..............................................................................#.... +.......................#.............#...................................................................................................... +#..........#...................................................#.......................#........................#......#.................... +....................................................#.............................................#......................................... +.................................................................................................................................#.......... +..................................#....................................................................#.................................... +........#.......................................#............#.....#........#............................................................... +............................................................................................................................................ +............................................................................................................................................ +..........................#.............................................................#.........................#......................... +..#............................#..........#..................................................................................#.............. +..................#...................................................#.............#.......................#.............................#. +............#.................................#........#.....#.............................................................................. +............................................................................................................................................ +.......................................................................................#..............#.................#...........#....... +....................#.....#....................................................#............#.................#............................. +....#............................................................................................#..............................#..........# +............................................................................................................................................ +...............................#......................#...............................................................#..................... +.................................................#...............#..................#.........#.............................#............... +........#......#.....................#................................................................................................#..... +.............................................#.................................#............................................................ +.#......................#..............................................................#................#................................... +............#.....#.....................................................................................................#................... +..................................#.....................................................................................................#... +............................................................................................................................................ +......#...............................................................#........................#..............................#............. +.....................#...........................#....................................#..............................#...................... +............................................................................#............................................................... +............#................#........#.......................#.....................................#....................................... +............................................................................................#.................#...........#................. +...................#................................................................................................................#....... +...................................#...................#..........#............#............................................................ +#.........................................#.....#..................................................................#........................ +............................................................................................................#....................#.......... +..........#..........................................................#.................................#...................................# +............................................................................................................................................ +......#.................#.......................................................#...............#.........................#................. +.............................#................#...................#.............................................#........................... +.............#.......................................#......................#.......#................................#...................... +........................................#................................................#...........................................#...... +...................................#........................................................................................................ +.................#...........................................#..............................................................#............... +.........#................#.......................................................................................#......................... +.....................#...................................................................................................................... +.#..............................................................#............#..........#.....#.........#................................... +......#.................................#.............................#..................................................................... +................................................................................................................................#........... +......................................................#............................................#....................#..............#.... +.........................#...................#.............................................................................................. +...............................................................#............#..........#.................#.................................. +..................................#......................................................................................................... +...............#..........................................................................................................#......#.......... +............................................................................................................................................ +......#........................................#..................................#................#..................................#..... +.#..........................#.........#.............................................................................#....................... +.......................................................#......................#.......#..................................................... +.............................................................#.................................................................#............ +...............#......................................................................................#.......#...........................#. +.....#....................................................................#...................#............................................. +..........................#................#.....#................#......................................................................... +..................................................................................#.................................#......#...........#.... +.................#.......................................................................................................................... +..................................#......................................................................................................... +...........#.........................................#.................#...............#.................................................... +................................................................#..............................#..........#....................#............ +...............#.............#............................#..................#.............................................................. +..#............................................#...........................................................................#................ +......................#..................................................................................................................... +.......................................#.................................#..........................................................#....... +...................................................................#........................................................................ +....#.............#................................................................#..................................#......#.............. +.............................................................#.............................................................................. +.........#................#.....................#.....................#.....#.....................#.......#................................. +...................................................................................................................................#........ +...........................................#...............................................#................................................ +.....................................................#..........#....................#...........................#............#.........#... +.#.............#......#..................................................................................................................... +.............................#............................................#..............................#.................................. +.......................................#............................................................................#....................... +..................#......................................#............#.............................#....................................... +.........#................#.....................................................#..............#.............#....................#.......#. +..#..............................................................#.........................................................#................ +...................................#..........#.............................#.........................................................#..... +......#..............................................................................................................#...................... +..............#............................................................................................................................. +......................................................#........#.................#.......................................#.................. +......................#.......#.................#......................................#.......#...............#............................ +..#..............#.......................#.................................................................................................. +.......................................................................#...................#.............#.................................. +.........#..........................................................................#.................................#..................... +......................................#..................#.......................................#....................................#..... +.........................#............................................................................#.......#.............#............... +....................#.........................................#............................................................................. +...............#............................................................................................................................ +......................................................................#...........#........#...................................#............ +.................................#..........#............................................................................................... +......#..........................................#............................#....................................#........................ +...............................................................#...........................................#.............................#.. +..#.........#..........#.................#..........................................................#....................................... diff --git a/2023/day11/src/lib.rs b/2023/day11/src/lib.rs new file mode 100644 index 0000000..938de38 --- /dev/null +++ b/2023/day11/src/lib.rs @@ -0,0 +1,104 @@ +fn expand(m: &mut Vec<Vec<i16>>, ch: i16) { + let rows = m.len(); + let mut cols = m[0].len(); + + let mut i = 0; + while i < rows { + if m[i].iter().filter(|&x| *x != 0).count() == 0 { + m.insert(i, vec![ch; cols]); + i += 1; + } + i += 1; + } + let rows = m.len(); + i = 0; + + while i < cols { + let mut c = 0; + for j in 0..rows { + if m[j][i] != 0 { + c += 1; + } + } + + if c == 0 { + for j in 0..rows { + m[j].insert(i, ch); + } + cols += 1; + i += 1; + } + i += 1; + } +} + +pub fn part1(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, 0); + 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 +} + +pub fn part2(input: &str) -> u32 { + let mut res: u32 = 0; + + res +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_part1() { + let input = include_str!("../example.txt"); + let result = part1(input); + assert_eq!(result, 374); + } + + #[test] + fn input_part1() { + let input = include_str!("../input.txt"); + let result = part1(input); + assert_eq!(result, 9623138); + } + + #[test] + fn example_part2() { + let input = include_str!("../example.txt"); + let result = part2(input); + assert_eq!(result, 1030); + } + // + // #[test] + // fn input_part2() { + // let input = include_str!("../input.txt"); + // let result = part2(input); + // assert_eq!(result, 71585); + // } +} |