day 1
This commit is contained in:
commit
c2575cbcea
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
input.txt
|
||||
target/
|
7
day-01/Cargo.lock
generated
Normal file
7
day-01/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day-01"
|
||||
version = "0.1.0"
|
6
day-01/Cargo.toml
Normal file
6
day-01/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day-01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
43
day-01/src/main.rs
Normal file
43
day-01/src/main.rs
Normal file
@ -0,0 +1,43 @@
|
||||
const INPUT: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/input.txt"));
|
||||
|
||||
fn main() {
|
||||
let (mut first, mut second): (Vec<_>, Vec<_>) =
|
||||
INPUT.lines()
|
||||
.filter_map(|line| {
|
||||
let mut parts = line.split_whitespace();
|
||||
let first = parts.next()?;
|
||||
let second = parts.next()?;
|
||||
Some((
|
||||
str::parse::<u32>(first).ok()?,
|
||||
str::parse::<u32>(second).ok()?,
|
||||
))
|
||||
})
|
||||
.unzip();
|
||||
|
||||
first.sort();
|
||||
second.sort();
|
||||
|
||||
let difference = first.iter().zip(second.iter().copied())
|
||||
.map(|(x, y)| x.abs_diff(y))
|
||||
.sum::<u32>();
|
||||
|
||||
println!("Part 1: {}", difference);
|
||||
|
||||
let mut similarity = 0;
|
||||
let mut idx = 0;
|
||||
for element in first {
|
||||
while second.get(idx).is_some_and(|x| *x < element) {
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
let mut occurances = 0;
|
||||
while second.get(idx) == Some(&element) {
|
||||
idx += 1;
|
||||
occurances += 1;
|
||||
}
|
||||
|
||||
similarity += occurances * element;
|
||||
}
|
||||
|
||||
println!("Part 2: {}", similarity);
|
||||
}
|
Loading…
Reference in New Issue
Block a user