summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2024-12-01 11:59:08 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2024-12-01 11:59:08 +0000
commit9c340e9288d5ad90fa7d582ccafc15c61e985246 (patch)
tree489049ae4aff78796e8827e3b0980a7d37cedacd
parent464400a609636cb8fd172610b4d5f391b2c0bb71 (diff)
downloadaoc2024-9c340e9288d5ad90fa7d582ccafc15c61e985246.tar.gz
aoc2024-9c340e9288d5ad90fa7d582ccafc15c61e985246.tar.xz
aoc2024-9c340e9288d5ad90fa7d582ccafc15c61e985246.zip
Day 1
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml6
-rw-r--r--src/bin/1.rs41
4 files changed, 55 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..5d41a28
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "aoc2024"
+version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..3453c1c
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "aoc2024"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/src/bin/1.rs b/src/bin/1.rs
new file mode 100644
index 0000000..df5dead
--- /dev/null
+++ b/src/bin/1.rs
@@ -0,0 +1,41 @@
+use std::{
+ collections::HashMap,
+ error::Error,
+ fs::File,
+ io::{BufRead, BufReader},
+};
+
+fn main() -> Result<(), Box<dyn Error>> {
+ let f = File::open("input/1")?;
+ let f = BufReader::new(f);
+ let mut list_a: Vec<i64> = Vec::new();
+ let mut list_b: Vec<i64> = Vec::new();
+ for line in f.lines() {
+ let line = line?;
+ let (a, b) = line.trim_end().split_once(" ").ok_or("Invalid line")?;
+ list_a.push(a.parse()?);
+ list_b.push(b.parse()?);
+ }
+ list_a.sort();
+ list_b.sort();
+ println!(
+ "part1: {}",
+ list_a
+ .iter()
+ .zip(list_b.iter())
+ .map(|p| (p.0 - p.1).abs())
+ .sum::<i64>()
+ );
+ let mut frequencies: HashMap<i64, i64> = HashMap::new();
+ for n in list_b {
+ *frequencies.entry(n).or_default() += 1;
+ }
+ println!(
+ "part2: {}",
+ list_a
+ .into_iter()
+ .map(|n| n * frequencies.get(&n).copied().unwrap_or_default())
+ .sum::<i64>()
+ );
+ Ok(())
+}