Skip to content

Commit

Permalink
Day 6 part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
snorremd committed Dec 10, 2023
1 parent 3a7c495 commit 322318a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ You can find the puzzle inputs for each day on the Advent of Code website, or yo

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `179.7µs` | `580.0µs` |
| [Day 2](./src/bin/02.rs) | `72.7µs` | `72.7µs` |
| [Day 3](./src/bin/03.rs) | `617.6µs` | `646.2µs` |
| [Day 4](./src/bin/04.rs) | `186.6µs` | `185.5µs` |
| [Day 5](./src/bin/05.rs) | `22.7µs` | `2.0s` |

**Total: 2002.56ms**
| [Day 1](./src/bin/01.rs) | `179.2µs` | `580.4µs` |
| [Day 2](./src/bin/02.rs) | `73.7µs` | `73.2µs` |
| [Day 3](./src/bin/03.rs) | `614.0µs` | `644.0µs` |
| [Day 4](./src/bin/04.rs) | `185.0µs` | `186.2µs` |
| [Day 5](./src/bin/05.rs) | `22.9µs` | `2.0s` |
| [Day 6](./src/bin/06.rs) | `403.0ns` | `17.7ms` |

**Total: 2020.26ms**
<!--- benchmarking table --->

---
Expand Down
68 changes: 68 additions & 0 deletions src/bin/06.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use itertools::{multizip, Itertools};

advent_of_code::solution!(6);

pub fn part_one(input: &str) -> Option<usize> {
// Parse input into tuples of (time, speed) in usize based on
// Time: 7 15 30
// Distance: 9 40 200
let mut times_distances = input
.lines()
.map(|s| s.split_whitespace().filter_map(|s| s.parse::<usize>().ok()));

let times = times_distances.next().unwrap();
let distances = times_distances.next().unwrap();
let rounds = times.zip(distances);

let score = rounds
.map(|(time, record_distance)| {
(0..time)
.map(|press| {
let remaining = time - press;
let distance = press * remaining;
distance
})
.filter(|&distance| distance > record_distance)
.count()
})
.reduce(|a, b| a * b);

score
}

pub fn part_two(input: &str) -> Option<usize> {
let [time, speed]: [usize; 2] = input
.lines()
.map(|s| s.chars().filter(|c| c.is_numeric()).collect::<String>())
.filter_map(|s| s.parse::<usize>().ok())
.collect_vec()
.try_into()
.unwrap();

(0..time)
.map(|press| {
let remaining = time - press;
let distance = press * remaining;
distance
})
.filter(|&distance| distance > speed)
.count()
.into()
}

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

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(288));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(71503));
}
}

0 comments on commit 322318a

Please sign in to comment.