Skip to content

Commit ba05334

Browse files
authored
Merge pull request #350 from paulcacheux/day6-2024
Day6 2024
2 parents be01c92 + 71ee2d3 commit ba05334

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

inputs/2024/day6_test.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
....#.....
2+
.........#
3+
..........
4+
..#.......
5+
.......#..
6+
..........
7+
.#..^.....
8+
........#.
9+
#.........
10+
......#...

results/2024.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@
2323
"day": 5,
2424
"part1": "6041",
2525
"part2": "4884"
26+
},
27+
{
28+
"day": 5,
29+
"part1": "4665",
30+
"part2": "1688"
2631
}
2732
]

src/aoc2024/day6.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
use crate::aoc2024::Aoc2024;
2+
use crate::grid::Grid;
3+
use crate::traits::days::Day6;
4+
use crate::traits::ParseInput;
5+
use crate::traits::Solution;
6+
7+
impl ParseInput<Day6> for Aoc2024 {
8+
type Parsed = (Grid<bool>, usize, usize);
9+
10+
fn parse_input(input: &str) -> Self::Parsed {
11+
let mut sx = 0;
12+
let mut sy = 0;
13+
let grid = Grid::parse(input, |c| c);
14+
15+
let mut bgrid = Grid::new(grid.width, grid.height, false);
16+
grid.iter().for_each(|(x, y, &c)| {
17+
let val = match c {
18+
'#' => true,
19+
'.' => false,
20+
'^' => {
21+
sx = x;
22+
sy = y;
23+
false
24+
}
25+
_ => unreachable!(),
26+
};
27+
bgrid.set(x, y, val);
28+
});
29+
(bgrid, sx, sy)
30+
}
31+
}
32+
33+
impl Solution<Day6> for Aoc2024 {
34+
type Part1Output = usize;
35+
type Part2Output = usize;
36+
37+
fn part1((grid, sx, sy): &(Grid<bool>, usize, usize)) -> usize {
38+
compute_path_size(grid, *sx, *sy).unwrap()
39+
}
40+
41+
fn part2((grid, sx, sy): &(Grid<bool>, usize, usize)) -> usize {
42+
let mut res = 0;
43+
let mut ngrid = grid.clone();
44+
for y in 0..grid.height {
45+
for x in 0..grid.width {
46+
if (x, y) == (*sx, *sy) {
47+
continue;
48+
}
49+
50+
if *grid.get(x, y) {
51+
continue;
52+
}
53+
54+
ngrid.set(x, y, true);
55+
if compute_path_size(&ngrid, *sx, *sy).is_none() {
56+
res += 1;
57+
}
58+
ngrid.set(x, y, false);
59+
}
60+
}
61+
62+
res
63+
}
64+
}
65+
66+
const DIRECTIONS: [(isize, isize); 4] = [(0, -1), (1, 0), (0, 1), (-1, 0)];
67+
68+
fn compute_path_size(grid: &Grid<bool>, sx: usize, sy: usize) -> Option<usize> {
69+
let (mut x, mut y) = (sx, sy);
70+
let mut didx = 0;
71+
let mut positions = Grid::new(grid.width, grid.height, [false; 4]);
72+
73+
loop {
74+
let pos = positions.get_mut(x, y);
75+
if pos[didx] {
76+
return None;
77+
}
78+
pos[didx] = true;
79+
80+
let (dx, dy) = DIRECTIONS[didx];
81+
if let Some((nx, ny)) = offset_pair(grid, x, y, dx, dy) {
82+
if *grid.get(nx, ny) {
83+
didx = (didx + 1) % 4;
84+
} else {
85+
x = nx;
86+
y = ny;
87+
}
88+
} else {
89+
break;
90+
}
91+
}
92+
93+
Some(
94+
positions
95+
.iter()
96+
.filter(|(_, _, &v)| v.into_iter().any(|b| b))
97+
.count(),
98+
)
99+
}
100+
101+
#[inline]
102+
fn offset_pair(
103+
grid: &Grid<bool>,
104+
x: usize,
105+
y: usize,
106+
dx: isize,
107+
dy: isize,
108+
) -> Option<(usize, usize)> {
109+
let nx = offset(x, dx, grid.width);
110+
let ny = offset(y, dy, grid.height);
111+
112+
match (nx, ny) {
113+
(Some(nx), Some(ny)) => Some((nx, ny)),
114+
_ => None,
115+
}
116+
}
117+
118+
#[inline]
119+
fn offset(base: usize, offset: isize, max: usize) -> Option<usize> {
120+
let res = base as isize + offset;
121+
122+
if res < 0 || res >= max as isize {
123+
return None;
124+
}
125+
126+
Some(res as usize)
127+
}

src/aoc2024/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod day2;
88
pub mod day3;
99
pub mod day4;
1010
pub mod day5;
11+
pub mod day6;
1112

1213
pub fn run_solution_for_day(day: u32, input: &str, results: Option<Results>) -> Option<TimingData> {
1314
let r = results
@@ -20,6 +21,7 @@ pub fn run_solution_for_day(day: u32, input: &str, results: Option<Results>) ->
2021
3 => run::<Aoc2024, Day3>(input, r),
2122
4 => run::<Aoc2024, Day4>(input, r),
2223
5 => run::<Aoc2024, Day5>(input, r),
24+
6 => run::<Aoc2024, Day6>(input, r),
2325
_ => return None,
2426
};
2527
Some(elapsed)

0 commit comments

Comments
 (0)