1
1
mirror of https://github.com/theoludwig/advent_of_code_2023.git synced 2024-12-08 00:45:53 +01:00

feat: add day 1

This commit is contained in:
Théo LUDWIG 2023-12-01 12:45:01 +01:00
parent 9617cc66cc
commit 88dbcbbc05
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
8 changed files with 1175 additions and 0 deletions

1
day_1/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
day_1/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day_1"
version = "1.0.0"

6
day_1/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day_1"
version = "1.0.0"
edition = "2021"
[dependencies]

72
day_1/README.md Normal file
View File

@ -0,0 +1,72 @@
# - Day 1: Trebuchet?! -
Source: <https://adventofcode.com/2023/day/1>
## Getting Started
### Prerequisites
[Rust](https://www.rust-lang.org/) v1.74.0
### Run
```sh
cargo run
```
## Instructions
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](https://adventofcode.com/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:
```txt
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 begin, [get your puzzle input](./input.txt).
Your puzzle answer was `55130`.
**The first half of this puzzle is complete! It provides one gold star: `*`.**
### 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:
```txt
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?**
Your puzzle answer was `54985`.
**Both parts of this puzzle are complete! They provide two gold stars: `**`.**

1000
day_1/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

78
day_1/src/main.rs Normal file
View File

@ -0,0 +1,78 @@
fn part_1(input: &str) -> i32 {
input
.lines()
.map(|line| {
let characters_digits = line
.chars()
.filter(|&character| character.is_ascii_digit())
.collect::<Vec<char>>();
let first_digit = characters_digits.first().unwrap_or(&'0').to_owned();
let last_digit = characters_digits.last().unwrap_or(&'0').to_owned();
let number = format!("{}{}", first_digit, last_digit);
let number: i32 = number.parse().expect("Should parse as a number.");
number
})
.sum()
}
fn part_2(input: &str) -> i32 {
let numbers_spelled_out_with_letters = [
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
];
input
.lines()
.map(|line| {
let mut characters_digits: Vec<char> = vec![];
let mut temporary = String::from("");
for character in line.chars() {
temporary += &character.to_string();
let mut temporary_spelled_number_index = None;
for (index, spelled_number) in numbers_spelled_out_with_letters.iter().enumerate() {
if temporary.contains(spelled_number) {
temporary_spelled_number_index = Some(index);
break;
}
}
if let Some(temporary_spelled_number_index) = temporary_spelled_number_index {
let number = temporary_spelled_number_index + 1;
characters_digits.push(
number
.to_string()
.chars()
.next()
.expect("Number should be single-character digit."),
);
temporary = character.to_string();
}
if character.is_ascii_digit() {
characters_digits.push(character);
temporary = String::from("");
}
}
let first_digit = characters_digits.first().unwrap_or(&'0').to_owned();
let last_digit = characters_digits.last().unwrap_or(&'0').to_owned();
let number = format!("{}{}", first_digit, last_digit);
let number: i32 = number.parse().expect("Should parse as a number.");
number
})
.sum()
}
fn main() {
let input_example1 = include_str!("../input_example_1.txt");
let input_example2 = include_str!("../input_example_2.txt");
let input = include_str!("../input.txt");
println!("Answer Part 1 (example1): {}", part_1(input_example1));
println!("Answer Part 1 (example2): {}", part_1(input_example2));
println!("Answer Part 1: {}", part_1(input));
println!("Answer Part 2 (example1): {}", part_2(input_example1));
println!("Answer Part 2 (example2): {}", part_2(input_example2));
println!("Answer Part 2: {}", part_2(input));
}