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

Commit72b7d89

Browse files
committed
Added days 2017-05 to 2017-08
1 parent6b93886 commit72b7d89

File tree

4 files changed

+374
-0
lines changed

4 files changed

+374
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":"""0
8+
3
9+
0
10+
1
11+
-3""",
12+
"expected": ['Unknown','Unknown'],
13+
}
14+
15+
test+=1
16+
test_data[test]= {"input":"""""",
17+
"expected": ['Unknown','Unknown'],
18+
}
19+
20+
test='real'
21+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
22+
test_data[test]= {"input":open(input_file,"r+").read().strip(),
23+
"expected": ['339351','24315397'],
24+
}
25+
26+
# -------------------------------- Control program execution -------------------------------- #
27+
28+
case_to_test='real'
29+
part_to_test=2
30+
verbose_level=1
31+
32+
# -------------------------------- Initialize some variables -------------------------------- #
33+
34+
puzzle_input=test_data[case_to_test]['input']
35+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
36+
puzzle_actual_result='Unknown'
37+
38+
39+
# -------------------------------- Actual code execution -------------------------------- #
40+
41+
ifpart_to_test==1:
42+
instructions=list(map(int,puzzle_input.split('\n')))
43+
i=0
44+
step=0
45+
whileTrue:
46+
try:
47+
instruction=instructions[i]
48+
instructions[i]+=1
49+
exceptIndexError:
50+
break
51+
52+
step+=1
53+
54+
i=i+instruction
55+
56+
puzzle_actual_result=step
57+
58+
59+
60+
61+
else:
62+
instructions=list(map(int,puzzle_input.split('\n')))
63+
i=0
64+
step=0
65+
whileTrue:
66+
try:
67+
instruction=instructions[i]
68+
ifinstructions[i]>=3:
69+
instructions[i]-=1
70+
else:
71+
instructions[i]+=1
72+
exceptIndexError:
73+
break
74+
75+
step+=1
76+
77+
i=i+instruction
78+
79+
puzzle_actual_result=step
80+
81+
82+
83+
# -------------------------------- Outputs / results -------------------------------- #
84+
85+
ifverbose_level>=3:
86+
print ('Input : '+puzzle_input)
87+
print ('Expected result : '+str(puzzle_expected_result))
88+
print ('Actual result : '+str(puzzle_actual_result))
89+
90+
91+
92+

‎2017/06-Memory Reallocation.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":"""0 2 7 0""",
8+
"expected": ['5','Unknown'],
9+
}
10+
11+
test+=1
12+
test_data[test]= {"input":"""""",
13+
"expected": ['Unknown','Unknown'],
14+
}
15+
16+
test='real'
17+
test_data[test]= {"input":'14 0 15 12 11 11 3 5 1 6 8 4 9 1 8 4',
18+
"expected": ['11137','1037'],
19+
}
20+
21+
# -------------------------------- Control program execution -------------------------------- #
22+
23+
case_to_test='real'
24+
part_to_test=1
25+
verbose_level=1
26+
27+
# -------------------------------- Initialize some variables -------------------------------- #
28+
29+
puzzle_input=test_data[case_to_test]['input']
30+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
31+
puzzle_actual_result='Unknown'
32+
33+
34+
# -------------------------------- Actual code execution -------------------------------- #
35+
36+
banks_history= [list(map(int,puzzle_input.split(' ')))]
37+
steps=0
38+
whileTrue:
39+
banks=banks_history[steps].copy()
40+
bank_id=min([xforxinrange(len(banks))ifbanks[x]==max(banks)])
41+
redistribute=banks[bank_id]
42+
banks[bank_id]=0
43+
foriinrange(1,redistribute+1):
44+
banks[(bank_id+i)%len(banks)]+=1
45+
46+
steps+=1
47+
ifbanksinbanks_history:
48+
ifpart_to_test==1:
49+
puzzle_actual_result=steps
50+
else:
51+
puzzle_actual_result=steps-min([xforxinrange(len(banks_history))ifbanks_history[x]==banks])
52+
break
53+
54+
banks_history.append(banks)
55+
56+
57+
# -------------------------------- Outputs / results -------------------------------- #
58+
59+
ifverbose_level>=3:
60+
print ('Input : '+puzzle_input)
61+
print ('Expected result : '+str(puzzle_expected_result))
62+
print ('Actual result : '+str(puzzle_actual_result))
63+
64+
65+
66+

‎2017/07-Recursive Circus.py‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":"""pbga (66)
8+
xhth (57)
9+
ebii (61)
10+
havc (66)
11+
ktlj (57)
12+
fwft (72) -> ktlj, cntj, xhth
13+
qoyq (66)
14+
padx (45) -> pbga, havc, qoyq
15+
tknk (41) -> ugml, padx, fwft
16+
jptl (61)
17+
ugml (68) -> gyxo, ebii, jptl
18+
gyxo (61)
19+
cntj (57)""",
20+
"expected": ['Unknown','Unknown'],
21+
}
22+
23+
test+=1
24+
test_data[test]= {"input":"""""",
25+
"expected": ['Unknown','Unknown'],
26+
}
27+
28+
test='real'
29+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
30+
test_data[test]= {"input":open(input_file,"r+").read().strip(),
31+
"expected": ['vtzay','910'],
32+
}
33+
34+
# -------------------------------- Control program execution -------------------------------- #
35+
36+
case_to_test='real'
37+
part_to_test=2
38+
verbose_level=1
39+
40+
# -------------------------------- Initialize some variables -------------------------------- #
41+
42+
puzzle_input=test_data[case_to_test]['input']
43+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
44+
puzzle_actual_result='Unknown'
45+
46+
47+
# -------------------------------- Actual code execution -------------------------------- #
48+
49+
all_held= []
50+
holders= []
51+
individual_weights= {}
52+
branches= {}
53+
forstringinpuzzle_input.split('\n'):
54+
ifstring=='':
55+
continue
56+
57+
individual_weights[string.split(' ')[0]]=int(string.split(' ')[1][1:-1])
58+
59+
is_holder=string.split('->')
60+
iflen(is_holder)==1:
61+
all_held.append(string.split(' ')[0])
62+
continue
63+
64+
holder,weight,_,*held=string.split(' ')
65+
all_held+= [x.replace(',','')forxinheld]
66+
holders.append(holder)
67+
68+
branches[holder]= [x.replace(',','')forxinheld]
69+
70+
forholderinholders:
71+
ifholdernotinall_held:
72+
puzzle_actual_result=holder
73+
break
74+
75+
76+
ifpart_to_test==2:
77+
unknown_weights=holders.copy()
78+
held_weight= {}
79+
total_weight= {x:individual_weights[x]forxinindividual_weightsifxnotinholders}
80+
mismatch= {}
81+
whilelen(unknown_weights):
82+
forholderinunknown_weights:
83+
ifall([xintotal_weightforxinbranches[holder]]):
84+
# We know the weights of all leaves, including sub-towers
85+
86+
held_weight[holder]= [total_weight[x]forxinbranches[holder]]
87+
ifany([x!=held_weight[holder][0]forxinheld_weight[holder]]):
88+
mismatch.update({holder:held_weight[holder]})
89+
total_weight[holder]=sum(held_weight[holder])+individual_weights[holder]
90+
unknown_weights.remove(holder)
91+
92+
# This is very ugly code
93+
# First, determine which mismatch disk has the minimum weight (because that's the closest to the problem)
94+
min_weight=min([yforxinmismatchforyinmismatch[x]])
95+
min_holder= [xforxinmismatchifmin_weightinmismatch[x]][0]
96+
97+
# Then, determine what are the correct and incorrect weights
98+
count_weights= {mismatch[min_holder].count(x):xforxinmismatch[min_holder]}
99+
wrong_weight=count_weights[1]
100+
correct_weight=count_weights[len(mismatch[min_holder])-1]
101+
delta=correct_weight-wrong_weight
102+
103+
# Find which tower has the wrong individual weight, then calculate its new weight
104+
wrong_holder= [xforxinbranches[min_holder]iftotal_weight[x]==wrong_weight][0]
105+
new_weight=individual_weights[wrong_holder]+delta
106+
107+
puzzle_actual_result=new_weight
108+
109+
110+
# -------------------------------- Outputs / results -------------------------------- #
111+
112+
ifverbose_level>=3:
113+
print ('Input : '+puzzle_input)
114+
print ('Expected result : '+str(puzzle_expected_result))
115+
print ('Actual result : '+str(puzzle_actual_result))
116+
117+
118+
119+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":"""b inc 5 if a > 1
8+
a inc 1 if b < 5
9+
c dec -10 if a >= 1
10+
c inc -20 if c == 10""",
11+
"expected": ['Unknown','Unknown'],
12+
}
13+
14+
test+=1
15+
test_data[test]= {"input":"""""",
16+
"expected": ['Unknown','Unknown'],
17+
}
18+
19+
test='real'
20+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
21+
test_data[test]= {"input":open(input_file,"r+").read().strip(),
22+
"expected": ['4416','5199'],
23+
}
24+
25+
# -------------------------------- Control program execution -------------------------------- #
26+
27+
case_to_test='real'
28+
part_to_test=2
29+
verbose_level=1
30+
31+
# -------------------------------- Initialize some variables -------------------------------- #
32+
33+
puzzle_input=test_data[case_to_test]['input']
34+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
35+
puzzle_actual_result='Unknown'
36+
37+
defis_integer (value):
38+
try:
39+
val=int(value)
40+
returnTrue
41+
exceptValueError:
42+
returnFalse
43+
44+
defapply_operation (val1,action,val2):
45+
ifaction=='dec':
46+
returnval1-int(val2)
47+
else:
48+
returnval1+int(val2)
49+
50+
# -------------------------------- Actual code execution -------------------------------- #
51+
registers= {}
52+
max_value=0
53+
forstringinpuzzle_input.split('\n'):
54+
target,action,value,_,source,condition,operand=string.split(' ')
55+
ifnottargetinregisters:
56+
registers[target]=0
57+
ifnotsourceinregisters:
58+
registers[source]=0
59+
60+
ifcondition=='==':
61+
ifregisters[source]==int(operand):
62+
registers[target]=apply_operation (registers[target],action,value)
63+
elifcondition=='!=':
64+
ifregisters[source]!=int(operand):
65+
registers[target]=apply_operation (registers[target],action,value)
66+
elifcondition=='>=':
67+
ifregisters[source]>=int(operand):
68+
registers[target]=apply_operation (registers[target],action,value)
69+
elifcondition=='<=':
70+
ifregisters[source]<=int(operand):
71+
registers[target]=apply_operation (registers[target],action,value)
72+
elifcondition=='>':
73+
ifregisters[source]>int(operand):
74+
registers[target]=apply_operation (registers[target],action,value)
75+
elifcondition=='<':
76+
ifregisters[source]<int(operand):
77+
registers[target]=apply_operation (registers[target],action,value)
78+
79+
max_value=max(max_value,max(registers.values()))
80+
81+
ifpart_to_test==1:
82+
puzzle_actual_result=max(registers.values())
83+
else:
84+
puzzle_actual_result=max_value
85+
86+
87+
88+
# -------------------------------- Outputs / results -------------------------------- #
89+
90+
ifverbose_level>=3:
91+
print ('Input : '+puzzle_input)
92+
print ('Expected result : '+str(puzzle_expected_result))
93+
print ('Actual result : '+str(puzzle_actual_result))
94+
95+
96+
97+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp