sancho1241
: [][sancho1241]
sancho1241 |
this documentation is autogenerated. Add a README.adoc
to your solution to take over the control of this :-)
I'm not a pro.
Quick and dirty solutions just for fun to learn Python.
Since part 1 was a warm up I decided to go for the recursion for part 2.
Needed to visit google but eventually made it.
# paste puzzle input here and convert to a list
def puzzle_input_list ():
puzzle_input = '''
114106
87170
133060
70662
134140
125874
50081
133117
100409
95098
70251
134043
87501
85034
110678
80615
64647
88555
106387
143755
101246
142348
92684
62051
94894
65873
78473
64042
147982
145898
85591
121413
132163
94351
80080
73554
106598
135174
147951
132517
50925
115752
114022
73448
50451
56205
81474
90028
124879
137452
91036
87221
126590
130592
91503
148689
86526
105924
52411
146708
149280
52100
80024
115412
91204
132726
59837
129863
140980
109574
103013
84105
138883
144861
126708
140290
54417
138154
125187
91537
90338
61150
61702
95888
100484
82115
122141
63986
138234
54150
57651
124570
88460
112144
112334
119114
58220
143221
86568
148706
'''
puzzle_input_list = puzzle_input.split()
return puzzle_input_list
# examples given in the puzzle for testing
def test_data():
return [12,14,1969,100756]
#calculate fuel amount per given mass
def fuel (mass):
required_fuel = (mass//3-2)
if required_fuel <= 0:
return 0
else:
return required_fuel + fuel(required_fuel)
# iterate through list calculate mass and add to total sum
total = 0
for x in puzzle_input_list():
#for x in test_data():
#print (int(x))
total += fuel (int(x))
print (total)
def puzzle_input_list ():
puzzle_input = '''
114106
87170
133060
70662
134140
125874
50081
133117
100409
95098
70251
134043
87501
85034
110678
80615
64647
88555
106387
143755
101246
142348
92684
62051
94894
65873
78473
64042
147982
145898
85591
121413
132163
94351
80080
73554
106598
135174
147951
132517
50925
115752
114022
73448
50451
56205
81474
90028
124879
137452
91036
87221
126590
130592
91503
148689
86526
105924
52411
146708
149280
52100
80024
115412
91204
132726
59837
129863
140980
109574
103013
84105
138883
144861
126708
140290
54417
138154
125187
91537
90338
61150
61702
95888
100484
82115
122141
63986
138234
54150
57651
124570
88460
112144
112334
119114
58220
143221
86568
148706
'''
#print (puzzle_input)
puzzle_input_list = puzzle_input.split()
return puzzle_input_list
# examples given in the puzzle
def test_data():
return [12,14,1969,100756]
#calculate fuel amount per given mass
def fuel (mass):
required_fuel = (mass//3-2)
if required_fuel <= 0:
return 0
else:
return required_fuel + fuel(required_fuel)
# iterate through list calculate mass and add to total sum
total = 0
for x in puzzle_input_list():
#for x in test_data():
#print (int(x))
total += fuel (int(x))
print (total)
this documentation is autogenerated. Add a README.adoc
to your solution to take over the control of this :-)
import operator
# assign the puzzle input as a list
puzzle_input = [1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,9,23,1,5,23,27,1,27,9,31,1,6,31,35,2,35,9,39,1,39,6,43,2,9,43,47,1,47,6,51,2,51,9,55,1,5,55,59,2,59,6,63,1,9,63,67,1,67,10,71,1,71,13,75,2,13,75,79,1,6,79,83,2,9,83,87,1,87,6,91,2,10,91,95,2,13,95,99,1,9,99,103,1,5,103,107,2,9,107,111,1,111,5,115,1,115,5,119,1,10,119,123,1,13,123,127,1,2,127,131,1,131,13,0,99,2,14,0,0]
#print (f"puzzle Input beginning: {puzzle_input}")
# create dictionary with operator code - function mapping
dict_func = {1:operator.add,
2:operator.mul,
99:'break'}
def intcode_computer(orig_puzzle_input, noun, verb):
'''
:param orig_puzzle_input: the original puzzle input
:param noun: value for 1st position
:param verb: value for 2nd position
:return: True if value 19690720 found, False otherwise
'''
# iterate through the list in 4-steps from operator to operator
puzzle_input=orig_puzzle_input.copy()
puzzle_input[1]=noun
puzzle_input[2] = verb
for int_code_pos in range (0,len(puzzle_input),4):
# operator found
operator_func = dict_func[puzzle_input[int_code_pos]]
if operator_func == 'break':
#Program halted
break
elif operator_func == None:
#unknown operating code
print (f"Unknown operating code {puzzle_input[int_code_pos]} ")
else:
#execute operation
puzzle_input[puzzle_input[int_code_pos+3]] = operator_func(puzzle_input[puzzle_input[int_code_pos+1]],puzzle_input[puzzle_input[int_code_pos+2]] )
#print (f"result with noun {noun}, verb {verb}: {puzzle_input}")
if puzzle_input[0] == 19690720:
return True
else:
return False
noun = 0
verb = 0
for noun in range (0,100):
for verb in range (0,100):
#print(f"result with noun {noun}, verb {verb}: {puzzle_input}")
if intcode_computer(puzzle_input,noun,verb):
break
else:
continue
break
#print (f"puzzle Input after function: {puzzle_input}")
print ("Value at pos 0: {a} ".format(a=puzzle_input[0]))
print (f"noun: {noun}, verb: {verb}, output: {100*noun+verb}")
I decided to put the operators in a dictionary refering to the functions.
I assume I could have used list comprehension for the calculation ...
For part 2 I created 2 nested loops
import operator
# assign the puzzle input as a list
puzzle_input = [1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,9,23,1,5,23,27,1,27,9,31,1,6,31,35,2,35,9,39,1,39,6,43,2,9,43,47,1,47,6,51,2,51,9,55,1,5,55,59,2,59,6,63,1,9,63,67,1,67,10,71,1,71,13,75,2,13,75,79,1,6,79,83,2,9,83,87,1,87,6,91,2,10,91,95,2,13,95,99,1,9,99,103,1,5,103,107,2,9,107,111,1,111,5,115,1,115,5,119,1,10,119,123,1,13,123,127,1,2,127,131,1,131,13,0,99,2,14,0,0]
# create dictionary with operator code - function mapping
dict_func = {1:operator.add,
2:operator.mul,
99:'break'}
# iterate through the list in 4-steps from operator to operator
for int_code_pos in range (0,len(puzzle_input),4):
# operator found
operator_func = dict_func[puzzle_input[int_code_pos]]
if operator_func == 'break':
#Program halted
break
elif operator_func == None:
#unknown operating code
print (f"Unknown operating code {puzzle_input[int_code_pos]} ")
else:
#execute operation
puzzle_input[puzzle_input[int_code_pos+3]] = operator_func(puzzle_input[puzzle_input[int_code_pos+1]],puzzle_input[puzzle_input[int_code_pos+2]] )
print ("Value at pos 0: {a} ".format(a=puzzle_input[0]))