-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.py
70 lines (59 loc) · 1.85 KB
/
11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import utils
import time
import argparse
import os
import math
from collections import defaultdict
def digits(value)->int:
return math.floor(math.log10(value)+1)
def blink_one(stone: int)->[int]:
if stone == 0:
return [1]
d = digits(stone)
if d % 2 == 0:
first = stone//(10**(d//2))
second = stone - (first*(10**(d//2)))
return [first,second]
return [stone * 2024]
def blink_all(stones: dict, count=1)->dict:
for _ in range(count):
for stone,count in list(stones.items()):
stones[stone] -= count
for newStone in blink_one(stone):
stones[newStone] += count
# This just saves time
for stone,count in list(stones.items()):
if count == 0:
del stones[stone]
return stones
def count_stones(stones: dict)->int:
count = 0
for s in stones.values():
count += s
return count
def solve(inv: str):
stones = defaultdict(int)
for s in [int(x) for x in inv.split()]:
stones[s] += 1
blinks1 = blink_all(stones.copy(),25)
blinks2 = blink_all(stones.copy(),75)
return (count_stones(blinks1),count_stones(blinks2))
# inExample = """
# 0 1 10 99 999
# """
inExample = """
125 17
"""
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", default="input/"+ os.path.basename(__file__).split('.')[0] +".txt", help="The file to run. If not specified, will look for a default file with the same name as this in input/")
parser.add_argument("-e","--example", action="store_true", help="Run the example instead of reading a file")
args = parser.parse_args()
if not args.example:
with open(args.input, 'r') as f:
inPuzzle = f.read()
else:
inPuzzle = inExample
start = time.time()
sol = solve(inPuzzle)
end = time.time()
print(f"Finished in {end-start:.3f} seconds: ", sol)