mirror of
https://github.com/theoludwig/advent_of_code_2023.git
synced 2024-12-08 00:45:53 +01:00
feat: add day 6
This commit is contained in:
parent
45eca9020d
commit
471d49c27e
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -84,6 +84,13 @@ dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day_6"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.9.0"
|
||||
|
1
day_6/.gitignore
vendored
Normal file
1
day_6/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
7
day_6/Cargo.toml
Normal file
7
day_6/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day_6"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rayon = { workspace = true }
|
73
day_6/README.md
Normal file
73
day_6/README.md
Normal file
@ -0,0 +1,73 @@
|
||||
# - Day 6: Wait For It -
|
||||
|
||||
Source: <https://adventofcode.com/2023/day/6>
|
||||
|
||||
## Instructions - Part 1
|
||||
|
||||
The ferry quickly brings you across Island Island. After asking around, you discover that there is indeed normally a large pile of sand somewhere near here, but you don't see anything besides lots of water and the small island where the ferry has docked.
|
||||
|
||||
As you try to figure out what to do next, you notice a poster on a wall near the ferry dock. "Boat races! Open to the public! Grand prize is an all-expenses-paid trip to **Desert Island**!" That must be where the sand comes from! Best of all, the boat races are starting in just a few minutes.
|
||||
|
||||
You manage to sign up as a competitor in the boat races just in time. The organizer explains that it's not really a traditional race - instead, you will get a fixed amount of time during which your boat has to travel as far as it can, and you win if your boat goes the farthest.
|
||||
|
||||
As part of signing up, you get a sheet of paper (your puzzle input) that lists the **time** allowed for each race and also the best **distance** ever recorded in that race. To guarantee you win the grand prize, you need to make sure you **go farther in each race** than the current record holder.
|
||||
|
||||
The organizer brings you over to the area where the boat races are held. The boats are much smaller than you expected - they're actually **toy boats**, each with a big button on top. Holding down the button **charges the boat**, and releasing the button **allows the boat to move**. Boats move faster if their button was held longer, but time spent holding the button counts against the total race time. You can only hold the button at the start of the race, and boats don't move until the button is released.
|
||||
|
||||
For example:
|
||||
|
||||
```txt
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
||||
```
|
||||
|
||||
This document describes three races:
|
||||
|
||||
- The first race lasts 7 milliseconds. The record distance in this race is 9 millimeters.
|
||||
- The second race lasts 15 milliseconds. The record distance in this race is 40 millimeters.
|
||||
- The third race lasts 30 milliseconds. The record distance in this race is 200 millimeters.
|
||||
|
||||
Your toy boat has a starting speed of **zero millimeters per millisecond**. For each whole millisecond you spend at the beginning of the race holding down the button, the boat's speed increases by **one millimeter per millisecond**.
|
||||
|
||||
So, because the first race lasts 7 milliseconds, you only have a few options:
|
||||
|
||||
- Don't hold the button at all (that is, hold it for **`0` milliseconds**) at the start of the race. The boat won't move; it will have traveled **`0` millimeters** by the end of the race.
|
||||
- Hold the button for **`1` millisecond** at the start of the race. Then, the boat will travel at a speed of 1 millimeter per millisecond for 6 milliseconds, reaching a total distance traveled of **`6` millimeters**.
|
||||
- Hold the button for **`2` milliseconds**, giving the boat a speed of 2 millimeters per millisecond. It will then get 5 milliseconds to move, reaching a total distance of **`10` millimeters**.
|
||||
- Hold the button for **`3` milliseconds**. After its remaining 4 milliseconds of travel time, the boat will have gone **`12` millimeters**.
|
||||
- Hold the button for **`4` milliseconds**. After its remaining 3 milliseconds of travel time, the boat will have gone **`12` millimeters**.
|
||||
- Hold the button for **`5` milliseconds**, causing the boat to travel a total of **`10` millimeters**.
|
||||
- Hold the button for **`6` milliseconds**, causing the boat to travel a total of **`6` millimeters**.
|
||||
- Hold the button for **`7` milliseconds**. That's the entire duration of the race. You never let go of the button. The boat can't move until you let go of the button. Please make sure you let go of the button so the boat gets to move. **`0` millimeters**.
|
||||
|
||||
Since the current record for this race is `9` millimeters, there are actually **`4`** different ways you could win: you could hold the button for `2`, `3`, `4`, or `5` milliseconds at the start of the race.
|
||||
|
||||
In the second race, you could hold the button for at least `4` milliseconds and at most `11` milliseconds and beat the record, a total of **`8`** different ways to win.
|
||||
|
||||
In the third race, you could hold the button for at least `11` milliseconds and no more than `19` milliseconds and still beat the record, a total of **`9`** ways you could win.
|
||||
|
||||
To see how much margin of error you have, determine the **number of ways you can beat the record** in each race; in this example, if you multiply these values together, you get **`288`** (`4` \* `8` \* `9`).
|
||||
|
||||
Determine the number of ways you could beat the record in each race. **What do you get if you multiply these numbers together?**
|
||||
|
||||
## Instructions - Part 2
|
||||
|
||||
As the race is about to start, you realize the piece of paper with race times and record distances you got earlier actually just has very bad [kerning](https://en.wikipedia.org/wiki/Kerning). There's really **only one race** - ignore the spaces between the numbers on each line.
|
||||
|
||||
So, the example from before:
|
||||
|
||||
```txt
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
||||
```
|
||||
|
||||
...now instead means this:
|
||||
|
||||
```txt
|
||||
Time: 71530
|
||||
Distance: 940200
|
||||
```
|
||||
|
||||
Now, you have to figure out how many ways there are to win this single race. In this example, the race lasts for **`71530` milliseconds** and the record distance you need to beat is **`940200` millimeters**. You could hold the button anywhere from `14` to `71516` milliseconds and beat the record, a total of **`71503`** ways!
|
||||
|
||||
**How many ways can you beat the record in this one much longer race?**
|
2
day_6/input.txt
Normal file
2
day_6/input.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Time: 38 94 79 70
|
||||
Distance: 241 1549 1074 1091
|
2
day_6/input_example_1.txt
Normal file
2
day_6/input_example_1.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
114
day_6/src/lib.rs
Normal file
114
day_6/src/lib.rs
Normal file
@ -0,0 +1,114 @@
|
||||
use rayon::prelude::*;
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Clone)]
|
||||
pub struct Race {
|
||||
pub maximum_time_in_milliseconds: usize,
|
||||
pub best_distance_in_millimeters_recorded: usize,
|
||||
}
|
||||
|
||||
impl Race {
|
||||
pub fn get_all_winning_strategies(&self) -> Vec<RaceStrategy> {
|
||||
(1..self.maximum_time_in_milliseconds)
|
||||
.into_par_iter()
|
||||
.map(|time_in_milliseconds_holding_button| RaceStrategy {
|
||||
time_in_milliseconds_holding_button,
|
||||
maximum_time_in_milliseconds: self.maximum_time_in_milliseconds,
|
||||
})
|
||||
.filter(|race_strategy| {
|
||||
let distance_in_millimeters_travelled =
|
||||
race_strategy.calculate_distance_in_millimeters_travelled();
|
||||
distance_in_millimeters_travelled > self.best_distance_in_millimeters_recorded
|
||||
})
|
||||
.collect::<Vec<RaceStrategy>>()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Clone)]
|
||||
pub struct RaceStrategy {
|
||||
pub time_in_milliseconds_holding_button: usize,
|
||||
pub maximum_time_in_milliseconds: usize,
|
||||
}
|
||||
|
||||
impl RaceStrategy {
|
||||
pub fn calculate_distance_in_millimeters_travelled(&self) -> usize {
|
||||
(self.maximum_time_in_milliseconds - self.time_in_milliseconds_holding_button)
|
||||
* self.time_in_milliseconds_holding_button
|
||||
}
|
||||
}
|
||||
|
||||
pub fn part_1(input: &str) -> usize {
|
||||
fn get_numbers_from_line(line: &str) -> Vec<usize> {
|
||||
line.split(':')
|
||||
.last()
|
||||
.unwrap_or_default()
|
||||
.split_ascii_whitespace()
|
||||
.map(|value| value.trim())
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(|value| {
|
||||
let result: usize = value.parse().unwrap_or_default();
|
||||
result
|
||||
})
|
||||
.collect::<Vec<usize>>()
|
||||
}
|
||||
|
||||
let mut lines = input.trim().lines();
|
||||
let maximum_times = get_numbers_from_line(lines.next().unwrap_or_default());
|
||||
let best_distances = get_numbers_from_line(lines.next().unwrap_or_default());
|
||||
maximum_times
|
||||
.iter()
|
||||
.zip(best_distances.iter())
|
||||
.map(
|
||||
|(&maximum_time_in_milliseconds, &best_distance_recorded_in_millimeters)| Race {
|
||||
maximum_time_in_milliseconds,
|
||||
best_distance_in_millimeters_recorded: best_distance_recorded_in_millimeters,
|
||||
},
|
||||
)
|
||||
.map(|race| race.get_all_winning_strategies().len())
|
||||
.product()
|
||||
}
|
||||
|
||||
pub fn part_2(input: &str) -> usize {
|
||||
fn get_number_from_line(line: &str) -> usize {
|
||||
line.split(':')
|
||||
.last()
|
||||
.unwrap_or_default()
|
||||
.replace(' ', "")
|
||||
.trim()
|
||||
.parse()
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
let mut lines = input.trim().lines();
|
||||
let race = Race {
|
||||
maximum_time_in_milliseconds: get_number_from_line(lines.next().unwrap_or_default()),
|
||||
best_distance_in_millimeters_recorded: get_number_from_line(
|
||||
lines.next().unwrap_or_default(),
|
||||
),
|
||||
};
|
||||
race.get_all_winning_strategies().len()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod day_6_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_1_example() {
|
||||
assert_eq!(part_1(include_str!("../input_example_1.txt")), 288);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_2_example() {
|
||||
assert_eq!(part_2(include_str!("../input_example_1.txt")), 71503);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_1() {
|
||||
assert_eq!(part_1(include_str!("../input.txt")), 1083852);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_2() {
|
||||
assert_eq!(part_2(include_str!("../input.txt")), 23501589);
|
||||
}
|
||||
}
|
8
day_6/src/main.rs
Normal file
8
day_6/src/main.rs
Normal file
@ -0,0 +1,8 @@
|
||||
use day_6::{part_1, part_2};
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("../input.txt");
|
||||
println!("- Day 6: Wait For It -");
|
||||
println!("Answer Part 1: {}", part_1(input));
|
||||
println!("Answer Part 2: {}", part_2(input));
|
||||
}
|
Loading…
Reference in New Issue
Block a user