From 4eb30db28378e9fd31f18996a1c0f04634b1eb2e Mon Sep 17 00:00:00 2001 From: JonOfUs Date: Sat, 24 Dec 2022 14:38:11 +0100 Subject: [PATCH] Day 23 --- res/23/input.txt | 75 ++++++++++++++++++ res/23/test-input.txt | 7 ++ src/days/d23.rs | 171 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- 4 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 res/23/input.txt create mode 100644 res/23/test-input.txt create mode 100644 src/days/d23.rs diff --git a/res/23/input.txt b/res/23/input.txt new file mode 100644 index 0000000..dba5250 --- /dev/null +++ b/res/23/input.txt @@ -0,0 +1,75 @@ +.###....#####.#..###..##..##.######.....##.....#..#.##.#.##..##..#.#.###### +####.###...#..#...#......####.#.#..#...####....#...#....###.....###.#..#... +##..#.#...##.##..#.##.#####..#....#...####.#.####.#...###.####..#...#.#.... +.#.###.#....##.#.##..#..###......#..#..###....#.##......#..#..#..#.###..##. +...#.###.#.#.#####..##....###.#...###.###..#...#..##........#..#...##.#.... +.#.##.##...#.##.#.......###..#.##..#.##..######...##.####.###...#.########. +.####.#.#.#.#...#..#..#####.#.####...#.##.###..#.....###......###...#...#.# +..###.##..#..#.#.....#.##...#....#..##...###.###.#...####.#..#.#.##..##..#. +#.#####..#..##..#...####.####.#..#......##..#####.####.####....###..##..#.. +.#.###..#####.#...##..#.#.##.##...####.#.##.#..##...###.#.##.#....##.####.. +..##.#..##.#.##.##.#.####..#.#.#####...#......##...#.#..###.#..#...#.####.# +..#...#..#..#.###.###..#.#####..#..#....###...###.##.#...#..#######.##.##.# +#######...#.....#..#...#..#.#.#..##....#..##..##.##.#.#.......###......##.# +.##.###.###.#.##.##..##...#.###.......#..####....##..##.#....###.##...#.### +##...#...#.##..#......##..#..#..##..#..##.#..#.#.#.#######...###.......#... +#..##.#..#.#.#..#.###..##....##.#.#..##.##.#.##.###.##..##.#######....#.#.# +..#....#.##...###.#...#.#######.##....#####.####....####..####.##..###..#.. +##........##.#.####..#..##.###.#..#...#..##.#.##.###...#...##.#.#..#..#..## +.##.....##.#..#.#..#....#.#.#####.###..###...##.#.##..##..#####..##.##..##. +#........#.#..###.######..#.##...##.#....#.#.#####.#.#.##..##.#.##..###...# +...#..#..##.##..#.#...#.####..#...###...##.#..#....##.##.#.#..#.###.....##. +.#.#.#.######.#..#######..##.#.#.#..##..###.##.#.##.#..#.##.##...####.##.## +..####....####..#.....##.#.#...##..#.###..#.##.##.####...#.....#..##..###.# +....######.##.###.##.#.###....#.#.#..#...#..##..#...####..#....#....##...## +###.#..#..######.#...###....##...#.##.......##.#.##.#.#......##...#..#..#.. +#.#...#.###.#.##.#####.#.##..###.#..#.#.######..#..###...###..#.##....##.## +#.#.....#..#.####.....####..#.#.###.#.....#.###....###.#.#.##..###.##....## +#.#..##.#..###...##..#.##.....###.#.##..#.#....###....##..#..#..#.##....#.. +..###...#......##.......##.####..#....#..#....##...##..##.#.#######.#.####. +.####.#.##.#.####...#...#.##....##...#..#..##..###.####.....###......#.##.# +.####.###..#..#...#..####.##.....#....#..#..##......###......#..##.#.##..#. +###.....#.###..######...###.###.#...####.##..#.#.#.##.####.###.#...#.##.... +.###.#.##....##.#####.#.......#...#..#......##.######.####.####.###.###..#. +##.##.#..#..###.###.#.#...#.##.#..#.###.##.#.#.###..#.#...##.##.###.#.#...# +#####.#...##.#..##.##...###..#....####..##..##...#.#..##.#.#...##..###.##.# +.#...#..#.#.#.#.#...###.........###..#..##....###.####..#.#.#####.#.#...#.# +..#####.#..#...#.###...###..##......##..##.##....#####..##.####..#....##.#. +#.#.....###.#.#.###.#####.##..######.#.##..########...##.....####....###... +..##.#.#.#..#.#.##.##.#...###....##.#.#....##.####.#.#.#.######.#.#.#.####. +#.##.#....##.#..####......##.#.##.#.#.#########.#.##...###.#........#.#.... +....##.#..##.##.##..#.#.####..####.#..####.##.##.#....#..#.#..#####..###... +.##.#....###....###.#..##..####.#.#....####.###.#..#.###...#...#..##.###### +.......#.#..##.#..###.#.####..####.##..#.##.####.#.###..#.#.####.#.###...## +####.#.#...##......#..#.##.##.###.####..##...####..#...###...##.###...#...# +#.#......#.#..#.##....##.#.##.#.#..#.#.##.##.#.##...##...#.#.#......#..#.#. +..####.###...##..##.####....##.#.#.#.####..##..#.###.###...####..#.....#..# +..###..#....##.#..####.#..####.##.#####..######.#...###....##.....#..##.#.. +####.###.#....###..#...#.#...#..##..#.#...#.##.##.####.##.##.##.##..#..#... +..#.##.....#.#.#.###...#.####.#####.#.#...#.....###....##.#.##..####.#..#.. +#..##.####.####.##..##.####.#..##.#....##.#.##..###.###.####.#.##.##...##.. +#.#....#..######...##.##......#.#...#####.#.##.#..#####..#..##....#...#.### +.#.#..##.#..##.#...##.#..####.##..#...##..#.#.#..##.####...#.......##..#.## +#.#.#..####.##.#.#......###..##.##..#.#.##.##..##.###.#..####.#..##..###.## +##.##.#......#.#..#.##.#..###.#.#....##..##.#..###..#........###..#.#..#.#. +##..##..###..##.#.####.#..###.....#...####.##...#...##.######.#.#.##.#...#. +.#..##..#...##......####...##.#.##...##.###.#.##..#..###.##.....#...#.##..# +#..#....#...#...#...#..#.#.#..#.####....#.#.#...##.#...#..###..#####.#...#. +.#....#..#.####.#####.#....######....#.#####....#.##.##.#...#.#....#.....#. +#.#####..###.##.#.#..#..#...######...#.###.##.##...##.#.#.#.#.##.##...#..#. +#.#..#.....####.##......##...##..###.#.##.#..#.#.##..#...##.##.##...#...##. +####..##..##.##....##.##.####.....###.#####.###.#####....#####.#.#..##.#.## +##.#..###.#.#.#######..#..##.#..#........#.##.#..#....##...#.####..#.#..... +.#..#.######.#.###.########..#..####.##...#.#.####..##..##.#...##.#...#.... +#######...#...###.##.#.#.#....##.#..####.#.##..##.######..#.####..#..#..##. +####.#..#####.......#...###.....##.#.########..#..#####.##.##.##.#.##.#...# +####.#.....#.##.#.#...#####.###.####.####.####....#..#........#######.####. +.#.#..#.#.#.#.##.###...##....##.#.#...#.#..####.#####..##...###.#.##.#..##. +#.##.#.##.#...#..#..#..##....#.##.###...#.#..##....#....###.#..#.#####.#... +.#.##..###.##..#######.#..#.##......###..####..##.##....#...#.#.#...#.###.# +#..#..#.###.##......#...###.#.#..##...##.#..#.#.#.####.#....#.###..####..## +...##.#..#...#.#.#.#...#.#...##.#..##..#...##..#..####.....##..###.#..##... +#.####.##....####.#.#.##.##...###..#......#####.....###....#.###..##...#.## +####..##..#..##..#...###.##..###.##..#.#..###...#...##.#.#..#...#..##.....# +#.##.#.##.#....#.#...##.##...#.##......###...##.#.#...####....#.#...#...#.# +#.#.##...####...##.###.#######.#.##..#.#..###..##.##.##....#.....#..#.#.... diff --git a/res/23/test-input.txt b/res/23/test-input.txt new file mode 100644 index 0000000..7ac3ba9 --- /dev/null +++ b/res/23/test-input.txt @@ -0,0 +1,7 @@ +....#.. +..###.# +#...#.# +.#...## +#.###.. +##.#.## +.#..#.. \ No newline at end of file diff --git a/src/days/d23.rs b/src/days/d23.rs new file mode 100644 index 0000000..81fdf85 --- /dev/null +++ b/src/days/d23.rs @@ -0,0 +1,171 @@ +use std::{collections::HashMap, fs}; + +const OFFSET: usize = 500; + +pub fn solve() { + let path = "res/23/input.txt"; + + let contents = fs::read_to_string(path).expect("I/O error, wrong path?"); + + let read_grid: Vec> = contents + .lines() + .filter(|&line| line != "") + .map(|line| { + line.chars() + .map(|c| match c { + '.' => false, + '#' => true, + _ => std::unreachable!(), + }) + .collect::>() + }) + .collect(); + + let mut grid = vec![vec![false; read_grid[0].len() + 2 * OFFSET]; read_grid.len() + 2 * OFFSET]; + for (y, line) in read_grid.iter().enumerate() { + for (x, val) in line.iter().enumerate() { + if *val { + grid[y + OFFSET][x + OFFSET] = true + } + } + } + + // run 10 rounds + for i in 0..10 { + run_round(&mut grid, i); + } + + let mut min_y = grid.len(); + let mut max_y = 0usize; + let mut min_x = grid[0].len(); + let mut max_x = 0usize; + let mut elf_counter = 0; + for (y, line) in grid.iter().enumerate() { + for (x, val) in line.iter().enumerate() { + if *val { + min_y = min_y.min(y); + max_y = max_y.max(y); + min_x = min_x.min(x); + max_x = max_x.max(x); + elf_counter += 1; + } + } + } + + let res = (max_y - min_y + 1) * (max_x - min_x + 1) - elf_counter; + println!("Result 1: {res}"); + + // task 2 + let mut round = 10; + while run_round(&mut grid, round) != 0 { + round += 1; + } + println!("Result 2: {}", round + 1) +} + +fn new_pos(y: usize, x: usize, round: i32, grid: &Vec>) -> Option<(usize, usize)> { + if !grid[y][x] { + return None; + } + + let mut elf_adjacent = false; + for yi in -1..=1 { + for xi in -1..=1 { + if (xi != 0 || yi != 0) && grid[(y as i64 + yi) as usize][(x as i64 + xi) as usize] { + elf_adjacent = true; + } + } + } + if !elf_adjacent { + return None; + } + + let north: Vec<(i64, i64)> = vec![(-1, -1), (-1, 0), (-1, 1)]; + let south: Vec<(i64, i64)> = vec![(1, -1), (1, 0), (1, 1)]; + let west: Vec<(i64, i64)> = vec![(-1, -1), (0, -1), (1, -1)]; + let east: Vec<(i64, i64)> = vec![(-1, 1), (0, 1), (1, 1)]; + + let order = match round % 4 { + 0 => { + // north + vec![north, south, west, east] + } + 1 => { + // south + vec![south, west, east, north] + } + 2 => { + // west + vec![west, east, north, south] + } + 3 => { + // east + vec![east, north, south, west] + } + _ => std::unreachable!(), + }; + + for dir in order { + if grid[(y as i64 + dir[0].0) as usize][(x as i64 + dir[0].1) as usize] { + continue; + } + if grid[(y as i64 + dir[1].0) as usize][(x as i64 + dir[1].1) as usize] { + continue; + } + if grid[(y as i64 + dir[2].0) as usize][(x as i64 + dir[2].1) as usize] { + continue; + } + return Some(( + (y as i64 + dir[1].0) as usize, + (x as i64 + dir[1].1) as usize, + )); + } + + return None; +} + +// Runs a round, returns how many elves moved +fn run_round(grid: &mut Vec>, round: i32) -> usize { + let mut changes_map = HashMap::<(usize, usize), Vec<(usize, usize)>>::new(); + + for (y, line) in grid.iter().enumerate() { + for (x, _) in line.iter().enumerate() { + match new_pos(y, x, round, &grid) { + Some((new_y, new_x)) => { + if changes_map.contains_key(&(new_y, new_x)) { + changes_map.get_mut(&(new_y, new_x)).unwrap().push((y, x)); + } else { + changes_map.insert((new_y, new_x), vec![(y, x)]); + } + } + None => {} + } + } + } + + let mut elf_move_counter = 0usize; + + for (pos, old_pos_vec) in changes_map { + // if only one elf wants to go to position + if old_pos_vec.len() == 1 { + grid[old_pos_vec[0].0][old_pos_vec[0].1] = false; + grid[pos.0][pos.1] = true; + elf_move_counter += 1; + } + } + + return elf_move_counter; +} + +fn _print_grid(grid: &Vec>) { + for y in grid.iter() { + for x in y.iter() { + match *x { + true => print!("#"), + false => print!("."), + } + } + println!() + } + println!(); +} diff --git a/src/main.rs b/src/main.rs index b8067c8..d546d12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ pub mod days; fn main() { - days::d21::solve() + days::d23::solve() //_all_days() }