day 1, rust

master
Codinget 2 months ago
commit df1ea7405a
  1. 2
      .gitignore
  2. 7
      01/Cargo.lock
  3. 6
      01/Cargo.toml
  4. 30
      01/src/main.rs

2
.gitignore vendored

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

7
01/Cargo.lock generated

@ -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);
}
Loading…
Cancel
Save