From 002f1a2b047105d7e3324e9a45c61d0928eef38f Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Tue, 22 Dec 2020 15:19:57 +0100 Subject: [PATCH] 2020: day22: use Hasher for part 2 --- aoc2020/src/day22.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/aoc2020/src/day22.rs b/aoc2020/src/day22.rs index 4c2a970..d956a8a 100644 --- a/aoc2020/src/day22.rs +++ b/aoc2020/src/day22.rs @@ -1,6 +1,8 @@ use std::cmp::Ordering; +use std::collections::hash_map::DefaultHasher; use std::collections::{HashSet, VecDeque}; use std::fmt::Write; +use std::hash::{Hash, Hasher}; use anyhow::{Context, Result}; @@ -48,9 +50,14 @@ fn play_recursive_game(mut deck_a: Deck, mut deck_b: Deck) -> (Deck, bool) { // exactly the same cards in the same order in the same players' decks, the game instantly // ends in a win for player 1. Previous rounds from other games are not considered. (This // prevents infinite games of Recursive Combat, which everyone agrees is a bad idea.) - let score_a = deck_score(&deck_a); - let score_b = deck_score(&deck_b); - let hash = score_a * 100_000 + score_b; + + // we could use the score here, and it works on my input, but to be on the safe side let's + // just compute the hash manually (the goal is to avoid cloning entire Decks just to check + // their presence in the set) + let mut hasher = DefaultHasher::new(); + deck_a.hash(&mut hasher); + deck_b.hash(&mut hasher); + let hash = hasher.finish(); if seen.contains(&hash) { return (deck_a, true); } else {