Skip to content

Commit

Permalink
aoc2023/day6: Add solution
Browse files Browse the repository at this point in the history
Signed-off-by: Andrej Orsula <orsula.andrej@gmail.com>
  • Loading branch information
AndrejOrsula committed Dec 6, 2023
1 parent 969d6fa commit e50d2ee
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ My solutions to [Advent of Code](https://adventofcode.com) puzzles.
| 3 | [Gear Ratios](https://adventofcode.com/2023/day/3) | [`day3.rs`](aoc2023/src/day3.rs) |
| 4 | [Scratchcards](https://adventofcode.com/2023/day/4) | [`day4.rs`](aoc2023/src/day4.rs) |
| 5 | [If You Give A Seed ...](https://adventofcode.com/2023/day/5) | [`day5.rs`](aoc2023/src/day5.rs) |
| 6 | [Wait For It](https://adventofcode.com/2023/day/6) | [`day6.rs`](aoc2023/src/day6.rs) |

</td><td>

Expand All @@ -34,6 +35,7 @@ My solutions to [Advent of Code](https://adventofcode.com) puzzles.
| 1.146 µs | 429.6 µs |
| 58.79 µs | 27.72 µs |
| 19.61 µs | 5.510 µs |
| 1.432 µs | 0.345 µs |

</td><td>

Expand All @@ -44,6 +46,7 @@ My solutions to [Advent of Code](https://adventofcode.com) puzzles.
| 0.419 µs | 308.1 µs |
| 53.39 µs | 28.44 µs |
| 16.38 µs | 19.364 s |
| 0.513 µs | 2.463 ms |

</td></tr>
</table>
Expand Down
2 changes: 2 additions & 0 deletions aoc2023/input/2023/day6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 57 72 69 92
Distance: 291 1172 1176 2026
78 changes: 78 additions & 0 deletions aoc2023/src/day6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use aoc_runner_derive::{aoc, aoc_generator};

#[aoc_generator(day6)]
fn parse(input: &str) -> smallvec::SmallVec<[utils::RaceData; 4]> {
use itertools::Itertools;

let (times, distances) = input
.lines()
.map(|line| line.split(':').last().unwrap().split_ascii_whitespace())
.collect_tuple()
.unwrap();
times
.into_iter()
.zip(distances)
.map(|(time, distance)| utils::RaceData {
time: time.to_owned(),
distance: distance.to_owned(),
})
.collect()
}

mod utils {
pub struct RaceData {
pub time: String,
pub distance: String,
}

impl RaceData {
pub fn n_record_breaks(&self) -> u64 {
let (time, distance) = (self.time.parse().unwrap(), self.distance.parse().unwrap());
match (1..time).find(|j| j * (time - j) > distance) {
Some(first_record) => 1 + time - (2 * first_record),
_ => 1,
}
}

pub fn merge(sequence: &[Self]) -> Self {
let (time, distance) = sequence.iter().fold(
Default::default(),
|(time, distance): (String, String), data| {
(time + &data.time, distance + &data.distance)
},
);
Self { time, distance }
}
}
}

#[aoc(day6, part1)]
fn part1(input: &[utils::RaceData]) -> u64 {
input.iter().map(utils::RaceData::n_record_breaks).product()
}

#[aoc(day6, part2)]
fn part2(input: &[utils::RaceData]) -> u64 {
utils::RaceData::merge(input).n_record_breaks()
}

#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;

const SAMPLE: &str = indoc! {"
Time: 7 15 30
Distance: 9 40 200
"};

#[test]
fn part1_example() {
assert_eq!(part1(&parse(SAMPLE)), 288);
}

#[test]
fn part2_example() {
assert_eq!(part2(&parse(SAMPLE)), 71503);
}
}
1 change: 1 addition & 0 deletions aoc2023/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod day2;
pub mod day3;
pub mod day4;
pub mod day5;
pub mod day6;

aoc_runner_derive::aoc_lib! { year = 2023 }

0 comments on commit e50d2ee

Please sign in to comment.