Created
December 3, 2019 06:53
-
-
Save theglauber/1795aebbae6e8f2383d43c7a088fedf1 to your computer and use it in GitHub Desktop.
Advent of code 2019 day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python | |
program = [ 1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,10,23,2,10,23,27,1,27,6,31,1,13,31,35,1,13,35,39,1,39,10,43,2,43,13,47,1,47,9,51,2,51,13,55,1,5,55,59,2,59,9,63,1,13,63,67,2,13,67,71,1,71,5,75,2,75,13,79,1,79,6,83,1,83,5,87,2,87,6,91,1,5,91,95,1,95,13,99,2,99,6,103,1,5,103,107,1,107,9,111,2,6,111,115,1,5,115,119,1,119,2,123,1,6,123,0,99,2,14,0,0 ] | |
def chunker(seq, size): | |
return (seq[pos:pos + size] for pos in range(0, len(seq), size)) | |
# restore the "1202 error" | |
program[1] = 12 | |
program[2] = 2 | |
for group in chunker(program, 4): | |
#print(repr(group)) | |
operator = group[0] | |
input_pos1 = group[1] | |
input_pos2 = group[2] | |
output_pos = group[3] | |
input1 = program[input_pos1] | |
input2 = program[input_pos2] | |
output = None | |
if operator == 1: | |
output = input1 + input2 | |
elif operator == 2: | |
output = input1 * input2 | |
elif operator == 99: | |
print( "Result is {}".format(program[0])) | |
exit(0) | |
else: | |
print( "Invalid operator: {}".format(operator)) | |
program[output_pos] = output | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python | |
import copy | |
program = [ 1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,10,23,2,10,23,27,1,27,6,31,1,13,31,35,1,13,35,39,1,39,10,43,2,43,13,47,1,47,9,51,2,51,13,55,1,5,55,59,2,59,9,63,1,13,63,67,2,13,67,71,1,71,5,75,2,75,13,79,1,79,6,83,1,83,5,87,2,87,6,91,1,5,91,95,1,95,13,99,2,99,6,103,1,5,103,107,1,107,9,111,2,6,111,115,1,5,115,119,1,119,2,123,1,6,123,0,99,2,14,0,0 ] | |
def chunker(seq, size): | |
return (seq[pos:pos + size] for pos in range(0, len(seq), size)) | |
def compute(program): | |
my_program = copy.deepcopy(program) | |
for group in chunker(my_program, 4): | |
#print(repr(group)) | |
operator = group[0] | |
input_pos1 = group[1] | |
input_pos2 = group[2] | |
output_pos = group[3] | |
input1 = my_program[input_pos1] | |
input2 = my_program[input_pos2] | |
output = None | |
if operator == 1: | |
output = input1 + input2 | |
elif operator == 2: | |
output = input1 * input2 | |
elif operator == 99: | |
return(my_program[0]) | |
else: | |
raise Exception("Invalid operator: {}".format(operator)) | |
my_program[output_pos] = output | |
for a in range(0,99): | |
for b in range(0,99): | |
program[1] = a | |
program[2] = b | |
result = compute(program) | |
print("{}, {} => {}".format(a,b,result)) | |
if result == 19690720: | |
exit(0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment