This commit is contained in:
StNicolay 2024-12-01 15:10:20 +03:00
commit c51d28eb75
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
4 changed files with 64 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
input.txt

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "testing"
version = "0.1.0"

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "testing"
version = "0.1.0"
edition = "2021"
[profile.release]
debug = 1
[lints.clippy]
pedantic = "warn"
all = "warn"
[dependencies]

42
src/main.rs Normal file
View File

@ -0,0 +1,42 @@
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();
}