43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
|
use std::{
|
||
|
collections::HashMap,
|
||
|
fs::File,
|
||
|
io::{BufRead, BufReader},
|
||
|
};
|
||
|
|
||
|
#[allow(dead_code)]
|
||
|
fn day1_1() {
|
||
|
let reader = BufReader::new(File::open("input.txt").unwrap());
|
||
|
let mut a = Vec::<i32>::new();
|
||
|
let mut b = Vec::<i32>::new();
|
||
|
for line in reader.lines() {
|
||
|
let line = line.unwrap();
|
||
|
let (a1, b1) = line.trim_end().split_once(" ").unwrap();
|
||
|
a.push(a1.parse().unwrap());
|
||
|
b.push(b1.parse().unwrap());
|
||
|
}
|
||
|
a.sort_unstable();
|
||
|
b.sort_unstable();
|
||
|
let result: i32 = a.into_iter().zip(b).map(|(a, b)| (a - b).abs()).sum();
|
||
|
println!("{result}");
|
||
|
}
|
||
|
|
||
|
#[allow(dead_code)]
|
||
|
fn day1_2() {
|
||
|
let reader = BufReader::new(File::open("input.txt").unwrap());
|
||
|
let mut a = Vec::<u32>::new();
|
||
|
let mut b = HashMap::<u32, u32>::new();
|
||
|
for line in reader.lines() {
|
||
|
let line = line.unwrap();
|
||
|
let (a1, b1) = line.trim_end().split_once(" ").unwrap();
|
||
|
a.push(a1.parse().unwrap());
|
||
|
*b.entry(b1.parse().unwrap()).or_insert(0) += 1;
|
||
|
}
|
||
|
let result: u32 = a.into_iter().map(|a| a * *b.get(&a).unwrap_or(&0)).sum();
|
||
|
println!("{result}");
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
day1_1();
|
||
|
day1_2();
|
||
|
}
|