diff --git a/2023/day1/Cargo.lock b/2023/day1/Cargo.lock index ae5bfe8..5912e12 100644 --- a/2023/day1/Cargo.lock +++ b/2023/day1/Cargo.lock @@ -2,6 +2,53 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "day1" version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" diff --git a/2023/day1/Cargo.toml b/2023/day1/Cargo.toml index fb6b7ec..3801a1b 100644 --- a/2023/day1/Cargo.toml +++ b/2023/day1/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +regex = "1.10.6" diff --git a/2023/day1/README.md b/2023/day1/README.md index cf284ae..3a785b5 100644 --- a/2023/day1/README.md +++ b/2023/day1/README.md @@ -26,4 +26,28 @@ Consider your entire calibration document. What is the sum of all of the calibra To begin, get your puzzle input. Answer: +You can also [Shareon Twitter Mastodon] this puzzle. + +## Part Two + +Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits". + +Equipped with this new information, you now need to find the real first and last digit on each line. For example: + +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen + +In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281. + +What is the sum of all of the calibration values? + +Answer: + +Although it hasn't changed, you can still get your puzzle input. + You can also [Shareon Twitter Mastodon] this puzzle. \ No newline at end of file diff --git a/2023/day1/src/main.rs b/2023/day1/src/main.rs index 153daec..34c57fd 100644 --- a/2023/day1/src/main.rs +++ b/2023/day1/src/main.rs @@ -1,3 +1,5 @@ +use regex::Regex; +use std::collections::HashMap; use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; @@ -7,7 +9,13 @@ fn main() -> io::Result<()> { let mut result = 0; if let Ok(lines) = read_lines("./src/input") { for line in lines.flatten() { - if let Some(value) = extract_digit_pair_sum(&line) { + print!("{} -> ", line); + + // No need for .as_str(), just pass &line + let mut replaced_line = replace_written_digits(&line); + replaced_line = replace_written_digits(&replaced_line); + println!("{}", replaced_line); + if let Some(value) = extract_digit_pair_sum(&replaced_line) { println!("Current result: {} + {} = {}", result, value, result + value); result += value; } @@ -15,14 +23,40 @@ fn main() -> io::Result<()> { } println!("Final result: {}", result); + Ok(()) } +fn replace_written_digits>(input: T) -> String { + // Create a map of written digits to their numeric counterparts + let digit_map = HashMap::from([ + ("one", "1ne"), + ("two", "2wo"), + ("three", "3hree"), + ("four", "4our"), + ("five", "5ive"), + ("six", "6ix"), + ("seven", "7even"), + ("eight", "8ight"), + ("nine", "9ine"), + ]); + + // Create a regex that matches the written digits + let re = Regex::new(r"(?:one|two|three|four|five|six|seven|eight|nine)").unwrap(); + + // Replace all occurrences of written digits with their numeric counterparts + re.replace_all(input.as_ref(), |caps: ®ex::Captures| { + // Access the full match (use caps.get(0) instead of caps[1]) + let matched_word = caps.get(0).unwrap().as_str(); + digit_map.get(matched_word).unwrap().to_string() + }).to_string() +} + // Extracts first and last digits from a line, forms a number, and returns the sum fn extract_digit_pair_sum(line: &str) -> Option { let first_digit = get_first_digit(line)?; let last_digit = get_last_digit(line)?; - + let digit_str = format!("{}{}", first_digit, last_digit); digit_str.parse::().ok() } diff --git a/2023/day2/Cargo.lock b/2023/day2/Cargo.lock new file mode 100644 index 0000000..63c2f60 --- /dev/null +++ b/2023/day2/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day2" +version = "0.1.0" diff --git a/2023/day2/Cargo.toml b/2023/day2/Cargo.toml new file mode 100644 index 0000000..cd3d088 --- /dev/null +++ b/2023/day2/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day2" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2023/day2/README.md b/2023/day2/README.md new file mode 100644 index 0000000..e69de29 diff --git a/2023/day2/src/main.rs b/2023/day2/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/2023/day2/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}