Day 22 (Python): Optimize part two (a lot)

This commit is contained in:
akp 2020-12-22 16:12:43 +00:00
parent 8212fb6d96
commit 56ec28b5c8
No known key found for this signature in database
GPG key ID: D3E7EAA31B39637E
3 changed files with 19 additions and 10 deletions

View file

@ -3,17 +3,17 @@
<details><summary>Script output</summary>
```
python .\python\
python .\python\ ft
AoC 2020: day 22 - Crab Combat
Python 3.8.5
Test cases
1.1 pass
2.1 pass
1.1 pass in 0.0 seconds
2.1 pass in 0.0 seconds
Answers
Part 1: 35397
Part 2: 31120 in 18.785030126571655 seconds
Part 1: 35397 in 0.0005002021789550781 seconds
Part 2: 33889 in 2.854492425918579 seconds
```
</details>

View file

@ -9,6 +9,9 @@ from partOne import partOne
from partTwo import partTwo
force_time = False
def run_and_time(f):
st = time.time()
x = f()
@ -45,7 +48,7 @@ def run_tests(test_cases):
else:
output_string += f"[red]fail[/red] (got {result}, expected {expectedInt})"
if t > 15:
if t > 15 or force_time:
output_string += f" in {t} seconds"
print(output_string + " "*12)
@ -86,6 +89,9 @@ if __name__ == "__main__":
visualise.visualise(challenge_input)
sys.exit()
if "ft" in sys.argv:
force_time = True
run_tests(info["testCases"])
if "debug" in sys.argv:
@ -97,7 +103,7 @@ if __name__ == "__main__":
output_string = "Part 1: "
x, t = run_and_time(lambda: partOne(challenge_input))
output_string += str(x)
if t > 15:
if t > 15 or force_time:
output_string += f" in {t} seconds"
print(output_string + " "*12)
@ -105,6 +111,6 @@ if __name__ == "__main__":
output_string = "Part 2: "
x, t = run_and_time(lambda: partTwo(challenge_input))
output_string += str(x)
if t > 15:
if t > 15 or force_time:
output_string += f" in {t} seconds"
print(output_string + " "*12)

View file

@ -10,11 +10,14 @@ def play_round(deck_one: List[int], deck_two: List[int]) -> Tuple[int, Tuple[Lis
seen_configurations = []
while len(deck_one) > 0 and len(deck_two) > 0:
if (deck_one, deck_two) in seen_configurations:
current_hash = hash(tuple(deck_one + deck_two))
if current_hash in seen_configurations:
# print("Seen this config before")
return 1, (deck_one, deck_two)
else:
seen_configurations.append((copy.deepcopy(deck_one), copy.deepcopy(deck_two)))
seen_configurations.append(current_hash)
# do gameplay shenanigans
top_one = deck_one.pop(0)