Skip to content

Commit f76a40a

Browse files
committed
Year 2024 Day 2
1 parent ddfe0bf commit f76a40a

File tree

7 files changed

+82
-1
lines changed

7 files changed

+82
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
7878
| Day | Problem | Solution | Benchmark (μs) |
7979
| --- | --- | --- | --: |
8080
| 1 | [Historian Hysteria](https://adventofcode.com/2024/day/1) | [Source](src/year2024/day01.rs) | 21 |
81+
| 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [Source](src/year2024/day02.rs) | 85 |
8182

8283
## 2023
8384

benches/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,5 @@ mod year2023 {
293293

294294
mod year2024 {
295295
benchmark!(year2024, day01);
296+
benchmark!(year2024, day02);
296297
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,5 @@ pub mod year2023 {
292292
/// # Locate the Chief Historian in time for the big Christmas sleigh launch.
293293
pub mod year2024 {
294294
pub mod day01;
295+
pub mod day02;
295296
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,5 +360,5 @@ fn year2023() -> Vec<Solution> {
360360
}
361361

362362
fn year2024() -> Vec<Solution> {
363-
vec![solution!(year2024, day01)]
363+
vec![solution!(year2024, day01), solution!(year2024, day02)]
364364
}

src/year2024/day02.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! # Red-Nosed Reports
2+
use crate::util::parse::*;
3+
4+
type Input = (u32, u32);
5+
6+
pub fn parse(input: &str) -> Input {
7+
let mut row = Vec::new();
8+
let mut part_one = 0;
9+
let mut part_two = 0;
10+
11+
for line in input.lines() {
12+
row.extend(line.iter_unsigned::<u32>());
13+
14+
if safe(&row) {
15+
part_one += 1;
16+
part_two += 1;
17+
} else {
18+
for i in 0..row.len() {
19+
let level = row.remove(i);
20+
21+
if safe(&row) {
22+
part_two += 1;
23+
break;
24+
}
25+
26+
row.insert(i, level);
27+
}
28+
}
29+
30+
row.clear();
31+
}
32+
33+
(part_one, part_two)
34+
}
35+
36+
pub fn part1(input: &Input) -> u32 {
37+
input.0
38+
}
39+
40+
pub fn part2(input: &Input) -> u32 {
41+
input.1
42+
}
43+
44+
fn safe(levels: &[u32]) -> bool {
45+
let mut up = true;
46+
let mut down = true;
47+
let mut range = true;
48+
49+
for w in levels.windows(2) {
50+
up &= w[0] < w[1];
51+
down &= w[0] > w[1];
52+
range &= (1..=3).contains(&w[0].abs_diff(w[1]));
53+
}
54+
55+
(up ^ down) && range
56+
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,5 @@ mod year2023 {
282282

283283
mod year2024 {
284284
mod day01_test;
285+
mod day02_test;
285286
}

tests/year2024/day02_test.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use aoc::year2024::day02::*;
2+
3+
const EXAMPLE: &str = "\
4+
7 6 4 2 1
5+
1 2 7 8 9
6+
9 7 6 2 1
7+
1 3 2 4 5
8+
8 6 4 4 1
9+
1 3 6 7 9";
10+
11+
#[test]
12+
fn part1_test() {
13+
let input = parse(EXAMPLE);
14+
assert_eq!(part1(&input), 2);
15+
}
16+
17+
#[test]
18+
fn part2_test() {
19+
let input = parse(EXAMPLE);
20+
assert_eq!(part2(&input), 4);
21+
}

0 commit comments

Comments
 (0)