Skip to content

Commit

Permalink
minor day 4 optimization and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 8, 2023
1 parent 734c363 commit dd8c630
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `27.7µs` | `38.2µs` |
| [Day 2](./src/bin/02.rs) | `41.3µs` | `41.5µs` |
| [Day 3](./src/bin/03.rs) | `119.4µs` | `114.8µs` |
| [Day 4](./src/bin/04.rs) | `53.3µs` | `53.8µs` |
| [Day 5](./src/bin/05.rs) | `20.5µs` | `24.3µs` |
| [Day 6](./src/bin/06.rs) | `202.0ns` | `102.0ns` |
| [Day 7](./src/bin/07.rs) | `131.4µs` | `136.0µs` |

**Total: 0.80ms**
| [Day 1](./src/bin/01.rs) | `35.5µs` | `34.2µs` |
| [Day 2](./src/bin/02.rs) | `42.1µs` | `41.8µs` |
| [Day 3](./src/bin/03.rs) | `116.8µs` | `116.1µs` |
| [Day 4](./src/bin/04.rs) | `48.9µs` | `50.4µs` |
| [Day 5](./src/bin/05.rs) | `20.8µs` | `24.1µs` |
| [Day 6](./src/bin/06.rs) | `208.0ns` | `102.0ns` |
| [Day 7](./src/bin/07.rs) | `128.7µs` | `133.9µs` |

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

---
Expand Down
23 changes: 12 additions & 11 deletions src/bin/04.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use rayon::{iter::ParallelIterator, str::ParallelString};

advent_of_code::solution!(4);

pub fn part_one(input: &str) -> Option<u32> {
Some(
input
.lines()
.par_lines()
.filter_map(winning_number_count)
.filter(|&c| c > 0)
.map(|c| 1 << (c - 1))
Expand Down Expand Up @@ -32,17 +34,16 @@ fn winning_number_count(card: &str) -> Option<u32> {
let (_, numbers_part) = card.split_once(':')?;
let (winning_str, have_str) = numbers_part.split_once('|')?;
let mut winning_numbers = [false; 100];
for s in winning_str.split_whitespace() {
winning_numbers[s.parse::<usize>().ok()?] = true;
}
let mut count = 0;
for s in have_str.split_whitespace() {
let num = s.parse::<usize>().ok()?;
if winning_numbers[num] {
count += 1;
}
for num in winning_str.split_whitespace().map(|s| s.parse::<usize>()) {
winning_numbers[num.ok()?] = true;
}
Some(count)
Some(
have_str
.split_whitespace()
.filter_map(|s| s.parse::<usize>().ok())
.filter(|&num| winning_numbers[num])
.count() as u32,
)
}

#[cfg(test)]
Expand Down

0 comments on commit dd8c630

Please sign in to comment.