Skip to content

Commit 411a2d7

Browse files
committed
Year 2024 Day 6
1 parent ef4b192 commit 411a2d7

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8282
| 3 | [Mull It Over](https://adventofcode.com/2024/day/3) | [Source](src/year2024/day03.rs) | 8 |
8383
| 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [Source](src/year2024/day04.rs) | 77 |
8484
| 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [Source](src/year2024/day05.rs) | 18 |
85+
| 6 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | [Source](src/year2024/day06.rs) | 5500 |
8586

8687
## 2023
8788

benches/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,5 @@ mod year2024 {
297297
benchmark!(year2024, day03);
298298
benchmark!(year2024, day04);
299299
benchmark!(year2024, day05);
300+
benchmark!(year2024, day06);
300301
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,5 @@ pub mod year2024 {
296296
pub mod day03;
297297
pub mod day04;
298298
pub mod day05;
299+
pub mod day06;
299300
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,5 +366,6 @@ fn year2024() -> Vec<Solution> {
366366
solution!(year2024, day03),
367367
solution!(year2024, day04),
368368
solution!(year2024, day05),
369+
solution!(year2024, day06),
369370
]
370371
}

src/year2024/day06.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//! # Guard Gallivant
2+
use crate::util::grid::*;
3+
use crate::util::hash::*;
4+
use crate::util::point::*;
5+
6+
pub fn parse(input: &str) -> Grid<u8> {
7+
Grid::parse(input)
8+
}
9+
10+
pub fn part1(grid: &Grid<u8>) -> usize {
11+
let mut grid = grid.clone();
12+
let mut position = grid.find(b'^').unwrap();
13+
let mut direction = UP;
14+
let mut result = 1;
15+
16+
while grid.contains(position + direction) {
17+
if grid[position + direction] == b'#' {
18+
direction = direction.clockwise();
19+
}
20+
position += direction;
21+
22+
if grid[position] != b'^' {
23+
result += 1;
24+
}
25+
grid[position] = b'^';
26+
}
27+
28+
result
29+
}
30+
31+
pub fn part2(grid: &Grid<u8>) -> usize {
32+
let mut grid = grid.clone();
33+
let mut position = grid.find(b'^').unwrap();
34+
let mut direction = UP;
35+
let mut result = 0;
36+
37+
let mut seen = FastSet::with_capacity(5_000);
38+
39+
while grid.contains(position + direction) {
40+
if grid[position + direction] == b'#' {
41+
direction = direction.clockwise();
42+
}
43+
44+
let next = position + direction;
45+
46+
if grid[next] == b'.' {
47+
grid[next] = b'#';
48+
49+
if check_cycle(&grid, &mut seen, position, direction) {
50+
result += 1;
51+
}
52+
53+
grid[next] = b'^';
54+
seen.clear();
55+
}
56+
57+
position = next;
58+
}
59+
60+
result
61+
}
62+
63+
fn check_cycle(
64+
grid: &Grid<u8>,
65+
seen: &mut FastSet<(Point, Point)>,
66+
mut position: Point,
67+
mut direction: Point,
68+
) -> bool {
69+
while grid.contains(position + direction) {
70+
if grid[position + direction] == b'#' {
71+
if !seen.insert((position, direction)) {
72+
return true;
73+
}
74+
direction = direction.clockwise();
75+
continue;
76+
}
77+
78+
position += direction;
79+
}
80+
81+
false
82+
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,5 @@ mod year2024 {
286286
mod day03_test;
287287
mod day04_test;
288288
mod day05_test;
289+
mod day06_test;
289290
}

tests/year2024/day06_test.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use aoc::year2024::day06::*;
2+
3+
const EXAMPLE: &str = "\
4+
....#.....
5+
.........#
6+
..........
7+
..#.......
8+
.......#..
9+
..........
10+
.#..^.....
11+
........#.
12+
#.........
13+
......#...";
14+
15+
#[test]
16+
fn part1_test() {
17+
let input = parse(EXAMPLE);
18+
assert_eq!(part1(&input), 41);
19+
}
20+
21+
#[test]
22+
fn part2_test() {
23+
let input = parse(EXAMPLE);
24+
assert_eq!(part2(&input), 6);
25+
}

0 commit comments

Comments
 (0)