Skip to content

Commit 31b1b21

Browse files
committed
AOC 2024 Day 02
1 parent 10003a7 commit 31b1b21

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

aoc/2024/rust/Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aoc/2024/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test_lib = []
2424
# Template dependencies
2525
chrono = { version = "0.4.38", optional = true }
2626
dhat = { version = "0.3.3", optional = true }
27+
itertools = "0.13.0"
2728
pico-args = "0.5.0"
2829
tinyjson = "2.5.1"
2930

aoc/2024/rust/data/examples/02.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
7 6 4 2 1
2+
1 2 7 8 9
3+
9 7 6 2 1
4+
1 3 2 4 5
5+
8 6 4 4 1
6+
1 3 6 7 9

aoc/2024/rust/src/bin/02.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use itertools::Itertools;
2+
3+
advent_of_code::solution!(2);
4+
5+
pub fn part_one(input: &str) -> Option<u32> {
6+
fn is_safe_report(report: &Vec<u32>) -> bool {
7+
fn is_increasing(report: &Vec<u32>) -> bool {
8+
for (a, b) in report.iter().tuple_windows() {
9+
if a >= b || b - a > 3 {
10+
return false;
11+
}
12+
}
13+
true
14+
}
15+
fn is_decreasing(report: &Vec<u32>) -> bool {
16+
for (a, b) in report.iter().tuple_windows() {
17+
if a <= b || a - b > 3 {
18+
return false;
19+
}
20+
}
21+
true
22+
}
23+
is_increasing(report) || is_decreasing(report)
24+
}
25+
Some(
26+
input
27+
.lines()
28+
.map(|line| {
29+
let report = line
30+
.split_ascii_whitespace()
31+
.map(|s| s.parse::<u32>().expect("A valid number"))
32+
.collect::<Vec<_>>();
33+
if is_safe_report(&report) {
34+
1
35+
} else {
36+
0
37+
}
38+
})
39+
.sum(),
40+
)
41+
}
42+
43+
pub fn part_two(input: &str) -> Option<u32> {
44+
fn is_safe_report(report: &Vec<u32>) -> bool {
45+
fn is_increasing(report: &Vec<u32>) -> bool {
46+
for (a, b) in report.iter().tuple_windows() {
47+
if a >= b || b - a > 3 {
48+
return false;
49+
}
50+
}
51+
true
52+
}
53+
fn is_decreasing(report: &Vec<u32>) -> bool {
54+
for (a, b) in report.iter().tuple_windows() {
55+
if a <= b || a - b > 3 {
56+
return false;
57+
}
58+
}
59+
true
60+
}
61+
if is_increasing(report) || is_decreasing(report) {
62+
return true;
63+
}
64+
for skip_idx in 0..report.len() {
65+
let clone = report
66+
.iter()
67+
.enumerate()
68+
.filter_map(|(idx, item)| if idx == skip_idx { None } else { Some(*item) })
69+
.collect::<Vec<_>>();
70+
if is_increasing(&clone) || is_decreasing(&clone) {
71+
return true;
72+
}
73+
}
74+
false
75+
}
76+
Some(
77+
input
78+
.lines()
79+
.map(|line| {
80+
let report = line
81+
.split_ascii_whitespace()
82+
.map(|s| s.parse::<u32>().expect("A valid number"))
83+
.collect::<Vec<_>>();
84+
if is_safe_report(&report) {
85+
1
86+
} else {
87+
0
88+
}
89+
})
90+
.sum(),
91+
)
92+
}
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use super::*;
97+
98+
#[test]
99+
fn test_part_one() {
100+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
101+
assert_eq!(result, Some(2));
102+
}
103+
104+
#[test]
105+
fn test_part_two() {
106+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
107+
assert_eq!(result, Some(4));
108+
}
109+
}

0 commit comments

Comments
 (0)