|
4 | 4 | test_data= {} |
5 | 5 |
|
6 | 6 | test=1 |
7 | | -test_data[test]= {"input":'''initial state: #..#.#..##......###...### |
| 7 | +test_data[test]= { |
| 8 | +"input":"""initial state: #..#.#..##......###...### |
8 | 9 |
|
9 | 10 | ...## => # |
10 | 11 | ..#.. => # |
|
19 | 20 | ##.## => # |
20 | 21 | ###.. => # |
21 | 22 | ###.# => # |
22 | | -####. => #''', |
23 | | -"expected": ['325','Unknown'], |
24 | | - } |
25 | | - |
26 | | -test='real' |
27 | | -input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt')) |
28 | | -test_data[test]= {"input":open(input_file,"r+").read().strip(), |
29 | | -"expected": ['3890','23743'], |
30 | | - } |
| 23 | +####. => #""", |
| 24 | +"expected": ["325","Unknown"], |
| 25 | +} |
| 26 | + |
| 27 | +test="real" |
| 28 | +input_file=os.path.join( |
| 29 | +os.path.dirname(__file__), |
| 30 | +"Inputs", |
| 31 | +os.path.basename(__file__).replace(".py",".txt"), |
| 32 | +) |
| 33 | +test_data[test]= { |
| 34 | +"input":open(input_file,"r+").read().strip(), |
| 35 | +"expected": ["3890","23743"], |
| 36 | +} |
31 | 37 |
|
32 | 38 | # -------------------------------- Control program execution -------------------------------- # |
33 | 39 |
|
34 | | -case_to_test='real' |
| 40 | +case_to_test="real" |
35 | 41 | part_to_test=2 |
36 | 42 |
|
37 | 43 | # -------------------------------- Initialize some variables -------------------------------- # |
38 | 44 |
|
39 | | -puzzle_input=test_data[case_to_test]['input'] |
40 | | -puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1] |
41 | | -puzzle_actual_result='Unknown' |
| 45 | +puzzle_input=test_data[case_to_test]["input"] |
| 46 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 47 | +puzzle_actual_result="Unknown" |
42 | 48 |
|
43 | 49 |
|
44 | 50 | # -------------------------------- Actual code execution -------------------------------- # |
|
53 | 59 |
|
54 | 60 | initial_state=puzzle_input.splitlines()[0][15:] |
55 | 61 |
|
56 | | -pots=np.full((len(initial_state)+10**6),'.') |
57 | | -pots[5*10**5:5*10**5+len(initial_state)]=np.fromiter(initial_state,dtype='S1',count=len(initial_state)) |
| 62 | +pots=np.full((len(initial_state)+10**6),".") |
| 63 | +pots[5*10**5 :5*10**5+len(initial_state)]=np.fromiter( |
| 64 | +initial_state,dtype="S1",count=len(initial_state) |
| 65 | +) |
58 | 66 |
|
59 | 67 | rules= {} |
60 | 68 | forstringinpuzzle_input.splitlines()[2:]: |
61 | | -source,target=string.split(' =>') |
| 69 | +source,target=string.split(" =>") |
62 | 70 | rules[source]=target |
63 | 71 |
|
64 | | -prev_sum=sum(np.where(pots=='#')[0])-5*10**5*len(np.where(pots=='#')[0]) |
65 | | -foriinrange(1,generations): |
| 72 | +prev_sum=sum(np.where(pots=="#")[0])-5*10**5*len(np.where(pots=="#")[0]) |
| 73 | +foriinrange(1,generations): |
66 | 74 |
|
67 | 75 | ifcase_to_test==1: |
68 | | -foriinrange(2,len(pots)-3): |
69 | | -if''.join(pots[i-2:i+3])notinrules: |
70 | | -rules[''.join(pots[i-2:i+3])]='.' |
| 76 | +foriinrange(2,len(pots)-3): |
| 77 | +if"".join(pots[i-2 :i+3])notinrules: |
| 78 | +rules["".join(pots[i-2 :i+3])]="." |
71 | 79 |
|
72 | | -min_x,max_x=min(np.where(pots=='#')[0]),max(np.where(pots=='#')[0]) |
| 80 | +min_x,max_x=min(np.where(pots=="#")[0]),max(np.where(pots=="#")[0]) |
73 | 81 |
|
74 | | -new_pots=np.full((len(initial_state)+10**6),'.') |
75 | | -new_pots[min_x-2:max_x+2]= [rules[''.join(pots[i-2:i+3])]foriinrange(min_x-2,max_x+2)] |
| 82 | +new_pots=np.full((len(initial_state)+10**6),".") |
| 83 | +new_pots[min_x-2 :max_x+2]= [ |
| 84 | +rules["".join(pots[i-2 :i+3])]foriinrange(min_x-2,max_x+2) |
| 85 | + ] |
76 | 86 | pots=new_pots.copy() |
77 | 87 |
|
78 | | -sum_pots=sum(np.where(new_pots=='#')[0])-5*10**5*len(np.where(new_pots=='#')[0]) |
| 88 | +sum_pots=sum(np.where(new_pots=="#")[0])-5*10**5*len( |
| 89 | +np.where(new_pots=="#")[0] |
| 90 | + ) |
79 | 91 |
|
80 | | -print (i,sum_pots,sum_pots-prev_sum) |
| 92 | +#print (i, sum_pots, sum_pots - prev_sum) |
81 | 93 | prev_sum=sum_pots |
82 | 94 |
|
83 | 95 | ifi==200: |
84 | | -puzzle_actual_result=sum_pots+96* (generations-200) |
| 96 | +puzzle_actual_result=sum_pots+96* (generations-200) |
85 | 97 | break |
86 | 98 |
|
87 | 99 | ifpart_to_test==1: |
|
90 | 102 |
|
91 | 103 | # -------------------------------- Outputs / results -------------------------------- # |
92 | 104 |
|
93 | | -print ('Expected result : '+str(puzzle_expected_result)) |
94 | | -print ('Actual result : '+str(puzzle_actual_result)) |
95 | | - |
96 | | - |
97 | | - |
98 | | - |
| 105 | +print("Expected result : "+str(puzzle_expected_result)) |
| 106 | +print("Actual result : "+str(puzzle_actual_result)) |