Day 23
This commit is contained in:
parent
c09a7236cd
commit
4eb30db283
4 changed files with 254 additions and 1 deletions
75
res/23/input.txt
Normal file
75
res/23/input.txt
Normal file
|
@ -0,0 +1,75 @@
|
|||
.###....#####.#..###..##..##.######.....##.....#..#.##.#.##..##..#.#.######
|
||||
####.###...#..#...#......####.#.#..#...####....#...#....###.....###.#..#...
|
||||
##..#.#...##.##..#.##.#####..#....#...####.#.####.#...###.####..#...#.#....
|
||||
.#.###.#....##.#.##..#..###......#..#..###....#.##......#..#..#..#.###..##.
|
||||
...#.###.#.#.#####..##....###.#...###.###..#...#..##........#..#...##.#....
|
||||
.#.##.##...#.##.#.......###..#.##..#.##..######...##.####.###...#.########.
|
||||
.####.#.#.#.#...#..#..#####.#.####...#.##.###..#.....###......###...#...#.#
|
||||
..###.##..#..#.#.....#.##...#....#..##...###.###.#...####.#..#.#.##..##..#.
|
||||
#.#####..#..##..#...####.####.#..#......##..#####.####.####....###..##..#..
|
||||
.#.###..#####.#...##..#.#.##.##...####.#.##.#..##...###.#.##.#....##.####..
|
||||
..##.#..##.#.##.##.#.####..#.#.#####...#......##...#.#..###.#..#...#.####.#
|
||||
..#...#..#..#.###.###..#.#####..#..#....###...###.##.#...#..#######.##.##.#
|
||||
#######...#.....#..#...#..#.#.#..##....#..##..##.##.#.#.......###......##.#
|
||||
.##.###.###.#.##.##..##...#.###.......#..####....##..##.#....###.##...#.###
|
||||
##...#...#.##..#......##..#..#..##..#..##.#..#.#.#.#######...###.......#...
|
||||
#..##.#..#.#.#..#.###..##....##.#.#..##.##.#.##.###.##..##.#######....#.#.#
|
||||
..#....#.##...###.#...#.#######.##....#####.####....####..####.##..###..#..
|
||||
##........##.#.####..#..##.###.#..#...#..##.#.##.###...#...##.#.#..#..#..##
|
||||
.##.....##.#..#.#..#....#.#.#####.###..###...##.#.##..##..#####..##.##..##.
|
||||
#........#.#..###.######..#.##...##.#....#.#.#####.#.#.##..##.#.##..###...#
|
||||
...#..#..##.##..#.#...#.####..#...###...##.#..#....##.##.#.#..#.###.....##.
|
||||
.#.#.#.######.#..#######..##.#.#.#..##..###.##.#.##.#..#.##.##...####.##.##
|
||||
..####....####..#.....##.#.#...##..#.###..#.##.##.####...#.....#..##..###.#
|
||||
....######.##.###.##.#.###....#.#.#..#...#..##..#...####..#....#....##...##
|
||||
###.#..#..######.#...###....##...#.##.......##.#.##.#.#......##...#..#..#..
|
||||
#.#...#.###.#.##.#####.#.##..###.#..#.#.######..#..###...###..#.##....##.##
|
||||
#.#.....#..#.####.....####..#.#.###.#.....#.###....###.#.#.##..###.##....##
|
||||
#.#..##.#..###...##..#.##.....###.#.##..#.#....###....##..#..#..#.##....#..
|
||||
..###...#......##.......##.####..#....#..#....##...##..##.#.#######.#.####.
|
||||
.####.#.##.#.####...#...#.##....##...#..#..##..###.####.....###......#.##.#
|
||||
.####.###..#..#...#..####.##.....#....#..#..##......###......#..##.#.##..#.
|
||||
###.....#.###..######...###.###.#...####.##..#.#.#.##.####.###.#...#.##....
|
||||
.###.#.##....##.#####.#.......#...#..#......##.######.####.####.###.###..#.
|
||||
##.##.#..#..###.###.#.#...#.##.#..#.###.##.#.#.###..#.#...##.##.###.#.#...#
|
||||
#####.#...##.#..##.##...###..#....####..##..##...#.#..##.#.#...##..###.##.#
|
||||
.#...#..#.#.#.#.#...###.........###..#..##....###.####..#.#.#####.#.#...#.#
|
||||
..#####.#..#...#.###...###..##......##..##.##....#####..##.####..#....##.#.
|
||||
#.#.....###.#.#.###.#####.##..######.#.##..########...##.....####....###...
|
||||
..##.#.#.#..#.#.##.##.#...###....##.#.#....##.####.#.#.#.######.#.#.#.####.
|
||||
#.##.#....##.#..####......##.#.##.#.#.#########.#.##...###.#........#.#....
|
||||
....##.#..##.##.##..#.#.####..####.#..####.##.##.#....#..#.#..#####..###...
|
||||
.##.#....###....###.#..##..####.#.#....####.###.#..#.###...#...#..##.######
|
||||
.......#.#..##.#..###.#.####..####.##..#.##.####.#.###..#.#.####.#.###...##
|
||||
####.#.#...##......#..#.##.##.###.####..##...####..#...###...##.###...#...#
|
||||
#.#......#.#..#.##....##.#.##.#.#..#.#.##.##.#.##...##...#.#.#......#..#.#.
|
||||
..####.###...##..##.####....##.#.#.#.####..##..#.###.###...####..#.....#..#
|
||||
..###..#....##.#..####.#..####.##.#####..######.#...###....##.....#..##.#..
|
||||
####.###.#....###..#...#.#...#..##..#.#...#.##.##.####.##.##.##.##..#..#...
|
||||
..#.##.....#.#.#.###...#.####.#####.#.#...#.....###....##.#.##..####.#..#..
|
||||
#..##.####.####.##..##.####.#..##.#....##.#.##..###.###.####.#.##.##...##..
|
||||
#.#....#..######...##.##......#.#...#####.#.##.#..#####..#..##....#...#.###
|
||||
.#.#..##.#..##.#...##.#..####.##..#...##..#.#.#..##.####...#.......##..#.##
|
||||
#.#.#..####.##.#.#......###..##.##..#.#.##.##..##.###.#..####.#..##..###.##
|
||||
##.##.#......#.#..#.##.#..###.#.#....##..##.#..###..#........###..#.#..#.#.
|
||||
##..##..###..##.#.####.#..###.....#...####.##...#...##.######.#.#.##.#...#.
|
||||
.#..##..#...##......####...##.#.##...##.###.#.##..#..###.##.....#...#.##..#
|
||||
#..#....#...#...#...#..#.#.#..#.####....#.#.#...##.#...#..###..#####.#...#.
|
||||
.#....#..#.####.#####.#....######....#.#####....#.##.##.#...#.#....#.....#.
|
||||
#.#####..###.##.#.#..#..#...######...#.###.##.##...##.#.#.#.#.##.##...#..#.
|
||||
#.#..#.....####.##......##...##..###.#.##.#..#.#.##..#...##.##.##...#...##.
|
||||
####..##..##.##....##.##.####.....###.#####.###.#####....#####.#.#..##.#.##
|
||||
##.#..###.#.#.#######..#..##.#..#........#.##.#..#....##...#.####..#.#.....
|
||||
.#..#.######.#.###.########..#..####.##...#.#.####..##..##.#...##.#...#....
|
||||
#######...#...###.##.#.#.#....##.#..####.#.##..##.######..#.####..#..#..##.
|
||||
####.#..#####.......#...###.....##.#.########..#..#####.##.##.##.#.##.#...#
|
||||
####.#.....#.##.#.#...#####.###.####.####.####....#..#........#######.####.
|
||||
.#.#..#.#.#.#.##.###...##....##.#.#...#.#..####.#####..##...###.#.##.#..##.
|
||||
#.##.#.##.#...#..#..#..##....#.##.###...#.#..##....#....###.#..#.#####.#...
|
||||
.#.##..###.##..#######.#..#.##......###..####..##.##....#...#.#.#...#.###.#
|
||||
#..#..#.###.##......#...###.#.#..##...##.#..#.#.#.####.#....#.###..####..##
|
||||
...##.#..#...#.#.#.#...#.#...##.#..##..#...##..#..####.....##..###.#..##...
|
||||
#.####.##....####.#.#.##.##...###..#......#####.....###....#.###..##...#.##
|
||||
####..##..#..##..#...###.##..###.##..#.#..###...#...##.#.#..#...#..##.....#
|
||||
#.##.#.##.#....#.#...##.##...#.##......###...##.#.#...####....#.#...#...#.#
|
||||
#.#.##...####...##.###.#######.#.##..#.#..###..##.##.##....#.....#..#.#....
|
7
res/23/test-input.txt
Normal file
7
res/23/test-input.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
....#..
|
||||
..###.#
|
||||
#...#.#
|
||||
.#...##
|
||||
#.###..
|
||||
##.#.##
|
||||
.#..#..
|
171
src/days/d23.rs
Normal file
171
src/days/d23.rs
Normal file
|
@ -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<Vec<bool>> = contents
|
||||
.lines()
|
||||
.filter(|&line| line != "")
|
||||
.map(|line| {
|
||||
line.chars()
|
||||
.map(|c| match c {
|
||||
'.' => false,
|
||||
'#' => true,
|
||||
_ => std::unreachable!(),
|
||||
})
|
||||
.collect::<Vec<bool>>()
|
||||
})
|
||||
.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<Vec<bool>>) -> 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<Vec<bool>>, 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<Vec<bool>>) {
|
||||
for y in grid.iter() {
|
||||
for x in y.iter() {
|
||||
match *x {
|
||||
true => print!("#"),
|
||||
false => print!("."),
|
||||
}
|
||||
}
|
||||
println!()
|
||||
}
|
||||
println!();
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
pub mod days;
|
||||
|
||||
fn main() {
|
||||
days::d21::solve()
|
||||
days::d23::solve()
|
||||
//_all_days()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue