Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit29f2dd6

Browse files
committed
Added day 2018-12
1 parentbbedcfd commit29f2dd6

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos,numpyasnp
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":'''initial state: #..#.#..##......###...###
8+
9+
...## => #
10+
..#.. => #
11+
.#... => #
12+
.#.#. => #
13+
.#.## => #
14+
.##.. => #
15+
.#### => #
16+
#.#.# => #
17+
#.### => #
18+
##.#. => #
19+
##.## => #
20+
###.. => #
21+
###.# => #
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+
}
31+
32+
# -------------------------------- Control program execution -------------------------------- #
33+
34+
case_to_test='real'
35+
part_to_test=2
36+
37+
# -------------------------------- Initialize some variables -------------------------------- #
38+
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'
42+
43+
44+
# -------------------------------- Actual code execution -------------------------------- #
45+
46+
# Note: numpy was used to practice. Clearly not the best choice here.
47+
48+
ifpart_to_test==1:
49+
generations=20
50+
else:
51+
generations=50000000000
52+
53+
54+
initial_state=puzzle_input.splitlines()[0][15:]
55+
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))
58+
59+
rules= {}
60+
forstringinpuzzle_input.splitlines()[2:]:
61+
source,target=string.split(' => ')
62+
rules[source]=target
63+
64+
prev_sum=sum(np.where(pots=='#')[0])-5*10**5*len(np.where(pots=='#')[0])
65+
foriinrange (1,generations):
66+
67+
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])]='.'
71+
72+
min_x,max_x=min(np.where(pots=='#')[0]),max(np.where(pots=='#')[0])
73+
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)]
76+
pots=new_pots.copy()
77+
78+
sum_pots=sum(np.where(new_pots=='#')[0])-5*10**5*len(np.where(new_pots=='#')[0])
79+
80+
print (i,sum_pots,sum_pots-prev_sum)
81+
prev_sum=sum_pots
82+
83+
ifi==200:
84+
puzzle_actual_result=sum_pots+96* (generations-200)
85+
break
86+
87+
ifpart_to_test==1:
88+
puzzle_actual_result=sum_pots
89+
90+
91+
# -------------------------------- Outputs / results -------------------------------- #
92+
93+
print ('Expected result : '+str(puzzle_expected_result))
94+
print ('Actual result : '+str(puzzle_actual_result))
95+
96+
97+
98+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp