Added Solution for 2023 day 01
This commit is contained in:
parent
bdec8d21fe
commit
a64def6dd9
5 changed files with 1104 additions and 0 deletions
8
2023/day01_trebuchet/Cargo.toml
Normal file
8
2023/day01_trebuchet/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day01_trebuchet"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
27
2023/day01_trebuchet/challenge.txt
Normal file
27
2023/day01_trebuchet/challenge.txt
Normal file
|
@ -0,0 +1,27 @@
|
|||
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
|
||||
|
||||
You've been doing this long enough to know that to restore snow operations, you need to check all *fifty stars* by December 25th.
|
||||
|
||||
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants *one star*. Good luck!
|
||||
|
||||
You try to ask why they can't just use a [weather machine](/2015/day/1) ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a [trebuchet](https://en.wikipedia.org/wiki/Trebuchet) ("please hold still, we need to strap you in").
|
||||
|
||||
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been *amended* by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
|
||||
|
||||
The newly-improved calibration document consists of lines of text; each line originally contained a specific *calibration value* that the Elves now need to recover. On each line, the calibration value can be found by combining the *first digit* and the *last digit* (in that order) to form a single *two-digit number*.
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
|
||||
```
|
||||
|
||||
In this example, the calibration values of these four lines are `12`, `38`, `15`, and `77`. Adding these together produces `*142*`.
|
||||
|
||||
Consider your entire calibration document. *What is the sum of all of the calibration values?*
|
||||
|
||||
To play, please identify yourself via one of these services:
|
58
2023/day01_trebuchet/src/lib.rs
Normal file
58
2023/day01_trebuchet/src/lib.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
pub enum NumberFormat { DigitsOnly, DigitsAndSpelledOut }
|
||||
|
||||
/// Extract the first digit of String `line`, concatenated with its last digit, as a usize. If `f`
|
||||
/// is `NumberFormat::DigitsOnly`, only the ASCII characters `0` through `9` count as digits, if it
|
||||
/// is `NumberFormat::DigitsAndSpelledOut`, the English words `one` through `nine` count as well.
|
||||
/// In the latter case, overlaps are allowed.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```
|
||||
/// use day01_trebuchet::{NumberFormat, calibration_value};
|
||||
/// assert_eq!(calibration_value("foo123four", NumberFormat::DigitsOnly), 13);
|
||||
/// assert_eq!(calibration_value("twone3elevenzero", NumberFormat::DigitsAndSpelledOut), 23);
|
||||
/// ```
|
||||
pub fn calibration_value(line: &str, f: NumberFormat) -> usize {
|
||||
// Replace number words with digits for part 2, but keep all Es, Ns, Os and Ts at the beginning
|
||||
// and end, because the words may overlap (which is allowed). Tho other letters, as well as
|
||||
// longer overlaps cannot occur in these words (i. e. while `one` ends in `ne`, no number
|
||||
// starts with `ne`; conversely, `six` starts with an S, which no number ends with).
|
||||
let line_v2 = line.replace("one", "o1e").replace("two", "t2o").replace("three", "t3e").replace("four", "4").replace("five", "5e").replace("six", "6").replace("seven", "7n").replace("eight", "e8t").replace("nine", "n9e");
|
||||
let line = match f {
|
||||
NumberFormat::DigitsOnly => line,
|
||||
NumberFormat::DigitsAndSpelledOut => &line_v2,
|
||||
};
|
||||
let digits: Vec<&str> = line.matches(|c: char| c.is_ascii_digit()).collect();
|
||||
if digits.is_empty() {
|
||||
0
|
||||
} else {
|
||||
10 * digits[0].parse::<usize>().unwrap() + digits[digits.len()-1].parse::<usize>().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(input: &str) -> (usize, usize) {
|
||||
let first = input.lines().map(|l| calibration_value(l, NumberFormat::DigitsOnly)).sum();
|
||||
let second = input.lines().map(|l| calibration_value(l, NumberFormat::DigitsAndSpelledOut)).sum();
|
||||
(first, second)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input), (351, 423));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), (54644, 53348));
|
||||
}
|
||||
}
|
1000
2023/day01_trebuchet/tests/challenge_input
Normal file
1000
2023/day01_trebuchet/tests/challenge_input
Normal file
File diff suppressed because it is too large
Load diff
11
2023/day01_trebuchet/tests/sample_input
Normal file
11
2023/day01_trebuchet/tests/sample_input
Normal file
|
@ -0,0 +1,11 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
Loading…
Add table
Add a link
Reference in a new issue