From 7865e624c54ed429e40c2533b4799d2aeb582853 Mon Sep 17 00:00:00 2001 From: Jos van Goor Date: Tue, 12 Dec 2023 21:40:46 +0100 Subject: [PATCH] Advent 2023 Day 11 --- advent_of_code_2023/day11/input.txt | 140 ++++++++++++++++++++++++++++ advent_of_code_2023/day11/main1.rs | 73 +++++++++++++++ advent_of_code_2023/day11/main2.rs | 88 +++++++++++++++++ 3 files changed, 301 insertions(+) create mode 100644 advent_of_code_2023/day11/input.txt create mode 100644 advent_of_code_2023/day11/main1.rs create mode 100644 advent_of_code_2023/day11/main2.rs diff --git a/advent_of_code_2023/day11/input.txt b/advent_of_code_2023/day11/input.txt new file mode 100644 index 0000000..4fac1a9 --- /dev/null +++ b/advent_of_code_2023/day11/input.txt @@ -0,0 +1,140 @@ +.......#..............#.......................#............#.......#........................#.....#..................................#...... +.................................#.................................................#........................................................ +............................................................................................................................................ +..........................................................................................................#.............#................... +...........#...............................................................#..............................................................#. +.#.......................................................................................................................................... +................#.......#........................#....................#....................#.................................#.............. +......................................#.....#.........................................................#..................................... +...................................................................................#..........................#......................#...... +......#..................................................................................................................................... +.............#............................................#.......................................................#......................... +..................#.....................#..........#.....................................................#..............#.....#............. +.................................................................#.........#................................................................ +..................................#.................................................................................................#......# +....#..........#...........#..........................#.....#................................................#.............................. +................................................................................#........................................................... +.......................................#........#.......................#...........................#....................................... +.......................................................................................................................................#.... +.#..............................#.......................#........#.................#..............................#......................... +......#..............#....................#............................................................#.................#.................. +............#............................................................................................................................... +.....................................................................................................................#...................... +.....................................................#...................................................................................#.. +...............................................#...........................#.....#.......................................................... +....................................#........................................................................#.............................. +...........#.................#.........................................................#...........#............................#........... +.....#...............#....................................#...................................#........................................#.... +..........................................................................................................#................#................ +.......................................#..............#....................................................................................# +.................................#................................................#...................#.............#....................... +...#........#...............#.....................#............#.................................#.............#............................ +............................................................................................................................................ +.............................................#........................#..................................................................... +..........................................................#................................................#..............................#. +...................#...................#........................................#..............................................#............ +....#.....#.......................#...........................................................#.....#....................................... +........................................................................................#................................................... +..............................................#.........................................................#................................... +..........................#................................................#.............................................#..............#... +........#.....................................................#.......#..........................................................#.......... +.............#.............................#..........#..............................#.....................#................................ +............................................................................................................................................ +.#..............................................................................#.................#......................................... +......................#...............#..................................................................................................... +...................................................................#..................................................................#..... +..........#.................................................................................#......................#........................ +.................#........................#............................#.................................................................... +..........................................................#................................................#............#................... +..................................................#........................#...................................................#............ +..............................................................................................#..................#.......................... +............................................................................................................................................ +.....#...............................................................................................#...................................... +....................................#.......................................................................#........#...................... +...................#............................................................#..........#................................................ +.............................#..........................................................................................................#... +...............#...............................#..........#......................................#.....#.....................#.............. +.......#...............#.............................................................#............................................#......... +............................................................................................................................................ +...................................#.......#........#........#.................................................#............................ +............................................................................................................................................ +..........#..................#.........................................#..............................................................#..... +................................................#..........................................#................#....................#.......... +.........................................................#...........................#...................................................... +#....................#...........#.................................................................#..............#......#.................. +.............................................#.......#...................................................#.................................. +............................#...........................................................#.....#............................................. +.......#........................................................................#........................................................... +..........................................................#...................................................#....................#........ +...#...................................................................................................................#.................... +...............#.................#.......................................................................................................... +....................#.................#.......#..................#.........................#.............#.............................#.... +........................................................#...........................#....................................................... +...........................#...............................................#................................................................ +.#.......#........................................................................................#........................................# +................................#................................................................................#...............#.......... +....................................................#....................................................................................... +...................................................................#.....................................#...............#............#..... +....................................#......................#.............#..........#.....#................................................. +...........#................................................................................................................................ +.......................#.....................#.................................................................#............................ +.............................#..............................................#........................#...................................... +..................#................................................................................................#................#....... +..................................................#.........#..........#..................................................#................. +.................................#.......................................................#.................................................# +....#.......................................#.........#.......................................................................#............. +............................................................................................................................................ +............................................................................................................................................ +........................................................................#................................................................... +..#.............................................#................#....................................................#..................... +..............#...............#............................................................................................................. +........................#............................#..........................#........#.................................................. +...................................................................................................#...........#........................#... +....#.............#..............#............................................................................................#............. +............................................................................................................................................ +......................................................................................................#..................................... +.................................................................#......................#................................................... +.....................#....................#...........#.................#................................................................... +................................................................................#......................................#..........#........# +........#........................................................................................#.......................................... +..................................#.........................................................#....................#..........#............... +#...........................#...........................................................................#................................... +............#................................................#.........#...............#.................................................... +....#....................................................................................................................................... +..........................................#.....................................................#..............#..............#............. +............................................................................#.........................#.............#....................... +................................#.....#..................................................................................................... +...................#......#.................................#.......................#.......................#............................... +...........................................................................................#......#...............................#......... +#.............#.......................................#..................................................................................... +.............................#..............#.............................................................................................#. +......................#..................................................#............................#..............#...................... +........#........#.....................#...................#......#.............#............................#............#................. +.........................................................................................#......................................#........... +..................................#..................#............................................#...............#..................#...... +..........................#....................#............................................................................................ +..........................................#................................................................................................. +.#...................................................................#.......................#.............#...............................# +......#.......................#................................#.....................................................#...................... +............#.....#...................#............#.....#.......................#.................#........................................ +..........................................................................................................................#.....#......#.... +..........................................................................................#.............#................................... +...........................................#........................................#....................................................... +..#...................................................................#..................................................................... +.........#...........................................#...................................................................................... +....................................#............................................................#.................................#.......# +...........................#......................................................................................#......................... +..........................................................#............................................................#.................... +...#..........#........#.................................................................................................................... +....................................................................................#......#.................#..................#........... +..............................................................................#............................................................. +.............................#.......#.....#...................#........#......................#.....................................#...... +............................................................................................................................................ +..........#.....#...................................................#.............#.....................#....................#.............. +........................#.................................#.............................#...........................#....................... +........................................#...........#........................................#...........................................#.. +.....#.........................................#.................#.....#............................#....................................... +...................................#........................................#........#...................................................... +...........................................................................................................................#................ +.#............................#..........................#...................................................#.........................#.... +.........#...................................#..........................................#.........................#......................... \ No newline at end of file diff --git a/advent_of_code_2023/day11/main1.rs b/advent_of_code_2023/day11/main1.rs new file mode 100644 index 0000000..e816c88 --- /dev/null +++ b/advent_of_code_2023/day11/main1.rs @@ -0,0 +1,73 @@ +use std::fs::read_to_string; + +fn difference(lhs: usize, rhs: usize) -> usize { + if lhs > rhs { + return lhs - rhs; + } + rhs - lhs +} + +fn main() { + let input = read_to_string("input.txt").unwrap(); + let mut starmap = input.lines().map(|line| { + let mut vec: Vec = Vec::new(); + vec.extend_from_slice(line.as_bytes()); + vec + }).collect::>(); + + let mut index = 0; + loop { + if index == starmap.len() { + break; + } + + if starmap[index].iter().filter(|ch| { **ch == b'#' }).count() == 0 { + starmap.insert(index, starmap[index].clone()); + index += 1; + } + + index += 1; + } + + index = 0; + loop { + if index >= starmap[0].len() { + break; + } + + let mut sum = 0; + for line in &starmap { + if line[index] == b'#' { + sum += 1; + } + } + + if sum == 0 { + for line in &mut starmap { + line.insert(index, b'.'); + } + index += 1; + } + + index += 1; + } + + let mut stars: Vec<(usize, usize)> = Vec::new(); + for row in 0..starmap.len() { + for col in 0..starmap[row].len() { + if starmap[row][col] == b'#' { + stars.push((row, col)); + } + } + } + + let mut sum_difference = 0; + for start_idx in 0..stars.len() { + for inner_idx in start_idx..stars.len() { + sum_difference += difference(stars[start_idx].0, stars[inner_idx].0); + sum_difference += difference(stars[start_idx].1, stars[inner_idx].1); + } + } + + println!("Sum of ranges: {}", sum_difference); +} diff --git a/advent_of_code_2023/day11/main2.rs b/advent_of_code_2023/day11/main2.rs new file mode 100644 index 0000000..a9f5d2e --- /dev/null +++ b/advent_of_code_2023/day11/main2.rs @@ -0,0 +1,88 @@ +use std::fs::read_to_string; + +fn walk_row(distance_map: &Vec<(usize, usize)>, start: usize, end: usize) -> usize { + let mut distance: usize = 0; + let start_index = if start < end { start } else { end }; + let end_index = if start > end { start } else { end }; + + for index in start_index..end_index { + distance += distance_map[index].0; + } + + distance +} + +fn walk_col(distance_map: &Vec>, col: usize, start: usize, end: usize) -> usize { + let mut distance = 0; + let start_index = if start < end { start } else { end }; + let end_index = if start > end { start } else { end }; + + for index in start_index..end_index { + distance += distance_map[index][col].1; + } + + distance +} + +fn main() { + let expansion: usize = 1_000_000; + let input = read_to_string("input.txt").unwrap(); + let starmap = input.lines().map(|line| { + let mut vec: Vec = Vec::new(); + vec.extend_from_slice(line.as_bytes()); + vec + }).collect::>(); + + let mut distance_map = starmap.iter().map(|line| { + vec![(1 as usize, 1 as usize); line.len()] + }).collect::>(); + + for index in 0..starmap.len() { + if starmap[index].iter().filter(|ch| { **ch == b'#' }).count() == 0 { + for distance in distance_map[index].iter_mut() { + distance.1 *= expansion; + } + } + } + + let mut index = 0; + loop { + if index >= starmap[0].len() { + break; + } + + let mut sum = 0; + for line in &starmap { + if line[index] == b'#' { + sum += 1; + } + } + + if sum == 0 { + for idx in 0..distance_map.len() { + distance_map[idx][index].0 *= expansion; + } + } + + index += 1; + } + + let mut stars: Vec<(usize, usize)> = Vec::new(); + for row in 0..starmap.len() { + for col in 0..starmap[row].len() { + if starmap[row][col] == b'#' { + stars.push((row, col)); + } + } + } + + let mut sum_difference = 0; + for start_idx in 0..stars.len() { + for inner_idx in start_idx..stars.len() { + sum_difference += walk_col(&distance_map, stars[start_idx].1, stars[start_idx].0, stars[inner_idx].0); + sum_difference += walk_row(&distance_map[stars[start_idx].0], stars[start_idx].1, stars[inner_idx].1); + } + } + + println!("Sum of ranges: {}", sum_difference); +}