Skip to content

Commit b77172e

Browse files
committed
Add day4 part2
1 parent a73abc6 commit b77172e

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/day4.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,37 @@ pub fn part1(input: &[Vec<char>]) -> usize {
5252
pub fn part2(input: &[Vec<char>]) -> usize {
5353
let mut num = 0;
5454
let directions = [
55-
(1, 1), // down-right
56-
(-1, -1), // up-left
57-
(1, -1), // down-left
58-
(-1, 1), // up-right
55+
((1, 1), (-1, -1)), // down-right and up-left
56+
((1, -1), (-1, 1)), // down-left and up-right
5957
];
6058

6159
for y in 0..input.len() {
6260
for x in 0..input[y].len() {
63-
for &(dy, dx) in &directions {
64-
if (0..5).all(|i| {
65-
let ny = y as isize + i * dy;
66-
let nx = x as isize + i * dx;
67-
ny >= 0
68-
&& ny < input.len() as isize
69-
&& nx >= 0
70-
&& nx < input[y].len() as isize
71-
&& match i {
72-
0 => input[ny as usize][nx as usize] == 'A',
73-
1 => input[ny as usize][nx as usize] == 'S',
74-
2 => input[ny as usize][nx as usize] == 'M',
75-
_ => false,
76-
}
77-
}) {
61+
if input[y][x] == 'A' {
62+
let mut mas = 0;
63+
for &((dy1, dx1), (dy2, dx2)) in &directions {
64+
let ny1 = y as isize + dy1;
65+
let nx1 = x as isize + dx1;
66+
let ny2 = y as isize + dy2;
67+
let nx2 = x as isize + dx2;
68+
69+
if ny1 >= 0
70+
&& ny1 < input.len() as isize
71+
&& nx1 >= 0
72+
&& nx1 < input[y].len() as isize
73+
&& ny2 >= 0
74+
&& ny2 < input.len() as isize
75+
&& nx2 >= 0
76+
&& nx2 < input[y].len() as isize
77+
&& ((input[ny1 as usize][nx1 as usize] == 'M'
78+
&& input[ny2 as usize][nx2 as usize] == 'S')
79+
|| (input[ny1 as usize][nx1 as usize] == 'S'
80+
&& input[ny2 as usize][nx2 as usize] == 'M'))
81+
{
82+
mas += 1;
83+
}
84+
}
85+
if mas == 2 {
7886
num += 1;
7987
}
8088
}

0 commit comments

Comments
 (0)