mirror of
https://github.com/Rhelvetican/aoc2024.git
synced 2024-12-23 06:51:40 +00:00
day 1
This commit is contained in:
commit
cf68639d1e
8 changed files with 1141 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
/input
|
65
Cargo.lock
generated
Normal file
65
Cargo.lock
generated
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc2024"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.92"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.37"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.90"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "2.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "2.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
edition = "2021"
|
||||||
|
name = "aoc2024"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
thiserror = "2"
|
1000
input/day1.txt
Normal file
1000
input/day1.txt
Normal file
File diff suppressed because it is too large
Load diff
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod solutions;
|
5
src/main.rs
Normal file
5
src/main.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
use aoc2024::solutions::day_1::part_two;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("{}", part_two());
|
||||||
|
}
|
60
src/solutions/day_1.rs
Normal file
60
src/solutions/day_1.rs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
const INPUT: &str = include_str!("../../input/day1.txt");
|
||||||
|
|
||||||
|
pub fn part_one() -> u64 {
|
||||||
|
let (mut left, mut right) = INPUT
|
||||||
|
.lines()
|
||||||
|
.filter_map(|s| s.split_once(' ').map(|(s, x)| (s.trim(), x.trim())))
|
||||||
|
.filter_map(
|
||||||
|
|(s, x)| match (s.parse::<u64>().ok(), x.parse::<u64>().ok()) {
|
||||||
|
(Some(x), Some(y)) => Some((x, y)),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.fold((Vec::new(), Vec::new()), |(mut v1, mut v2), (x, y)| {
|
||||||
|
v1.push(x);
|
||||||
|
v2.push(y);
|
||||||
|
(v1, v2)
|
||||||
|
});
|
||||||
|
|
||||||
|
left.sort();
|
||||||
|
right.sort();
|
||||||
|
|
||||||
|
left.into_iter()
|
||||||
|
.zip(right)
|
||||||
|
.map(|(l, r)| if l > r { l - r } else { r - l })
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part_two() -> u64 {
|
||||||
|
let mut cache = HashMap::<u64, u64>::new();
|
||||||
|
let mut similarity_score = 0;
|
||||||
|
|
||||||
|
let (left, right) = INPUT
|
||||||
|
.lines()
|
||||||
|
.filter_map(|s| s.split_once(' ').map(|(s, x)| (s.trim(), x.trim())))
|
||||||
|
.filter_map(
|
||||||
|
|(s, x)| match (s.parse::<u64>().ok(), x.parse::<u64>().ok()) {
|
||||||
|
(Some(x), Some(y)) => Some((x, y)),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.fold((Vec::new(), Vec::new()), |(mut v1, mut v2), (x, y)| {
|
||||||
|
v1.push(x);
|
||||||
|
v2.push(y);
|
||||||
|
(v1, v2)
|
||||||
|
});
|
||||||
|
|
||||||
|
for key in left {
|
||||||
|
if let Some(&val) = cache.get(&key) {
|
||||||
|
similarity_score += key * val;
|
||||||
|
} else {
|
||||||
|
let val = right.iter().filter(|e| e.eq(&&key)).count() as u64;
|
||||||
|
cache.insert(key, val);
|
||||||
|
similarity_score += key * val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
similarity_score
|
||||||
|
}
|
1
src/solutions/mod.rs
Normal file
1
src/solutions/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod day_1;
|
Loading…
Reference in a new issue