Skip to content

Commit 55a073a

Browse files
committed
Add ordinal directions to aoc_lib
1 parent d9ec36e commit 55a073a

File tree

12 files changed

+85
-78
lines changed

12 files changed

+85
-78
lines changed

aoc_2021/src/day_15.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::BinaryHeap;
33
use hashbrown::HashMap;
44
use nd_vec::{vector, Vector};
55

6-
use aoc_lib::{direction::Direction, matrix::Matrix};
6+
use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
77
use common::{solution, Answer};
88

99
solution!("Chiton", 15);

aoc_2023/src/day_10.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashSet;
22

33
use nd_vec::{vector, Vec2};
44

5-
use aoc_lib::direction::Direction;
5+
use aoc_lib::direction::cardinal::Direction;
66
use common::{solution, Answer};
77

88
type Pos = Vec2<usize>;

aoc_2023/src/day_16.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashSet;
22

3-
use aoc_lib::{direction::Direction, matrix::Matrix};
3+
use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
44
use common::{solution, Answer};
55
use nd_vec::{vector, Vec2};
66

aoc_2023/src/day_17.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
collections::{BinaryHeap, HashMap},
44
};
55

6-
use aoc_lib::{direction::Direction, matrix::Matrix};
6+
use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
77
use common::{solution, Answer};
88
use nd_vec::{vector, Vec2};
99

aoc_2023/src/day_18.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use aoc_lib::direction::Direction;
1+
use aoc_lib::direction::cardinal::Direction;
22
use common::{solution, Answer};
33
use nd_vec::vector;
44

aoc_2023/src/day_21.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashSet;
22

3-
use aoc_lib::{direction::Direction, matrix::Matrix};
3+
use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
44
use common::{solution, Answer};
55
use nd_vec::{vector, Vec2};
66

aoc_2023/src/day_23.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
convert::identity,
44
};
55

6-
use aoc_lib::{direction::Direction, matrix::Matrix};
6+
use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
77
use common::{solution, Answer};
88
use nd_vec::{vector, Vec2};
99

aoc_2024/src/day_04.rs

Lines changed: 28 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1-
use aoc_lib::matrix::Matrix;
1+
use std::convert::identity;
2+
3+
use aoc_lib::{direction::ordinal::Direction, matrix::Matrix};
24
use common::{solution, Answer};
3-
use nd_vec::{vector, Vector};
5+
use nd_vec::vector;
46

57
solution!("Ceres Search", 4);
68

79
fn part_a(input: &str) -> Answer {
8-
let matrix = Matrix::new_chars(input, |x| x);
10+
let matrix = Matrix::new_chars(input, identity);
911
let mut count = 0;
1012

1113
for y in 0..matrix.size.y() {
1214
for x in 0..matrix.size.x() {
1315
let start = vector!(x, y);
14-
for dir in Direction::ALL {
15-
let mut pos = start.clone();
16-
let mut word = String::new();
17-
for _ in 0..4 {
18-
if let Some(&chr) = matrix.get(pos) {
19-
word.push(chr);
20-
} else {
21-
break;
22-
}
23-
24-
let Some(next) = dir.try_advance(pos) else {
25-
break;
26-
};
16+
if *matrix.get(start).unwrap() != 'X' {
17+
continue;
18+
}
19+
20+
'outer: for dir in Direction::ALL {
21+
let mut pos = start;
22+
for expected in ['M', 'A', 'S'] {
23+
let next = dir.try_advance(pos);
24+
let Some(next) = next else { continue 'outer };
2725
pos = next;
28-
}
2926

30-
if word == "XMAS" {
31-
count += 1;
27+
if Some(&expected) != matrix.get(pos) {
28+
continue 'outer;
29+
};
3230
}
31+
32+
count += 1;
3333
}
3434
}
3535
}
3636

3737
count.into()
3838
}
3939

40+
/// The directions to advance from the middle 'A' for each MAS instance.
41+
const MAS_DIRECTIONS: [[Direction; 2]; 2] = [
42+
[Direction::NorthEast, Direction::SouthWest],
43+
[Direction::SouthEast, Direction::NorthWest],
44+
];
45+
4046
fn part_b(input: &str) -> Answer {
41-
let matrix = Matrix::new_chars(input, |x| x);
47+
let matrix = Matrix::new_chars(input, identity);
4248
let mut count = 0;
4349

4450
for y in 0..matrix.size.y() {
@@ -48,19 +54,10 @@ fn part_b(input: &str) -> Answer {
4854
continue;
4955
}
5056

51-
let directions = [
52-
[Direction::NorthEast, Direction::SouthWest],
53-
[Direction::SouthEast, Direction::NorthWest],
54-
// should have a 'M' and a 'S'
55-
];
56-
57-
for mas in directions {
57+
for mas in MAS_DIRECTIONS {
5858
let (mut m, mut s) = (false, false);
5959
for dir in mas {
60-
let Some(pos) = dir.try_advance(start) else {
61-
continue 'outer;
62-
};
63-
let Some(&chr) = matrix.get(pos) else {
60+
let Some(&chr) = dir.try_advance(start).and_then(|x| matrix.get(x)) else {
6461
continue 'outer;
6562
};
6663

@@ -80,45 +77,6 @@ fn part_b(input: &str) -> Answer {
8077
count.into()
8178
}
8279

83-
#[derive(Debug)]
84-
enum Direction {
85-
North,
86-
NorthEast,
87-
East,
88-
SouthEast,
89-
South,
90-
SouthWest,
91-
West,
92-
NorthWest,
93-
}
94-
95-
impl Direction {
96-
pub const ALL: [Direction; 8] = [
97-
Direction::North,
98-
Direction::NorthEast,
99-
Direction::East,
100-
Direction::SouthEast,
101-
Direction::South,
102-
Direction::SouthWest,
103-
Direction::West,
104-
Direction::NorthWest,
105-
];
106-
107-
pub fn try_advance(&self, pos: Vector<usize, 2>) -> Option<Vector<usize, 2>> {
108-
Some(match self {
109-
Self::North => vector!(pos.x(), pos.y() + 1),
110-
Self::NorthEast => vector!(pos.x() + 1, pos.y() + 1),
111-
Self::East => vector!(pos.x() + 1, pos.y()),
112-
Self::SouthEast if pos.y() > 0 => vector!(pos.x() + 1, pos.y() - 1),
113-
Self::South if pos.y() > 0 => vector!(pos.x(), pos.y() - 1),
114-
Self::SouthWest if pos.x() > 0 && pos.y() > 0 => vector!(pos.x() - 1, pos.y() - 1),
115-
Self::West if pos.x() > 0 => vector!(pos.x() - 1, pos.y()),
116-
Self::NorthWest if pos.x() > 0 => vector!(pos.x() - 1, pos.y() + 1),
117-
_ => return None,
118-
})
119-
}
120-
}
121-
12280
#[cfg(test)]
12381
mod test {
12482
use indoc::indoc;
File renamed without changes.

aoc_lib/src/direction/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod cardinal;
2+
pub mod ordinal;

0 commit comments

Comments
 (0)