From ee572e5e066e36cbf920a2a86ea07918562d601b Mon Sep 17 00:00:00 2001 From: Ty Date: Fri, 1 Dec 2023 00:38:04 -0700 Subject: [PATCH] I'm tired don't judge my setup ill fix it later or something --- .gitignore | 2 ++ Cargo.lock | 7 +++++ Cargo.toml | 8 ++++++ src/day1/mod.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 13 +++++++++ 5 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/day1/mod.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5aa729 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/**/input.txt \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c0c9382 --- /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 = "advent" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ac5e7d9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "advent" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/day1/mod.rs b/src/day1/mod.rs new file mode 100644 index 0000000..d01db50 --- /dev/null +++ b/src/day1/mod.rs @@ -0,0 +1,70 @@ +pub fn part1() { + let input = include_str!("input.txt"); + + let mut values = Vec::::new(); + + for line in input.lines() { + let digits = line.chars().filter(|char| char.is_numeric()).collect::>(); + let calibrartion_value = format!("{}{}", digits.first().unwrap(), digits.last().unwrap()); + values.push(calibrartion_value.parse().unwrap()); + } + + println!("{}", values.into_iter().reduce(|acc, e| acc + e).unwrap()); +} + +pub fn part2() { + let input = include_str!("input.txt"); + + let mut values = Vec::::new(); + + for line in input.lines() { + let line = line.to_owned(); + + // Find instances of number words and choose the closest, recursively replacing (Can't just .replace b/c of shit like "twone") + let digits = vec![ + line.match_indices("one").collect::>(), + line.match_indices("two").collect::>(), + line.match_indices("three").collect::>(), + line.match_indices("four").collect::>(), + line.match_indices("five").collect::>(), + line.match_indices("six").collect::>(), + line.match_indices("seven").collect::>(), + line.match_indices("eight").collect::>(), + line.match_indices("nine").collect::>(), + line.match_indices("1").collect::>(), + line.match_indices("2").collect::>(), + line.match_indices("3").collect::>(), + line.match_indices("4").collect::>(), + line.match_indices("5").collect::>(), + line.match_indices("6").collect::>(), + line.match_indices("7").collect::>(), + line.match_indices("8").collect::>(), + line.match_indices("9").collect::>(), + ].into_iter(); + + let mut flat = Vec::<(usize, String)>::new(); + for digit in digits { + for (index, text) in digit { + let text = text + .to_owned() + .replace("one", "1") + .replace("two", "2") + .replace("three", "3") + .replace("four", "4") + .replace("five", "5") + .replace("six", "6") + .replace("seven", "7") + .replace("eight", "8") + .replace("nine", "9"); + + flat.push((index, text)); + } + } + flat.sort_by(|a, b| a.0.cmp(&b.0)); + + let calibration_value = format!("{}{}", flat.first().unwrap().1, flat.last().unwrap().1); + values.push(calibration_value.parse().unwrap()); + } + + println!("{}", values.into_iter().reduce(|acc, e| acc + e).unwrap()); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..12aedd0 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,13 @@ +pub mod day1; + +fn main() { + let args = std::env::args().collect::>(); + + let day = args[1].as_str(); + let part = args[2].as_str(); + + match day { + "1" => match part { "1" => day1::part1(), "2" => day1::part2(), _ => panic!("Invalid part #") }, + _ => panic!("Invalid day #") + } +}