Day1
This commit is contained in:
parent
efd8d34727
commit
02044a65fb
3 changed files with 95 additions and 0 deletions
16
Cargo.lock
generated
16
Cargo.lock
generated
|
|
@ -74,6 +74,7 @@ dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"humantime",
|
"humantime",
|
||||||
|
"itertools",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -190,6 +191,12 @@ version = "1.0.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "eyre"
|
name = "eyre"
|
||||||
version = "0.6.12"
|
version = "0.6.12"
|
||||||
|
|
@ -230,6 +237,15 @@ version = "1.70.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
|
|
|
||||||
|
|
@ -9,3 +9,4 @@ bstr = "1.11.0"
|
||||||
clap = { version = "4.5.21", features = ["derive"] }
|
clap = { version = "4.5.21", features = ["derive"] }
|
||||||
color-eyre = "0.6.3"
|
color-eyre = "0.6.3"
|
||||||
humantime = "2.1.0"
|
humantime = "2.1.0"
|
||||||
|
itertools = "0.13.0"
|
||||||
|
|
|
||||||
78
src/bin/day1.rs
Normal file
78
src/bin/day1.rs
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
use std::{collections::HashMap, time::Instant};
|
||||||
|
|
||||||
|
use aoc_2024::{load, print_res};
|
||||||
|
use bstr::BString;
|
||||||
|
use color_eyre::eyre::{Context, ContextCompat};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
type Parsed = (Vec<u64>, Vec<u64>);
|
||||||
|
|
||||||
|
pub fn parsing(input: &BString) -> color_eyre::Result<Parsed> {
|
||||||
|
let (first, second) = core::str::from_utf8(input)
|
||||||
|
.unwrap()
|
||||||
|
.lines()
|
||||||
|
.map(|l| -> color_eyre::Result<_> {
|
||||||
|
let mut nums = l.split_ascii_whitespace();
|
||||||
|
let first: u64 = nums
|
||||||
|
.next()
|
||||||
|
.with_context(|| "missing first number")?
|
||||||
|
.parse()
|
||||||
|
.with_context(|| "parsing first number")?;
|
||||||
|
let second: u64 = nums
|
||||||
|
.next()
|
||||||
|
.with_context(|| "missing second number")?
|
||||||
|
.parse()
|
||||||
|
.with_context(|| "parsing second number")?;
|
||||||
|
|
||||||
|
Ok((first, second))
|
||||||
|
})
|
||||||
|
.process_results(|iter| iter.unzip())?;
|
||||||
|
Ok((first, second))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1((mut first, mut second): Parsed) {
|
||||||
|
first.sort_unstable();
|
||||||
|
second.sort_unstable();
|
||||||
|
|
||||||
|
let total_distance: u64 = first.iter().zip(second).map(|(a, b)| a.abs_diff(b)).sum();
|
||||||
|
print_res!("Total distance: {total_distance}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part2((left, right): Parsed) {
|
||||||
|
let mut similarities = HashMap::new();
|
||||||
|
let mut total_similarity = 0;
|
||||||
|
|
||||||
|
for n in left {
|
||||||
|
match similarities.get(&n) {
|
||||||
|
None => {
|
||||||
|
let similarity = n * right.iter().filter(|&&x| x == n).count() as u64;
|
||||||
|
similarities.insert(n, similarity);
|
||||||
|
total_similarity += similarity;
|
||||||
|
}
|
||||||
|
Some(v) => total_similarity += v,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print_res!("Total similarity: {total_similarity}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() -> color_eyre::Result<()> {
|
||||||
|
let context = load()?;
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
|
let parsed = parsing(&context.input)?;
|
||||||
|
let elapsed = humantime::format_duration(start.elapsed());
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
|
if context.part == 1 {
|
||||||
|
part1(parsed);
|
||||||
|
} else {
|
||||||
|
part2(parsed);
|
||||||
|
}
|
||||||
|
let elapsed_part = humantime::format_duration(start.elapsed());
|
||||||
|
|
||||||
|
println!(" Parsing: {elapsed}");
|
||||||
|
println!(" Solving: {elapsed_part}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue