mirror of
https://github.com/kristoferssolo/Advent-of-Code.git
synced 2025-10-21 18:00:35 +00:00
day-02 part-1
This commit is contained in:
parent
cf14bce68e
commit
33729d3cdb
14
2024/Cargo.lock
generated
14
2024/Cargo.lock
generated
@ -129,6 +129,20 @@ dependencies = [
|
|||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day-02"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"divan",
|
||||||
|
"itertools",
|
||||||
|
"miette",
|
||||||
|
"nom",
|
||||||
|
"rstest",
|
||||||
|
"test-log",
|
||||||
|
"tracing",
|
||||||
|
"tracing-subscriber",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "divan"
|
name = "divan"
|
||||||
version = "0.1.16"
|
version = "0.1.16"
|
||||||
|
|||||||
23
2024/day-02/Cargo.toml
Normal file
23
2024/day-02/Cargo.toml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
[package]
|
||||||
|
name = "day-02"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
itertools.workspace = true
|
||||||
|
nom.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
tracing-subscriber.workspace = true
|
||||||
|
miette.workspace = true
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
divan.workspace = true
|
||||||
|
rstest.workspace = true
|
||||||
|
test-log.workspace = true
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "day-02-bench"
|
||||||
|
path = "benches/benchmarks.rs"
|
||||||
|
harness = false
|
||||||
21
2024/day-02/benches/benchmarks.rs
Normal file
21
2024/day-02/benches/benchmarks.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
use day_02::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
divan::main();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[divan::bench]
|
||||||
|
fn part1() {
|
||||||
|
part1::process(divan::black_box(include_str!(
|
||||||
|
"../input1.txt",
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[divan::bench]
|
||||||
|
fn part2() {
|
||||||
|
part2::process(divan::black_box(include_str!(
|
||||||
|
"../input2.txt",
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
12
2024/day-02/src/bin/part1.rs
Normal file
12
2024/day-02/src/bin/part1.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use day_02::part1::process;
|
||||||
|
use miette::{Context, Result};
|
||||||
|
|
||||||
|
#[tracing::instrument]
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
let file = include_str!("../../input1.txt");
|
||||||
|
let result = process(file).context("process part 1")?;
|
||||||
|
println!("{}", result);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
12
2024/day-02/src/bin/part2.rs
Normal file
12
2024/day-02/src/bin/part2.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use day_02::part2::process;
|
||||||
|
use miette::{Context, Result};
|
||||||
|
|
||||||
|
#[tracing::instrument]
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
let file = include_str!("../../input2.txt");
|
||||||
|
let result = process(file).context("process part 2")?;
|
||||||
|
println!("{}", result);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
2
2024/day-02/src/lib.rs
Normal file
2
2024/day-02/src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod part1;
|
||||||
|
pub mod part2;
|
||||||
57
2024/day-02/src/part1.rs
Normal file
57
2024/day-02/src/part1.rs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
use miette::Result;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum Level {
|
||||||
|
Safe,
|
||||||
|
Unsafe,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&Vec<usize>> for Level {
|
||||||
|
fn from(value: &Vec<usize>) -> Self {
|
||||||
|
if (value.iter().is_sorted() || value.iter().rev().is_sorted())
|
||||||
|
&& value.windows(2).all(|pairs| {
|
||||||
|
let diff = pairs[0].abs_diff(pairs[1]);
|
||||||
|
(1..=3).contains(&diff)
|
||||||
|
})
|
||||||
|
{
|
||||||
|
return Self::Safe;
|
||||||
|
}
|
||||||
|
Self::Unsafe
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument]
|
||||||
|
pub fn process(input: &str) -> Result<usize> {
|
||||||
|
let sum = input
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let levels = line
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|x| x.parse::<usize>().unwrap())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Level::from(&levels)
|
||||||
|
})
|
||||||
|
.filter(|&report| report == Level::Safe)
|
||||||
|
.count();
|
||||||
|
Ok(sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process() -> Result<()> {
|
||||||
|
let input = "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
|
||||||
|
";
|
||||||
|
let result = 2;
|
||||||
|
assert_eq!(process(input)?, result);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
21
2024/day-02/src/part2.rs
Normal file
21
2024/day-02/src/part2.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
use miette::Result;
|
||||||
|
|
||||||
|
#[tracing::instrument]
|
||||||
|
pub fn process(input: &str) -> Result<usize> {
|
||||||
|
todo!("day xx - part 2");
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process() -> Result<()> {
|
||||||
|
let input = "";
|
||||||
|
todo!("haven't built test yet");
|
||||||
|
let result = 0;
|
||||||
|
assert_eq!(process(input)?, result);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user