day 1, rust
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
target/
|
||||||
|
input.txt
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc2024-01"
|
||||||
|
version = "0.1.0"
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc2024-01"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
fn main() {
|
||||||
|
let raw = std::fs::read_to_string("input.txt").expect("Couldn't read input");
|
||||||
|
let mut left = Vec::new();
|
||||||
|
let mut right = Vec::new();
|
||||||
|
for line in raw.split("\n") {
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let split = line
|
||||||
|
.split(" ")
|
||||||
|
.map(|x| x.parse())
|
||||||
|
.collect::<Result<Vec<u32>, _>>()
|
||||||
|
.unwrap();
|
||||||
|
left.push(split[0]);
|
||||||
|
right.push(split[1]);
|
||||||
|
}
|
||||||
|
left.sort();
|
||||||
|
right.sort();
|
||||||
|
let first = left
|
||||||
|
.iter()
|
||||||
|
.zip(right.iter())
|
||||||
|
.map(|(a, b)| a.max(b) - a.min(b))
|
||||||
|
.fold(0, |a, b| a + b);
|
||||||
|
println!("{}", first);
|
||||||
|
let second = left
|
||||||
|
.iter()
|
||||||
|
.map(|a| *a * right.iter().filter(|b| *a == **b).count() as u32)
|
||||||
|
.fold(0, |a, b| a + b);
|
||||||
|
println!("{}", second);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user