Skip to content

Instantly share code, notes, and snippets.

@h3matite
Created December 2, 2019 06:27
Show Gist options
  • Save h3matite/3d40f126bd6436529334ad4f9ded4f46 to your computer and use it in GitHub Desktop.
Save h3matite/3d40f126bd6436529334ad4f9ded4f46 to your computer and use it in GitHub Desktop.
Advent of Code Day 2
def run_program(data):
for i in range(0, len(data), 4):
if i == 0 or i%4 == 0:
if data[i] == 99:
return data[0]
elif data[i] == 1:
data[data[i+3]] = data[data[i+1]] + data[data[i+2]]
elif data[i] == 2:
data[data[i+3]] = data[data[i+1]] * data[data[i+2]]
else:
print("---------------------------FOUND BAD INSTRUCTION CODE")
return -1
return data[0]
## Load Data
with open("input.txt", "r") as f:
data = f.readlines()
## Format Data, Set Variables
data = data[0].split(",")
data = [int(x) for x in data]
data[1] = 12
data[2] = 2
## Run Program
print("Day2, Part1 --", run_program(data))
def run_program(data):
for i in range(0, len(data), 4):
if i == 0 or i%4 == 0:
if data[i] == 99:
return data[0]
elif data[i] == 1:
data[data[i+3]] = data[data[i+1]] + data[data[i+2]]
elif data[i] == 2:
data[data[i+3]] = data[data[i+1]] * data[data[i+2]]
else:
print("---------------------------FOUND BAD INSTRUCTION CODE")
return -1
return data[0]
def find_corret_inputs(data, pointer_code):
for first_i in range(0, 99):
for second_i in range(0, 99):
new_data = list(data)
new_data[1], new_data[2] = first_i, second_i
if run_program(new_data) == pointer_code:
return (100 * first_i + second_i)
else:
del(new_data)
## Load Data
with open("input.txt", "r") as f:
data = f.readlines()
## Format Data
data = data[0].split(",")
data = [int(x) for x in data]
pointer_code = 19690720
## Run Program
print("Day2, Part2 --", find_corret_inputs(data, pointer_code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment