-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
40 lines (34 loc) · 1.36 KB
/
2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def processIntcode(intcode):
"""Processes an Intcode and returns the resulting output integer."""
memory = intcode[:]
i = 0
while memory[i] is not 99:
if memory[i] is 1:
memory[memory[i+3]] = memory[memory[i+1]] + memory[memory[i+2]]
i += 4
elif memory[i] is 2:
memory[memory[i+3]] = memory[memory[i+1]] * memory[memory[i+2]]
i += 4
return memory[0]
def processIntcodeWithInputs(intcode, noun, verb):
"""Processes an Intcode, with given parameters substituted for positions 1 (noun) and 2 (verb), and returns the resulting output integer."""
memory = intcode[:]
memory[1] = noun
memory[2] = verb
return processIntcode(memory)
def parseCommaSeparatedIntegers(string):
"""Takes a string of comma-separated integers and returns an integer list."""
return list(map(int, string.split(',')))
if __name__ == "__main__":
with open("input/2.input", "r") as file:
intcode = parseCommaSeparatedIntegers(file.read())
# Part 1
print("PART 1 | Output of Intcode: ", processIntcodeWithInputs(intcode, 12, 2))
# Part 2
target_output = 19690720
for noun in range(100):
for verb in range(100):
output = processIntcodeWithInputs(intcode, noun, verb)
if output == target_output:
print("PART 2 | Target output", target_output, "reached with Noun:", noun, "Verb:", verb, "Result:", (100*noun + verb))
print("PART 2 | All possible noun/verb combinations searched.")