This commit is contained in:
Tobias Berger 2024-12-02 13:03:35 +01:00
parent bd88f94db8
commit 3509e0916e
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
5 changed files with 1096 additions and 5 deletions

2
Cargo.lock generated
View file

@ -19,7 +19,7 @@ checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
[[package]] [[package]]
name = "advent-of-code-2024" name = "advent-of-code-2024"
version = "24.2.0" version = "24.2.2"
dependencies = [ dependencies = [
"reqwest", "reqwest",
] ]

View file

@ -1,7 +1,7 @@
[package] [package]
authors = ["Toby <toby@tobot.dev>"] authors = ["Toby <toby@tobot.dev>"]
name = "advent-of-code-2024" name = "advent-of-code-2024"
version = "24.2.0" version = "24.2.2"
edition = "2021" edition = "2021"
default-run = "get_input" default-run = "get_input"

1000
day02/input.txt Normal file

File diff suppressed because it is too large Load diff

94
day02/main.rs Normal file
View file

@ -0,0 +1,94 @@
#![feature(array_windows)]
pub const INPUT: &str = include_str!("./input.txt");
fn parse_input(input: &'static str) -> Vec<Vec<i32>> {
let input = input
.lines()
.map(|line| -> Vec<i32> {
line.split_whitespace()
.map(|num| num.parse::<i32>().unwrap())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
input
}
pub fn main() {
let input = parse_input(INPUT);
let result_1 = part_1(&input);
println!("{result_1}");
let result_2 = part_2(&input);
println!("{result_2}");
}
fn is_safe(report: &Vec<i32>) -> bool {
let increasing = report[0] < report[1];
report.array_windows::<2>().all(|window| {
let increase = window[1] as i32 - window[0] as i32;
if increasing {
1 <= increase && increase <= 3
} else {
-1 >= increase && increase >= -3
}
})
}
fn part_1(input: &Vec<Vec<i32>>) -> i32 {
let mut safe = 0;
for report in input {
if is_safe(report) {
safe += 1;
}
}
safe
}
fn part_2(input: &Vec<Vec<i32>>) -> i32 {
let mut safe = 0;
for report in input {
for i in 0..report.len() {
let modified_report = [&report[..i], &report[i + 1..]].concat();
if is_safe(&modified_report) {
safe += 1;
break;
}
}
}
safe
}
#[cfg(test)]
mod tests {
use crate::parse_input;
const SAMPLE_INPUT: &str = r#"7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9"#;
#[test]
fn test_part_1_with_sample_solution() {
let input = parse_input(SAMPLE_INPUT);
assert_eq!(super::part_1(&input), 2)
}
#[test]
fn test_part_1_with_solution() {
let input = parse_input(super::INPUT);
assert_eq!(super::part_1(&input), 379);
}
#[test]
fn test_part_2_with_sample_solution() {
let input = parse_input(SAMPLE_INPUT);
assert_eq!(super::part_2(&input), 4)
}
#[test]
fn test_part_2_with_solution() {
let input = parse_input(super::INPUT);
assert_eq!(super::part_2(&input), 430);
}
}

View file

@ -1,3 +0,0 @@
[toolchain]
channel = "stable"
components = ["rustfmt", "cargo", "rustc", "rust-src"]