Skip to content

Commit

Permalink
2023-06: Solve second puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
MLNW committed Dec 7, 2023
1 parent a4d3975 commit 022aa2f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
12 changes: 6 additions & 6 deletions 2023-rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `64.5µs` | `624.7µs` |
| [Day 2](./src/bin/02.rs) | `41.0µs` | `47.0µs` |
| [Day 3](./src/bin/03.rs) | `428.9µs` | `421.1µs` |
| [Day 4](./src/bin/04.rs) | `97.7µs` | `19.3ms` |
| [Day 1](./src/bin/01.rs) | `64.7µs` | `644.9µs` |
| [Day 2](./src/bin/02.rs) | `41.5µs` | `52.3µs` |
| [Day 3](./src/bin/03.rs) | `454.5µs` | `444.3µs` |
| [Day 4](./src/bin/04.rs) | `101.8µs` | `19.9ms` |
| [Day 5](./src/bin/05.rs) | `30.3µs` | `26.5s` |
| [Day 6](./src/bin/06.rs) | `489.0ns` | `-` |
| [Day 6](./src/bin/06.rs) | `534.0ns` | `38.9ms` |

**Total: 26521.03ms**
**Total: 26560.60ms**
<!--- benchmarking table --->

## Usage
Expand Down
41 changes: 26 additions & 15 deletions 2023-rust/src/bin/06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,53 @@ use itertools::Itertools;
advent_of_code::solution!(6);

struct Race {
total_time: u32,
record_distance: u32,
total_time: u64,
record_distance: u64,
}

impl Race {
fn distance(&self, time_holding_button: u32) -> u32 {
fn distance(&self, time_holding_button: u64) -> u64 {
let left_over_ms = self.total_time - time_holding_button;
time_holding_button * left_over_ms
}

fn simulate(&self) -> Vec<u32> {
fn simulate(&self) -> Vec<u64> {
(1..self.total_time - 1)
.map(|time_holding_button| self.distance(time_holding_button))
.filter(|distance| distance > &self.record_distance)
.collect_vec()
}
}

pub fn part_one(input: &str) -> Option<u32> {
let races = parse_input(input);
pub fn part_one(input: &str) -> Option<u64> {
let races = parse_input(input, false);

races
.iter()
.map(|race| race.simulate().len() as u32)
.map(|race| race.simulate().len() as u64)
.reduce(|acc, possible_wins| acc * possible_wins)
}

pub fn part_two(input: &str) -> Option<u32> {
None
pub fn part_two(input: &str) -> Option<u64> {
let race = &parse_input(input, true)[0];

Some(race.simulate().len() as u64)
}

fn parse_input(input: &str) -> Vec<Race> {
fn parse_input(input: &str, remove_spaces: bool) -> Vec<Race> {
let (times, distances) = input.split_once('\n').unwrap();

let times = parse_numbers(times.strip_prefix("Time:").unwrap());
let distances = parse_numbers(distances.strip_prefix("Distance:").unwrap());
let times = times.strip_prefix("Time:").unwrap();
let distances = distances.strip_prefix("Distance:").unwrap();

let (times, distances) = if remove_spaces {
(times.replace(' ', ""), distances.replace(' ', ""))
} else {
(times.to_owned(), distances.to_owned())
};

let times = parse_numbers(times);
let distances = parse_numbers(distances);

times
.iter()
Expand All @@ -50,9 +61,9 @@ fn parse_input(input: &str) -> Vec<Race> {
.collect_vec()
}

fn parse_numbers(line: &str) -> Vec<u32> {
fn parse_numbers(line: String) -> Vec<u64> {
line.split_whitespace()
.map(|number| number.parse::<u32>().unwrap())
.map(|number| number.parse::<u64>().unwrap())
.collect_vec()
}

Expand All @@ -69,6 +80,6 @@ mod tests {
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(71503));
}
}

0 comments on commit 022aa2f

Please sign in to comment.