From 0024d9175c87ed3614c938740f7885293b9e0e85 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Mon, 11 Dec 2023 13:03:26 +0530 Subject: [PATCH] day11: added --- src/day10/2.rs | 3 +- src/day11/1.rs | 80 ++++++++++++++++++++++++- src/day11/2.rs | 82 ++++++++++++++++++++++++- src/day11/input.txt | 140 +++++++++++++++++++++++++++++++++++++++++++ src/day11/sample.txt | 10 ++++ 5 files changed, 311 insertions(+), 4 deletions(-) create mode 100644 src/day11/input.txt create mode 100644 src/day11/sample.txt diff --git a/src/day10/2.rs b/src/day10/2.rs index 8516433..f01feb9 100644 --- a/src/day10/2.rs +++ b/src/day10/2.rs @@ -117,8 +117,6 @@ fn process(data: &str) -> usize { break; } start = false; - - local_map[sx][sy] = grid[sx][sy]; // what we need to figure out the next step // 1. The current character // 2. Direction we are headed in @@ -143,6 +141,7 @@ fn process(data: &str) -> usize { let next_pipe = grid[p as usize][q as usize]; if valid_pipes.contains(&next_pipe) { + local_map[sx][sy] = grid[sx][sy]; sx = p as usize; sy = q as usize; direction = new_direction; diff --git a/src/day11/1.rs b/src/day11/1.rs index b51073e..0cc58ff 100644 --- a/src/day11/1.rs +++ b/src/day11/1.rs @@ -1 +1,79 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +#![feature(test)] + +extern crate test; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +fn process(data: &str) -> usize { + let grid: Vec> = data + .lines() + .filter(|x| !x.is_empty()) + .map(|x| x.chars().collect()) + .collect(); + + let n = grid[0].len(); + + let mut blank_cols = Vec::new(); + for j in 0..n { + let mut all_empty = true; + + for row in grid.iter() { + if row[j] != '.' { + all_empty = false; + break; + } + } + + if all_empty { + blank_cols.push(j); + } + } + + let mut nodes = vec![]; + + let mut total_row_offset = 0; + for (i, row) in grid.iter().enumerate() { + if !row.iter().any(|&x| x == '#') { + total_row_offset += 1; + } + let mut total_col_offset = 0; + let mut j_offset = 0; + + for j in 0..n { + if j_offset < blank_cols.len() && j >= blank_cols[j_offset] { + total_col_offset += 1; + j_offset += 1; + } + if grid[i][j] != '#' { + continue; + } + + nodes.push(((i + total_row_offset), (j + total_col_offset))); + } + } + + let mut answer = 0; + for (i, &(sx, sy)) in nodes.iter().enumerate() { + for &(dx, dy) in nodes.iter().skip(i + 1) { + let distance = ((dy as i32 - sy as i32).abs() + (dx as i32 - sx as i32).abs()) as usize; + + answer += distance; + } + } + + answer +} + +fn main() { + for input in INPUTS.iter() { + println!("total = {}", process(input)); + } +} + +#[bench] +fn part1(b: &mut test::Bencher) { + b.iter(|| { + let v = process(INPUTS[INPUTS.len() - 1]); + test::black_box(v); + }); +} diff --git a/src/day11/2.rs b/src/day11/2.rs index b51073e..dcc4295 100644 --- a/src/day11/2.rs +++ b/src/day11/2.rs @@ -1 +1,81 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +#![feature(test)] + +extern crate test; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +const MULTIPLIER: usize = 999999; + +fn process(data: &str) -> usize { + let grid: Vec> = data + .lines() + .filter(|x| !x.is_empty()) + .map(|x| x.chars().collect()) + .collect(); + + let n = grid[0].len(); + + let mut blank_cols = Vec::new(); + for j in 0..n { + let mut all_empty = true; + + for row in grid.iter() { + if row[j] != '.' { + all_empty = false; + break; + } + } + + if all_empty { + blank_cols.push(j); + } + } + + let mut nodes = vec![]; + + let mut total_row_offset = 0; + for (i, row) in grid.iter().enumerate() { + if !row.iter().any(|&x| x == '#') { + total_row_offset += MULTIPLIER; + } + let mut total_col_offset = 0; + let mut j_offset = 0; + + for j in 0..n { + if j_offset < blank_cols.len() && j >= blank_cols[j_offset] { + total_col_offset += MULTIPLIER; + j_offset += 1; + } + if grid[i][j] != '#' { + continue; + } + + nodes.push(((i + total_row_offset), (j + total_col_offset))); + } + } + + let mut answer = 0; + for (i, &(sx, sy)) in nodes.iter().enumerate() { + for &(dx, dy) in nodes.iter().skip(i + 1) { + let distance = ((dy as i32 - sy as i32).abs() + (dx as i32 - sx as i32).abs()) as usize; + + answer += distance; + } + } + + answer +} + +fn main() { + for input in INPUTS.iter() { + println!("total = {}", process(input)); + } +} + +#[bench] +fn part1(b: &mut test::Bencher) { + b.iter(|| { + let v = process(INPUTS[INPUTS.len() - 1]); + test::black_box(v); + }); +} diff --git a/src/day11/input.txt b/src/day11/input.txt new file mode 100644 index 0000000..7e78179 --- /dev/null +++ b/src/day11/input.txt @@ -0,0 +1,140 @@ +.#..........#.....................#........................................................#....................................#........... +...................#........................................#......................#........................................................ +...........................................#......#..................#...................................................#.................. +....#......................#...............................................#.........................#.....#......#................#........ +.................................................................#.......................#.................................................. +..............................................................................................................................#............. +.......#........#.............#...................................................#...................................#..................... +.........................#...................................#............................................................................#. +....................#.............#.........#..........................................#...............#.................................... +.....................................................#...............#.............................................................#........ +...........................................................................................................................#................ +..........................................................#.........................................#...............#....................... +..............................................................................................#............................................. +.#........#........................#......#..............................................#..............................#...............#... +...............#......#...............................#.......#.........#......#.............................#.................#............ +...............................#..............#............................................................................................. +............................................................................................................................................ +.........................#.......................................#.......................................................................... +..................#........................#......................................#........................................#................ +..................................................#.......#..............................................#........................#.......#. +...#........#........................#....................................#...............#................................................. +............................................................................................................................................ +.......#........................#....................................................................................#...................... +...............#...........#........................................................................#....................................... +.............................................#................#.........#.............................................................#..... +...........#...........................#................#...........................#....................................................... +..................................................#.......................................#.............#................................... +.....#..................#..........#..............................#.............................#........................................#.. +..............................#...............................................................................................#............. +...................................................................................................................#........................ +..............#..........................................#.............#...........................#..............................#......... +...#...................................#.........................................#.......................................................... +..................#...................................................................#................................................#.... +..........................#...................................#.........................................#................................... +............................................................................................................................................ +.........#...........#..........#..........................................#....................#..............#........#.....#...........#. +....................................................#....................................................................................... +.............................................#.............................................................................................. +......................................#..................#..............................#...........#.......#............................... +............................#..................................#..............#............................................................. +...............#............................................................................................................................ +...........................................#............................#.......................#........................................... +.....#..............#.................................................................................#...........................#......... +............................................................................................................................................ +........................................#..........#.................#..........................................#........................... +.............................................#.................................#...........#................................................ +...............................#.......................#...........................................#.......#....................#........... +#........#.............#...............................................................................................#...................# +.....................................#........................#..........#.........#........................................................ +.................#............................................................................#............................................. +..........................#.......................................#.................................................#....................... +.....................................................#...................................#....................................#............. +..#......................................................................................................................................... +.............#.....#.......................#.............#....................................................#........................#.... +......#........................................................................................#............................................ +.................................#.................................#........................................................................ +.................................................................................#.......................#................................#. +#.....................................................#...................................................................#.....#........... +.........#..................................................#......................................#........................................ +..............................#.............#.................................................................#.......#..................... +........................#............................................................#......#................................#.............. +.............#.............................................................................................................................. +.....#........................................................#............#..........................#...........................#......... +...........................#......#.................................................................................#...................#... +.........#.....................................#...............................#............................................................ +..................#.......................................#.......#................................#........................................ +...............................#.....................#...................................#................................#................. +..............#..........................#.............................#............#.....................#.....................#.........#. +.................................................................................................................#.......................... +......#..................................................................................................................................... +...........#.............#.........#........................................................................................................ +...................................................................#..............#..................#...................................... +..................#...........................................................................................#............................. +........#.................................#..............#.................................................................#..........#..... +...#.....................................................................#..............#.......#........................................... +............................................................................................................................................ +............................................................................................................................................ +.................................#................#..........................................................#...............#.............# +..............#............................................................#................................................................ +......#.....................#..........................#........#.....#............#..............#......................................... +....................#..................................................................................#..................#................. +....................................#.........#........................................................................................#.... +..........................................................#..............#.................................................................. +................#...............................................................................................#.............#............. +............................................................................................................................................ +............................#...................#......#.............#..............................................#....................... +.............#...................................................................................#........................................#. +......................#..........................................................#.......#.................................................. +#...........................................................#............................................................................... +.........#.........................#.................#....................................................#.................#............... +............................................................................................................................................ +...................#.............................................#.....#.................................................................... +........................#.........................#.....................................................................................#... +.....#........................................................................................................................#............. +.......................................#...............................................................#.........#.......................... +............................#...........................#...........#......................#..............................#.......#......... +..#................................#...........#............................................................................................ +.................#..................................#...................#................................................................... +..........#.................................................#................................................#.............................. +.....................#...............................................................................#...................................... +..............#..................#..............................#.............#....................................#.........#........#..... +...#.............................................................................................#.......................................... +............................................#..........................................#.................................................... +.........#...............................................#..............................................................#................... +................#.........#................................................................................................................. +.....#..................................#............................#..............................#........#..................#..........# +...............................#............................................................................................................ +....................................................#........#.............................................................................. +........#..........................#.........#...............................................................................#.............. +....................................................................................................................#....................... +....................#........#......................................#.........#.....#.....#................................................. +................................................................................................................................#.....#..... +...............#..........................................#...............#...............................#................................. +...#..............................#......................................................................................................... +........................................................................................#............#....................................#. +............................................................................................................................#............... +...................................................................................#............................#........................... +#........#........#........#..........#.........#........................................................................................... +.............................................................#..............................#............................#.................. +................................#.......................#................................................................................... +.......................#................................................#...............#.........#..................#................#..... +............#..................................................................#........................#................................... +............................................................................................................................................ +......#.....................#.......#......#..............#......#.......................................................................... +.....................#...................................................................................................................... +................................#................................................#.............#...........................#.............#.. +..............................................................................................................#............................. +............#............#..........................................................................#.............................#......... +.....#...........#.....................#.............................#.....#........................................#....................... +................................................#...........#..........................#.................................................... +............................................................................................................................................ +#..........................#.....#................................................................#......................................#.. +..........................................#.................................................................#.............#................. +................#.........................................................#................................................................. +.......#..............................#..............................................#..........................................#........... +........................#...................................#............................................................................... +............................................................................................................................#............... +..............................#.....................#.............................#...........#.................#........................... +...............................................#................#.....#................................................#.................... +...................#...................#.................#.................#.........................#...................................... diff --git a/src/day11/sample.txt b/src/day11/sample.txt new file mode 100644 index 0000000..a0bda53 --- /dev/null +++ b/src/day11/sample.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file