diff --git a/2024/Cargo.toml b/2024/Cargo.toml index ab56df7..01804fa 100644 --- a/2024/Cargo.toml +++ b/2024/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "2" members = [ - # "day1", + "day1", # "day2", # "day3", # "day4", diff --git a/2024/day1/.gitignore b/2024/day1/.gitignore new file mode 100644 index 0000000..e983cca --- /dev/null +++ b/2024/day1/.gitignore @@ -0,0 +1,19 @@ +input.txt +flamegraph.svg +perf.data* +### Rust +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + diff --git a/2024/day1/Cargo.toml b/2024/day1/Cargo.toml new file mode 100644 index 0000000..0007567 --- /dev/null +++ b/2024/day1/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day1" +authors = ["mirsella "] +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.13.0" + diff --git a/2024/day1/build.rs b/2024/day1/build.rs new file mode 100644 index 0000000..b6a19a0 --- /dev/null +++ b/2024/day1/build.rs @@ -0,0 +1,28 @@ +use std::fs::File; +use std::io::{self, Read}; +use std::path::PathBuf; +use std::{env, fs}; + +fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()> { + let mut contents = String::new(); + File::open(file_path)?.read_to_string(&mut contents)?; + let new_contents = contents.replace(old, new); + if contents != new_contents { + println!("Updating {}", file_path.display()); + fs::write(file_path, new_contents)?; + } + Ok(()) +} + +fn main() -> io::Result<()> { + let pkg_name = env::var("CARGO_PKG_NAME").unwrap(); + replace_in_file( + &"../Cargo.toml".into(), + &format!("# \"{pkg_name}\""), + &format!("\"{pkg_name}\""), + )?; + + replace_in_file(&"./Cargo.toml".into(), "\n[workspace]", "")?; + + Ok(()) +} diff --git a/2024/day1/src/main.rs b/2024/day1/src/main.rs new file mode 100644 index 0000000..1e6dd5e --- /dev/null +++ b/2024/day1/src/main.rs @@ -0,0 +1,56 @@ +use itertools::Itertools; + +fn part1(input: &str) -> usize { + let (mut a, mut b): (Vec, Vec) = input + .lines() + .map(|l| { + l.split_whitespace() + .map(|n| n.parse::().unwrap()) + .next_tuple::<(_, _)>() + .unwrap() + }) + .unzip(); + a.sort_unstable(); + b.sort_unstable(); + a.into_iter() + .zip(b) + .map(|(a, b)| (b as isize - a as isize).unsigned_abs()) + .sum() +} +fn part2(input: &str) -> usize { + let (a, b): (Vec, Vec) = input + .lines() + .map(|l| { + l.split_whitespace() + .map(|n| n.parse::().unwrap()) + .next_tuple::<(_, _)>() + .unwrap() + }) + .unzip(); + a.into_iter() + .map(|va| va * b.iter().filter(|&&vb| vb == va).count()) + .sum() +} +fn main() { + let input = include_str!("../input.txt"); + println!("Part 1: {}", part1(input)); + println!("Part 2: {}", part2(input)); +} + +#[cfg(test)] +mod tests { + const INPUT: &str = "3 4 +4 3 +2 5 +1 3 +3 9 +3 3"; + #[test] + fn part1() { + assert_eq!(super::part1(INPUT), 11); + } + #[test] + fn part2() { + assert_eq!(super::part2(INPUT), 31); + } +}