Skip to content

Commit

Permalink
even smaller day 7 optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 8, 2023
1 parent 12a1a92 commit 212aeac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 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) | `30.8µs` | `39.3µs` |
| [Day 2](./src/bin/02.rs) | `45.3µs` | `42.2µs` |
| [Day 3](./src/bin/03.rs) | `117.8µs` | `116.8µs` |
| [Day 4](./src/bin/04.rs) | `48.8µs` | `50.4µs` |
| [Day 5](./src/bin/05.rs) | `20.7µs` | `24.3µs` |
| [Day 6](./src/bin/06.rs) | `203.0ns` | `96.0ns` |
| [Day 7](./src/bin/07.rs) | `100.7µs` | `97.0µs` |

**Total: 0.73ms**
| [Day 1](./src/bin/01.rs) | `33.9µs` | `36.0µs` |
| [Day 2](./src/bin/02.rs) | `41.5µs` | `40.8µs` |
| [Day 3](./src/bin/03.rs) | `116.5µs` | `115.9µs` |
| [Day 4](./src/bin/04.rs) | `49.4µs` | `49.7µs` |
| [Day 5](./src/bin/05.rs) | `20.9µs` | `24.1µs` |
| [Day 6](./src/bin/06.rs) | `202.0ns` | `100.0ns` |
| [Day 7](./src/bin/07.rs) | `93.6µs` | `92.6µs` |

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

---
Expand Down
12 changes: 6 additions & 6 deletions src/bin/07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ enum HandType {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
struct Hand {
hand_type: HandType,
cards: [Card; 5],
cards_ordering: [usize; 5],
cards_ordering: [u8; 5],
joker: Option<Card>,
}

Expand All @@ -79,16 +78,17 @@ impl Hand {
joker,
..Default::default()
};
let mut cards: [Card; 5] = Default::default();
for (i, card) in s.chars().map(Card::try_from).enumerate() {
let card = card?;
hand.cards[i] = card;
cards[i] = card;
hand.cards_ordering[i] = match (joker, card) {
(Some(j), c) if j == c => 0,
(Some(_), c) => c.ordinal() as usize + 1,
(None, c) => c.ordinal() as usize,
(Some(_), c) => c.ordinal() as u8 + 1,
(None, c) => c.ordinal() as u8,
}
}
hand.hand_type = hand_type(hand.cards, joker);
hand.hand_type = hand_type(cards, joker);
Ok(hand)
}
}
Expand Down

0 comments on commit 212aeac

Please sign in to comment.