Skip to content

Commit b9ef79b

Browse files
committed
Update 04
1 parent 09d595c commit b9ef79b

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ Solutions for [Advent of Code](https://adventofcode.com/2024) in [Rust](https://
1111

1212
| Day | Part 1 | Part 2 |
1313
| :---: | :---: | :---: |
14-
| [Day 1](./src/bin/01.rs) | `130.7µs` | `187.6µs` |
15-
| [Day 2](./src/bin/02.rs) | `275.6µs` | `572.8µs` |
16-
| [Day 3](./src/bin/03.rs) | `534.6µs` | `659.3µs` |
14+
| [Day 1](./src/bin/01.rs) | `63.5µs` | `84.0µs` |
15+
| [Day 2](./src/bin/02.rs) | `873.6µs` | `976.9µs` |
16+
| [Day 3](./src/bin/03.rs) | `125.8µs` | `120.7µs` |
17+
| [Day 4](./src/bin/04.rs) | `280.3µs` | `85.4µs` |
1718

18-
**Total: 2.36ms**
19+
**Total: 2.61ms**
1920
<!--- benchmarking table --->
2021

2122

src/bin/04.rs

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
advent_of_code::solution!(4);
22

3-
const DIRECTIONS: [(isize, isize); 8] = [
3+
const DIRECTIONS: [(i32, i32); 8] = [
44
(0, 1),
55
(1, 0),
66
(1, 1),
@@ -11,57 +11,46 @@ const DIRECTIONS: [(isize, isize); 8] = [
1111
(-1, 1),
1212
];
1313

14-
fn count_xmas_occurrences(grid: Vec<Vec<char>>, target: &str) -> usize {
15-
let rows = grid.len();
16-
let cols = grid.len();
17-
let mut count = 0;
18-
19-
for i in 0..rows {
20-
for j in 0..cols {
21-
for &(dx, dy) in &DIRECTIONS {
22-
count += check_direction(&grid, i, j, dx, dy, target);
14+
fn count_xmas_occurrences(grid: Vec<Vec<char>>, word: &str) -> usize {
15+
let mut result: u32 = 0;
16+
let word_length: usize = word.chars().count();
17+
18+
for y in 0..grid.len() {
19+
for x in 0..grid.len() {
20+
if grid[y][x] == word.chars().next().unwrap() {
21+
for &d in &DIRECTIONS {
22+
let mut found = true;
23+
24+
for j in 1..word_length {
25+
let nx = x as i32 + d.0 * j as i32;
26+
let ny = y as i32 + d.1 * j as i32;
27+
28+
if nx < 0
29+
|| ny < 0
30+
|| nx >= grid[0].len() as i32
31+
|| ny >= grid.len() as i32
32+
|| grid[ny as usize][nx as usize] != word.chars().nth(j).unwrap()
33+
{
34+
found = false; // If any character does not match, break
35+
break;
36+
}
37+
}
38+
39+
if found {
40+
result += 1;
41+
}
42+
}
2343
}
2444
}
2545
}
2646

27-
count
47+
result as usize
2848
}
2949

30-
fn check_direction(
31-
grid: &[Vec<char>], //&Vec<Vec<char>>,
32-
start_x: usize,
33-
start_y: usize,
34-
dx: isize,
35-
dy: isize,
36-
target: &str,
37-
) -> usize {
38-
let mut x = start_x as isize;
39-
let mut y = start_y as isize;
40-
let target_len = target.len();
41-
let mut chars = Vec::new();
42-
43-
for _ in 0..target_len {
44-
// Check we are within table
45-
if x < 0 || y < 0 || x >= grid.len() as isize || y >= grid[0].len() as isize {
46-
return 0;
47-
}
48-
chars.push(grid[x as usize][y as usize]);
49-
x += dx;
50-
y += dy;
51-
}
52-
53-
if chars.iter().collect::<String>() == target {
54-
return 1;
55-
}
56-
57-
0
58-
}
59-
60-
pub fn part_one(input: &str) -> Option<u32> {
50+
pub fn part_one(input: &str) -> Option<usize> {
6151
let chars = input.lines().map(|line| line.chars().collect()).collect();
6252
let result = count_xmas_occurrences(chars, "XMAS");
63-
64-
Some(result as u32)
53+
Some(result)
6554
}
6655

6756
pub fn part_two(input: &str) -> Option<u32> {

0 commit comments

Comments
 (0)