Skip to content

Commit

Permalink
2023-04: Avoid HashMap overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
MLNW committed Dec 4, 2023
1 parent f190364 commit f70ffaa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
10 changes: 5 additions & 5 deletions 2023-rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `64.6µs` | `639.8µs` |
| [Day 2](./src/bin/02.rs) | `41.8µs` | `48.0µs` |
| [Day 3](./src/bin/03.rs) | `430.0µs` | `424.2µs` |
| [Day 4](./src/bin/04.rs) | `98.3µs` | `47.2ms` |
| [Day 1](./src/bin/01.rs) | `65.0µs` | `634.6µs` |
| [Day 2](./src/bin/02.rs) | `40.9µs` | `47.4µs` |
| [Day 3](./src/bin/03.rs) | `427.6µs` | `424.2µs` |
| [Day 4](./src/bin/04.rs) | `96.0µs` | `19.6ms` |

**Total: 48.95ms**
**Total: 21.34ms**
<!--- benchmarking table --->

## Usage
Expand Down
11 changes: 4 additions & 7 deletions 2023-rust/src/bin/04.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;

use itertools::Itertools;

Expand Down Expand Up @@ -43,17 +43,14 @@ pub fn part_one(input: &str) -> Option<u32> {
pub fn part_two(input: &str) -> Option<u32> {
let cards = parse_input(input);

let lookup: HashMap<u32, Vec<u32>> =
cards.iter().map(|card| (card.id, card.copies())).collect();
let lookup = cards.iter().map(|card| card.copies()).collect_vec();

let mut queue = VecDeque::from(cards.iter().map(|card| card.id).collect::<Vec<u32>>());

let mut total = 0;
while let Some(card) = queue.pop_front() {
while let Some(id) = queue.pop_front() {
total += 1;

let copies = lookup.get(&card).unwrap();
queue.extend(copies);
queue.extend(&lookup[id as usize]);
}

Some(total)
Expand Down

0 comments on commit f70ffaa

Please sign in to comment.