Counter().most_common() is twice as fast as searching in a for-loop
This commit is contained in:
parent
8552731beb
commit
77dd1077e5
10
day19.py
10
day19.py
@ -1,4 +1,4 @@
|
|||||||
from collections import defaultdict
|
from collections import Counter
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
from tools.aoc import AOCDay
|
from tools.aoc import AOCDay
|
||||||
from tools.coordinate import Coordinate, DistanceAlgorithm
|
from tools.coordinate import Coordinate, DistanceAlgorithm
|
||||||
@ -17,7 +17,7 @@ ROTATION_PATH = [
|
|||||||
|
|
||||||
|
|
||||||
def overlap(beacon1: Grid, beacon2: Grid) -> Union[Coordinate, None]:
|
def overlap(beacon1: Grid, beacon2: Grid) -> Union[Coordinate, None]:
|
||||||
diffs = defaultdict(int)
|
diffs = Counter()
|
||||||
for c1 in beacon1.getActiveCells():
|
for c1 in beacon1.getActiveCells():
|
||||||
for c2 in beacon2.getActiveCells():
|
for c2 in beacon2.getActiveCells():
|
||||||
# Interestingly adding an if statement here to break out early when already 12 matches are found
|
# 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.
|
# and checking the results afterwards.
|
||||||
diffs[c1 - c2] += 1
|
diffs[c1 - c2] += 1
|
||||||
|
|
||||||
for d in diffs:
|
d = diffs.most_common(1)[0]
|
||||||
if diffs[d] >= 12:
|
if d[1] >= 12:
|
||||||
return d
|
return d[0]
|
||||||
|
|
||||||
|
|
||||||
def converge(scanner_grids: List[Grid]) -> (Grid, List[Coordinate]):
|
def converge(scanner_grids: List[Grid]) -> (Grid, List[Coordinate]):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user