Day 1
This commit is contained in:
commit
c51d28eb75
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
input.txt
|
7
Cargo.lock
generated
Normal file
7
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 = "testing"
|
||||||
|
version = "0.1.0"
|
13
Cargo.toml
Normal file
13
Cargo.toml
Normal 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
42
src/main.rs
Normal 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();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user