Skip to content

Commit 1352fba

Browse files
committed
fix part 1
1 parent a7d5112 commit 1352fba

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

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": ""
2631
}
2732
]

src/aoc2024/day6.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use crate::aoc2024::Aoc2024;
24
use crate::grid::Grid;
35
use crate::traits::days::Day6;
@@ -31,16 +33,32 @@ impl ParseInput<Day6> for Aoc2024 {
3133
}
3234

3335
impl Solution<Day6> for Aoc2024 {
34-
type Part1Output = u32;
36+
type Part1Output = usize;
3537
type Part2Output = u32;
3638

37-
fn part1((grid, sx, sy): &(Grid<bool>, usize, usize)) -> u32 {
39+
fn part1((grid, sx, sy): &(Grid<bool>, usize, usize)) -> usize {
40+
const DIRECTIONS: [(isize, isize); 4] = [(0, -1), (1, 0), (0, 1), (-1, 0)];
41+
3842
let (mut x, mut y) = (*sx, *sy);
39-
let (mut dx, mut dy) = (0, -1);
43+
let mut didx = 0;
44+
let mut positions = HashSet::new();
4045

4146
loop {
42-
let (nx, ny) = (x.offset(dx), y.offset(dy));
47+
positions.insert((x, y));
48+
let (dx, dy) = DIRECTIONS[didx];
49+
if let Some((nx, ny)) = offset_pair(grid, x, y, dx, dy) {
50+
if *grid.get(nx, ny) {
51+
didx = (didx + 1) % 4;
52+
} else {
53+
x = nx;
54+
y = ny;
55+
}
56+
} else {
57+
break;
58+
}
4359
}
60+
61+
positions.len()
4462
}
4563

4664
fn part2(_input: &(Grid<bool>, usize, usize)) -> u32 {
@@ -49,9 +67,13 @@ impl Solution<Day6> for Aoc2024 {
4967
}
5068

5169
fn offset_pair(grid: &Grid<bool>, x: usize, y: usize, dx: isize, dy: isize) -> Option<(usize, usize)> {
52-
let nx = offset(x, dx, grid.width) else { return None };
53-
let ny = offset(y, dy,grid.height) else { return None };
54-
(nx, ny)
70+
let nx = offset(x, dx, grid.width);
71+
let ny = offset(y, dy,grid.height);
72+
73+
match (nx, ny) {
74+
(Some(nx), Some(ny)) => Some((nx, ny)),
75+
_ => None,
76+
}
5577
}
5678

5779
fn offset(base: usize, offset: isize, max: usize) -> Option<usize> {

0 commit comments

Comments
 (0)