From 77dd1077e56f83f1caaa002abd48f1974888bee3 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Mon, 17 Jan 2022 09:38:18 +0100 Subject: [PATCH] Counter().most_common() is twice as fast as searching in a for-loop --- day19.py | 10 +++++----- day24.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/day19.py b/day19.py index 90b197d..67e4207 100644 --- a/day19.py +++ b/day19.py @@ -1,4 +1,4 @@ -from collections import defaultdict +from collections import Counter from itertools import combinations from tools.aoc import AOCDay from tools.coordinate import Coordinate, DistanceAlgorithm @@ -17,7 +17,7 @@ ROTATION_PATH = [ def overlap(beacon1: Grid, beacon2: Grid) -> Union[Coordinate, None]: - diffs = defaultdict(int) + diffs = Counter() for c1 in beacon1.getActiveCells(): for c2 in beacon2.getActiveCells(): # Interestingly adding an if statement here to break out early when already 12 matches are found @@ -25,9 +25,9 @@ def overlap(beacon1: Grid, beacon2: Grid) -> Union[Coordinate, None]: # and checking the results afterwards. diffs[c1 - c2] += 1 - for d in diffs: - if diffs[d] >= 12: - return d + d = diffs.most_common(1)[0] + if d[1] >= 12: + return d[0] def converge(scanner_grids: List[Grid]) -> (Grid, List[Coordinate]): diff --git a/day24.py b/day24.py index c67b80d..3e7910c 100644 --- a/day24.py +++ b/day24.py @@ -8,7 +8,7 @@ def getTheNumberFast(pushpull, adders, subbers, part2=False): inc = 1 else: number_init = [9] * 14 - inc = - 1 + inc = -1 z = int(1e9) number = number_init.copy()