day1
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
**/Cargo.lock
|
||||
**/target
|
||||
6
day1/Cargo.toml
Normal file
6
day1/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
1000
day1/input.txt
Normal file
1000
day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
28
day1/src/main.rs
Normal file
28
day1/src/main.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use std::collections::{BTreeMap, BinaryHeap};
|
||||
|
||||
fn main() {
|
||||
let input = std::fs::read_to_string("input.txt").unwrap();
|
||||
let lines = input.lines();
|
||||
let mut left_vecs = BinaryHeap::new();
|
||||
let mut right_vecs = BinaryHeap::new();
|
||||
for line in lines {
|
||||
let (left, right) = line.split_once(" ").unwrap();
|
||||
left_vecs.push(left.parse::<i32>().unwrap());
|
||||
right_vecs.push(right.parse::<i32>().unwrap());
|
||||
}
|
||||
let left_sorted = left_vecs.into_sorted_vec();
|
||||
let right_sorted = right_vecs.into_sorted_vec();
|
||||
let mut right2count = BTreeMap::new();
|
||||
for right in right_sorted.iter() {
|
||||
*right2count.entry(right).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let mut sum = 0;
|
||||
let mut score = 0;
|
||||
for (left, right) in left_sorted.iter().zip(right_sorted.iter()) {
|
||||
sum += left.abs_diff(*right);
|
||||
let s = right2count.get(left).unwrap_or(&0);
|
||||
score += left * s;
|
||||
}
|
||||
println!("{}, {}", sum, score);
|
||||
}
|
||||
Reference in New Issue
Block a user