Reorganization

This commit is contained in:
Jan-Bulthuis 2024-12-06 19:00:58 +01:00
parent 02fe9aea97
commit 85ce1c4641
13 changed files with 49 additions and 34 deletions

4
.gitignore vendored
View File

@ -1,3 +1,3 @@
target/ */target/
input/ */input/
*.out *.out

View File

View File

@ -2,8 +2,8 @@ use std::collections::VecDeque;
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
type Input = ((usize, usize), Vec<Vec<u8>>); // type Input = ((usize, usize), Vec<Vec<u8>>);
// type Input = ((usize, usize), Vec<Vec<u8>>, Vec<Vec<[u8; 5]>>); type Input = ((usize, usize), Vec<Vec<u8>>, Vec<Vec<[u8; 5]>>);
const UP: u8 = 0; const UP: u8 = 0;
const DOWN: u8 = 1; const DOWN: u8 = 1;
@ -43,31 +43,31 @@ fn parse(input: &str) -> Input {
}) })
.collect(); .collect();
// let mut jumps = vec![vec![[u8::MAX; 5]; map[0].len()]; map.len()]; let mut jumps = vec![vec![[u8::MAX; 5]; map[0].len()]; map.len()];
// while let Some((x, y)) = stones.pop_front() { while let Some((x, y)) = stones.pop_front() {
// for dir in NEXT_DIR { for dir in NEXT_DIR {
// let mut d = 1; let mut d = 1;
// loop { loop {
// let pos = (x as isize - d * DIR[dir].0, y as isize - d * DIR[dir].1); let pos = (x as isize - d * DIR[dir].0, y as isize - d * DIR[dir].1);
// if pos.0 < 0 if pos.0 < 0
// || pos.0 >= map[0].len() as isize || pos.0 >= map[0].len() as isize
// || pos.1 < 0 || pos.1 < 0
// || pos.1 >= map.len() as isize || pos.1 >= map.len() as isize
// || map[pos.1 as usize][pos.0 as usize] == WALL || map[pos.1 as usize][pos.0 as usize] == WALL
// { {
// break; break;
// } }
// jumps[pos.1 as usize][pos.0 as usize][dir] = (d - 1) as u8; jumps[pos.1 as usize][pos.0 as usize][dir] = (d - 1) as u8;
// d += 1; d += 1;
// } }
// } }
// } }
// (pos, map, jumps) (pos, map, jumps)
(pos, map) // (pos, map)
} }
#[aoc(day6, part1)] #[aoc(day6, part1)]
@ -76,7 +76,6 @@ fn part1(input: &Input) -> u64 {
let w = input.1[0].len(); let w = input.1[0].len();
let mut map = input.1.clone(); let mut map = input.1.clone();
// let mut jumps = input.2.clone();
let mut pos = ((input.0).0 as isize, (input.0).1 as isize); let mut pos = ((input.0).0 as isize, (input.0).1 as isize);
let mut dir = UP as usize; let mut dir = UP as usize;
@ -108,7 +107,6 @@ fn part1(input: &Input) -> u64 {
} }
#[aoc(day6, part2)] #[aoc(day6, part2)]
fn part2(input: &Input) -> u64 { fn part2(input: &Input) -> u64 {
let h = input.1.len(); let h = input.1.len();
let w = input.1[0].len(); let w = input.1[0].len();
@ -116,6 +114,7 @@ fn part2(input: &Input) -> u64 {
let initial = ((input.0).0 as isize, (input.0).1 as isize); let initial = ((input.0).0 as isize, (input.0).1 as isize);
let mut map = input.1.clone(); let mut map = input.1.clone();
let jumps = &input.2;
let mut pos = initial; let mut pos = initial;
let mut dir = UP as usize; let mut dir = UP as usize;
@ -138,7 +137,7 @@ fn part2(input: &Input) -> u64 {
if next_pos != initial && map[next_pos.1 as usize][next_pos.0 as usize] == EMPTY { if next_pos != initial && map[next_pos.1 as usize][next_pos.0 as usize] == EMPTY {
map[next_pos.1 as usize][next_pos.0 as usize] = WALL; map[next_pos.1 as usize][next_pos.0 as usize] = WALL;
if check_loop(pos, dir, &mut map) { if check_loop(pos, next_pos, dir, &mut map, jumps) {
count += 1; count += 1;
} }
map[next_pos.1 as usize][next_pos.0 as usize] = PASSED; map[next_pos.1 as usize][next_pos.0 as usize] = PASSED;
@ -150,7 +149,13 @@ fn part2(input: &Input) -> u64 {
count count
} }
fn check_loop(mut pos: (isize, isize), mut dir: usize, map: &mut [Vec<u8>]) -> bool { fn check_loop(
mut pos: (isize, isize),
stone: (isize, isize),
mut dir: usize,
map: &mut [Vec<u8>],
jumps: &[Vec<[u8; 5]>],
) -> bool {
let h = map.len(); let h = map.len();
let w = map[0].len(); let w = map[0].len();
@ -171,12 +176,24 @@ fn check_loop(mut pos: (isize, isize), mut dir: usize, map: &mut [Vec<u8>]) -> b
turn = true; turn = true;
} }
let next_pos = (pos.0 + DIR[dir].0, pos.1 + DIR[dir].1); let next_pos = if (pos.0 == stone.0) || (pos.1 == stone.1) {
(pos.0 + DIR[dir].0, pos.1 + DIR[dir].1)
} else {
let d = jumps[pos.1 as usize][pos.0 as usize][dir] as isize;
if d == u8::MAX as isize {
changed.into_iter().for_each(|pos: (isize, isize)| {
map[pos.1 as usize][pos.0 as usize] &= !(UP | DOWN | LEFT | RIGHT);
});
return false;
}
(pos.0 + d * DIR[dir].0, pos.1 + d * DIR[dir].1)
};
if turn { if turn {
if map[next_pos.1 as usize][next_pos.0 as usize] & dir as u8 != 0 { if map[next_pos.1 as usize][next_pos.0 as usize] & dir as u8 != 0 {
changed.into_iter().for_each(|pos: (isize, isize)| { changed.into_iter().for_each(|pos: (isize, isize)| {
map[pos.1 as usize][pos.0 as usize] &= PASSED | WALL | EMPTY; map[pos.1 as usize][pos.0 as usize] &= !(UP | DOWN | LEFT | RIGHT);
}); });
return true; return true;
@ -190,7 +207,7 @@ fn check_loop(mut pos: (isize, isize), mut dir: usize, map: &mut [Vec<u8>]) -> b
} }
changed.into_iter().for_each(|pos: (isize, isize)| { changed.into_iter().for_each(|pos: (isize, isize)| {
map[pos.1 as usize][pos.0 as usize] &= PASSED | WALL | EMPTY; map[pos.1 as usize][pos.0 as usize] &= !(UP | DOWN | LEFT | RIGHT);
}); });
false false

1
run.sh
View File

@ -1 +0,0 @@
cargo test

View File

@ -1 +0,0 @@
# max_width = 1000