Skip to content

Commit dfb46f9

Browse files
committed
Neater
1 parent 6e30bf3 commit dfb46f9

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

src/year2024/day14.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! we combine the two times into a single time mod 10403 that is the answer.
1515
use crate::util::iter::*;
1616
use crate::util::parse::*;
17+
use std::cmp::Ordering::*;
1718

1819
type Robot = [usize; 4];
1920

@@ -28,34 +29,22 @@ pub fn parse(input: &str) -> Vec<Robot> {
2829
}
2930

3031
pub fn part1(input: &[Robot]) -> i32 {
31-
let mut q1 = 0;
32-
let mut q2 = 0;
33-
let mut q3 = 0;
34-
let mut q4 = 0;
32+
let mut quadrants = [0; 4];
3533

3634
for [x, y, dx, dy] in input {
3735
let x = (x + 100 * dx) % 101;
3836
let y = (y + 100 * dy) % 103;
3937

40-
if x < 50 {
41-
if y < 51 {
42-
q1 += 1;
43-
}
44-
if y > 51 {
45-
q2 += 1;
46-
}
47-
}
48-
if x > 50 {
49-
if y < 51 {
50-
q3 += 1;
51-
}
52-
if y > 51 {
53-
q4 += 1;
54-
}
38+
match (x.cmp(&50), y.cmp(&51)) {
39+
(Less, Less) => quadrants[0] += 1,
40+
(Less, Greater) => quadrants[1] += 1,
41+
(Greater, Less) => quadrants[2] += 1,
42+
(Greater, Greater) => quadrants[3] += 1,
43+
_ => (),
5544
}
5645
}
5746

58-
q1 * q2 * q3 * q4
47+
quadrants.iter().product()
5948
}
6049

6150
pub fn part2(robots: &[Robot]) -> usize {

0 commit comments

Comments
 (0)