diff --git a/.gitignore b/.gitignore index c9e2ee2..e58bf05 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -target/ -input/ +*/target/ +*/input/ *.out \ No newline at end of file diff --git a/Cargo.lock b/aoc_2024/Cargo.lock similarity index 100% rename from Cargo.lock rename to aoc_2024/Cargo.lock diff --git a/Cargo.toml b/aoc_2024/Cargo.toml similarity index 100% rename from Cargo.toml rename to aoc_2024/Cargo.toml diff --git a/bacon.toml b/aoc_2024/bacon.toml similarity index 100% rename from bacon.toml rename to aoc_2024/bacon.toml diff --git a/src/day1.rs b/aoc_2024/src/day1.rs similarity index 100% rename from src/day1.rs rename to aoc_2024/src/day1.rs diff --git a/src/day2.rs b/aoc_2024/src/day2.rs similarity index 100% rename from src/day2.rs rename to aoc_2024/src/day2.rs diff --git a/src/day3.rs b/aoc_2024/src/day3.rs similarity index 100% rename from src/day3.rs rename to aoc_2024/src/day3.rs diff --git a/src/day4.rs b/aoc_2024/src/day4.rs similarity index 100% rename from src/day4.rs rename to aoc_2024/src/day4.rs diff --git a/src/day5.rs b/aoc_2024/src/day5.rs similarity index 100% rename from src/day5.rs rename to aoc_2024/src/day5.rs diff --git a/src/day6.rs b/aoc_2024/src/day6.rs similarity index 71% rename from src/day6.rs rename to aoc_2024/src/day6.rs index 5c0c80c..27843a5 100644 --- a/src/day6.rs +++ b/aoc_2024/src/day6.rs @@ -2,8 +2,8 @@ use std::collections::VecDeque; use aoc_runner_derive::{aoc, aoc_generator}; -type Input = ((usize, usize), Vec>); -// type Input = ((usize, usize), Vec>, Vec>); +// type Input = ((usize, usize), Vec>); +type Input = ((usize, usize), Vec>, Vec>); const UP: u8 = 0; const DOWN: u8 = 1; @@ -43,31 +43,31 @@ fn parse(input: &str) -> Input { }) .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() { - // for dir in NEXT_DIR { - // let mut d = 1; - // loop { - // let pos = (x as isize - d * DIR[dir].0, y as isize - d * DIR[dir].1); - // if pos.0 < 0 - // || pos.0 >= map[0].len() as isize - // || pos.1 < 0 - // || pos.1 >= map.len() as isize - // || map[pos.1 as usize][pos.0 as usize] == WALL - // { - // break; - // } + while let Some((x, y)) = stones.pop_front() { + for dir in NEXT_DIR { + let mut d = 1; + loop { + let pos = (x as isize - d * DIR[dir].0, y as isize - d * DIR[dir].1); + if pos.0 < 0 + || pos.0 >= map[0].len() as isize + || pos.1 < 0 + || pos.1 >= map.len() as isize + || map[pos.1 as usize][pos.0 as usize] == WALL + { + 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) + (pos, map, jumps) + // (pos, map) } #[aoc(day6, part1)] @@ -76,7 +76,6 @@ fn part1(input: &Input) -> u64 { let w = input.1[0].len(); 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 dir = UP as usize; @@ -108,7 +107,6 @@ fn part1(input: &Input) -> u64 { } #[aoc(day6, part2)] - fn part2(input: &Input) -> u64 { let h = input.1.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 mut map = input.1.clone(); + let jumps = &input.2; let mut pos = initial; 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 { 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; } map[next_pos.1 as usize][next_pos.0 as usize] = PASSED; @@ -150,7 +149,13 @@ fn part2(input: &Input) -> u64 { count } -fn check_loop(mut pos: (isize, isize), mut dir: usize, map: &mut [Vec]) -> bool { +fn check_loop( + mut pos: (isize, isize), + stone: (isize, isize), + mut dir: usize, + map: &mut [Vec], + jumps: &[Vec<[u8; 5]>], +) -> bool { let h = map.len(); let w = map[0].len(); @@ -171,12 +176,24 @@ fn check_loop(mut pos: (isize, isize), mut dir: usize, map: &mut [Vec]) -> b 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 map[next_pos.1 as usize][next_pos.0 as usize] & dir as u8 != 0 { 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; @@ -190,7 +207,7 @@ fn check_loop(mut pos: (isize, isize), mut dir: usize, map: &mut [Vec]) -> b } 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 diff --git a/src/lib.rs b/aoc_2024/src/lib.rs similarity index 100% rename from src/lib.rs rename to aoc_2024/src/lib.rs diff --git a/run.sh b/run.sh deleted file mode 100755 index e74ce90..0000000 --- a/run.sh +++ /dev/null @@ -1 +0,0 @@ -cargo test \ No newline at end of file diff --git a/rustfmt.toml b/rustfmt.toml deleted file mode 100644 index 4194bf2..0000000 --- a/rustfmt.toml +++ /dev/null @@ -1 +0,0 @@ -# max_width = 1000