day1 part2
This commit is contained in:
47
2023/day1/Cargo.lock
generated
47
2023/day1/Cargo.lock
generated
@@ -2,6 +2,53 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
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]]
|
[[package]]
|
||||||
name = "day1"
|
name = "day1"
|
||||||
version = "0.1.0"
|
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"
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
regex = "1.10.6"
|
||||||
|
|||||||
@@ -26,4 +26,28 @@ Consider your entire calibration document. What is the sum of all of the calibra
|
|||||||
To begin, get your puzzle input.
|
To begin, get your puzzle input.
|
||||||
|
|
||||||
Answer:
|
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.
|
You can also [Shareon Twitter Mastodon] this puzzle.
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use regex::Regex;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, BufRead};
|
use std::io::{self, BufRead};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@@ -7,7 +9,13 @@ fn main() -> io::Result<()> {
|
|||||||
let mut result = 0;
|
let mut result = 0;
|
||||||
if let Ok(lines) = read_lines("./src/input") {
|
if let Ok(lines) = read_lines("./src/input") {
|
||||||
for line in lines.flatten() {
|
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);
|
println!("Current result: {} + {} = {}", result, value, result + value);
|
||||||
result += value;
|
result += value;
|
||||||
}
|
}
|
||||||
@@ -15,14 +23,40 @@ fn main() -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
println!("Final result: {}", result);
|
println!("Final result: {}", result);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn replace_written_digits<T: AsRef<str>>(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
|
// Extracts first and last digits from a line, forms a number, and returns the sum
|
||||||
fn extract_digit_pair_sum(line: &str) -> Option<i32> {
|
fn extract_digit_pair_sum(line: &str) -> Option<i32> {
|
||||||
let first_digit = get_first_digit(line)?;
|
let first_digit = get_first_digit(line)?;
|
||||||
let last_digit = get_last_digit(line)?;
|
let last_digit = get_last_digit(line)?;
|
||||||
|
|
||||||
let digit_str = format!("{}{}", first_digit, last_digit);
|
let digit_str = format!("{}{}", first_digit, last_digit);
|
||||||
digit_str.parse::<i32>().ok()
|
digit_str.parse::<i32>().ok()
|
||||||
}
|
}
|
||||||
|
|||||||
7
2023/day2/Cargo.lock
generated
Normal file
7
2023/day2/Cargo.lock
generated
Normal file
@@ -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"
|
||||||
6
2023/day2/Cargo.toml
Normal file
6
2023/day2/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
0
2023/day2/README.md
Normal file
0
2023/day2/README.md
Normal file
3
2023/day2/src/main.rs
Normal file
3
2023/day2/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user