This commit is contained in:
2024-12-02 11:51:53 +09:00
commit a1d47b07c5
4 changed files with 1036 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
**/Cargo.lock
**/target

6
day1/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "day1"
version = "0.1.0"
edition = "2021"
[dependencies]

1000
day1/input.txt Normal file

File diff suppressed because it is too large Load Diff

28
day1/src/main.rs Normal file
View 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);
}