@@ -5,6 +5,11 @@ pub struct Day {
5
5
}
6
6
7
7
impl Puzzle for Day {
8
+ /// We're given a grid of characters and asked to count the number of occurrences of a string
9
+ /// in any direction.
10
+ ///
11
+ /// Time complexity: O(n*m)
12
+ /// Auxiliary space complexity: O(1)
8
13
fn solve_part_1 ( & self ) -> String {
9
14
let mut count = 0 ;
10
15
for row in 0 ..self . grid . len ( ) {
@@ -15,6 +20,11 @@ impl Puzzle for Day {
15
20
count. to_string ( )
16
21
}
17
22
23
+ /// We're given a grid of characters and asked to count the number of occurrences of a
24
+ /// particular pattern in the grid.
25
+ ///
26
+ /// Time complexity: O(n*m)
27
+ /// Auxiliary space complexity: O(1)
18
28
fn solve_part_2 ( & self ) -> String {
19
29
let mut count = 0 ;
20
30
for row in 0 ..self . grid . len ( ) {
@@ -28,67 +38,55 @@ impl Puzzle for Day {
28
38
}
29
39
}
30
40
31
- static XMAS : & str = "XMAS" ;
32
-
33
41
fn count_xmas ( grid : & [ Vec < char > ] , row : usize , col : usize ) -> i32 {
34
- let mut count = 0 ;
35
- for drow in -1 ..=1 {
36
- for dcol in -1 ..=1 {
37
- if drow == 0 && dcol == 0 {
38
- continue ;
39
- }
40
- if is_xmas ( grid, row as isize , col as isize , ( drow, dcol) ) {
41
- count += 1 ;
42
- }
43
- }
44
- }
45
- count
42
+ const DIRECTIONS : [ ( i8 , i8 ) ; 8 ] = [
43
+ ( -1 , -1 ) ,
44
+ ( -1 , 0 ) ,
45
+ ( -1 , 1 ) ,
46
+ ( 0 , -1 ) ,
47
+ ( 0 , 1 ) ,
48
+ ( 1 , -1 ) ,
49
+ ( 1 , 0 ) ,
50
+ ( 1 , 1 ) ,
51
+ ] ;
52
+ DIRECTIONS
53
+ . iter ( )
54
+ . filter ( |& & dir| is_xmas ( grid, row as isize , col as isize , dir) )
55
+ . count ( ) as i32
46
56
}
47
57
48
58
fn is_xmas ( grid : & [ Vec < char > ] , mut row : isize , mut col : isize , dir : ( i8 , i8 ) ) -> bool {
49
- let mut i = 0 ;
50
- while in_bounds ( grid , row , col ) && i < XMAS . len ( ) {
51
- if grid[ row as usize ] [ col as usize ] != XMAS . chars ( ) . nth ( i ) . unwrap ( ) {
59
+ const XMAS : & str = "XMAS" ;
60
+ for expected in XMAS . chars ( ) {
61
+ if ! in_bounds ( grid, row , col ) || grid [ row as usize ] [ col as usize ] != expected {
52
62
return false ;
53
63
}
54
64
row += dir. 0 as isize ;
55
65
col += dir. 1 as isize ;
56
- i += 1 ;
57
66
}
58
- i == 4
67
+ true
59
68
}
60
69
61
70
fn is_x_mas ( grid : & [ Vec < char > ] , row : isize , col : isize ) -> bool {
62
- if grid[ row as usize ] [ col as usize ] != 'A'
63
- || !in_bounds ( grid, row - 1 , col - 1 )
64
- || !in_bounds ( grid, row - 1 , col + 1 )
65
- || !in_bounds ( grid, row + 1 , col - 1 )
66
- || !in_bounds ( grid, row + 1 , col + 1 )
67
- {
71
+ if grid[ row as usize ] [ col as usize ] != 'A' {
68
72
return false ;
69
73
}
70
- let up_left = grid[ ( row - 1 ) as usize ] [ ( col - 1 ) as usize ] ;
71
- let up_right = grid[ ( row - 1 ) as usize ] [ ( col + 1 ) as usize ] ;
72
- let down_left = grid[ ( row + 1 ) as usize ] [ ( col - 1 ) as usize ] ;
73
- let down_right = grid[ ( row + 1 ) as usize ] [ ( col + 1 ) as usize ] ;
74
- let mut matches = 0 ;
75
- if up_left == 'M' && down_right == 'S' {
76
- matches += 1 ;
77
- }
78
- if up_left == 'S' && down_right == 'M' {
79
- matches += 1 ;
80
- }
81
- if up_right == 'M' && down_left == 'S' {
82
- matches += 1 ;
83
- }
84
- if up_right == 'S' && down_left == 'M' {
85
- matches += 1 ;
86
- }
87
- matches == 2
74
+ let diagonals = [
75
+ ( ( row - 1 , col - 1 ) , ( row + 1 , col + 1 ) ) ,
76
+ ( ( row - 1 , col + 1 ) , ( row + 1 , col - 1 ) ) ,
77
+ ] ;
78
+ diagonals. iter ( ) . all ( |& ( loc1, loc2) | {
79
+ if !in_bounds ( grid, loc1. 0 , loc1. 1 ) || !in_bounds ( grid, loc2. 0 , loc2. 1 ) {
80
+ return false ;
81
+ }
82
+ let val1 = grid[ loc1. 0 as usize ] [ loc1. 1 as usize ] ;
83
+ let val2 = grid[ loc2. 0 as usize ] [ loc2. 1 as usize ] ;
84
+ ( val1 == 'M' && val2 == 'S' ) || ( val1 == 'S' && val2 == 'M' )
85
+ } )
88
86
}
89
87
90
88
fn in_bounds ( grid : & [ Vec < char > ] , row : isize , col : isize ) -> bool {
91
- row >= 0 && row < grid. len ( ) as isize && col >= 0 && col < grid[ 0 ] . len ( ) as isize
89
+ row >= 0 && ( row as usize ) < grid. len ( ) && col >= 0 && ( col as usize ) < grid[ 0 ] . len ( )
92
90
}
93
91
94
92
impl Day {
0 commit comments