From 212aeac8fb5cef090e0e0a3fe894548f8f29aa8d Mon Sep 17 00:00:00 2001 From: Kevin Caffrey Date: Thu, 7 Dec 2023 19:57:38 -0500 Subject: [PATCH] even smaller day 7 optimization --- README.md | 18 +++++++++--------- src/bin/07.rs | 12 ++++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 75f00d1..519008c 100644 --- a/README.md +++ b/README.md @@ -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** --- diff --git a/src/bin/07.rs b/src/bin/07.rs index c793c03..68ede35 100644 --- a/src/bin/07.rs +++ b/src/bin/07.rs @@ -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, } @@ -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) } }