Initial commit

This commit is contained in:
Felix Suchert 2024-12-10 01:59:54 +01:00
commit 6095e30342
Signed by: feliix42
GPG key ID: 24363525EA0E8A99
7 changed files with 1107 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

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 = 4
[[package]]
name = "aoc2024"
version = "0.1.0"

10
Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "aoc2024"
version = "0.1.0"
edition = "2024"
authors = ["Felix Suchert <dev@felixsuchert.de>"]
description = "My solutions to the advent of code 2024"
license = "MIT"
[dependencies]

1001
inputs/day_1.txt Normal file

File diff suppressed because it is too large Load diff

22
src/main.rs Normal file
View file

@ -0,0 +1,22 @@
mod solutions;
use std::env;
use std::str::FromStr;
fn main() -> std::io::Result<()> {
println!("Hello, world!");
let mut args = env::args();
if args.len() < 2 {
eprintln!("Usage: {} [day] ...", args.next().unwrap());
return Ok(());
}
match u8::from_str(&args.next().unwrap()) {
Ok(1) => solutions::day_1::run(args)?,
Ok(_) => eprintln!("Unknown day. Are you from the future?"),
Err(_) => eprintln!("Unknown day format. Use integers to specify."),
}
Ok(())
}

64
src/solutions/day_1.rs Normal file
View file

@ -0,0 +1,64 @@
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str::FromStr;
pub fn run(mut args: env::Args) -> std::io::Result<()> {
let filename = args
.next()
.expect("You must provide a filename as argument");
let f = File::open(filename)?;
let reader = BufReader::new(f);
let mut a_list = Vec::new();
let mut b_list = Vec::new();
for line in reader.lines() {
let l = line.unwrap();
if l.is_empty() {
continue;
}
let mut split = l.split_whitespace();
a_list.push(u32::from_str(split.next().unwrap()).unwrap());
b_list.push(u32::from_str(split.next().unwrap()).unwrap());
}
a_list.sort_unstable();
b_list.sort_unstable();
// Task 1
let sum = a_list
.iter()
.zip(b_list.iter())
.fold(0, |acc, (a, b)| acc + a.abs_diff(*b));
println!("Sum of all differences (sorted): {sum}");
// Task 2
let mut similarity_score = 0;
let mut b_idx = 0;
let mut current_count = 0;
for a_item in a_list {
while a_item > b_list[b_idx] {
b_idx += 1;
current_count = 0;
}
if a_item == b_list[b_idx] {
if current_count == 0 {
current_count += 1;
while b_list[b_idx + 1] == b_list[b_idx] {
current_count += 1;
b_idx += 1;
}
}
similarity_score += a_item * current_count;
}
}
println!("Similarity Score: {similarity_score}");
Ok(())
}

2
src/solutions/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod day_1;