Days 1 and 2 parts 2
This commit is contained in:
parent
2d9bba8a15
commit
aeaec72182
|
@ -1,11 +1,17 @@
|
||||||
def get_fuel(mass):
|
def get_fuel(mass):
|
||||||
return mass // 3 - 2
|
return max(0, mass // 3 - 2)
|
||||||
|
|
||||||
needed_fuel = 0
|
needed_fuel = 0
|
||||||
with open("day1.txt", "rt") as input_file:
|
with open("day1.txt", "rt") as input_file:
|
||||||
for line in input_file.readlines():
|
for line in input_file.readlines():
|
||||||
mass = int(line.strip())
|
mass = int(line.strip())
|
||||||
fuel = get_fuel(mass)
|
fuel = get_fuel(mass)
|
||||||
|
|
||||||
|
# Part 2. Comment from line 11 to 14 included for part 1.
|
||||||
|
fuel_for_fuel = fuel
|
||||||
|
while fuel_for_fuel > 0:
|
||||||
|
fuel_for_fuel = get_fuel(fuel_for_fuel)
|
||||||
|
fuel += fuel_for_fuel
|
||||||
needed_fuel += fuel
|
needed_fuel += fuel
|
||||||
|
|
||||||
print(needed_fuel)
|
print(needed_fuel)
|
||||||
|
|
54
2019/day2.py
54
2019/day2.py
|
@ -1,31 +1,35 @@
|
||||||
import sys
|
import itertools
|
||||||
|
|
||||||
CODES = None
|
from intcode import Intcode
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
with open("day2.txt", "rt") as input_file:
|
with open("day2.txt", "rt") as input_file:
|
||||||
CODES = [int(i) for i in input_file.read().strip().split(",")]
|
text = input_file.readlines()[0].rstrip()
|
||||||
|
codes = Intcode.parse_input(text)
|
||||||
|
|
||||||
CODES[1] = 12
|
# Part 1
|
||||||
CODES[2] = 2
|
codes_p1 = codes.copy()
|
||||||
|
codes_p1[1] = 12
|
||||||
|
codes_p1[2] = 2
|
||||||
|
ic = Intcode(codes_p1)
|
||||||
|
ic.run()
|
||||||
|
print(f"Value at pos 0: {ic._memory[0]}.")
|
||||||
|
|
||||||
ip = 0
|
# Part 2
|
||||||
while True:
|
noun, verb = 0, 0
|
||||||
code = CODES[ip]
|
for x, y in itertools.permutations(range(1, 100), 2):
|
||||||
if code == 1:
|
codes_p2 = codes.copy()
|
||||||
operand1_pos = CODES[ip + 1]
|
codes_p2[1] = x
|
||||||
operand2_pos = CODES[ip + 2]
|
codes_p2[2] = y
|
||||||
output_pos = CODES[ip + 3]
|
ic = Intcode(codes_p2)
|
||||||
CODES[output_pos] = CODES[operand1_pos] + CODES[operand2_pos]
|
ic.run()
|
||||||
ip += 4
|
if ic._memory[0] == 19690720:
|
||||||
elif code == 2:
|
noun, verb = x, y
|
||||||
operand1_pos = CODES[ip + 1]
|
|
||||||
operand2_pos = CODES[ip + 2]
|
|
||||||
output_pos = CODES[ip + 3]
|
|
||||||
CODES[output_pos] = CODES[operand1_pos] * CODES[operand2_pos]
|
|
||||||
ip += 4
|
|
||||||
elif code == 99:
|
|
||||||
break
|
break
|
||||||
else:
|
print(f"Found noun={noun} and verb={verb}.")
|
||||||
print("Wrong opcode:", code)
|
print(f"Answer: {100 * noun + verb}")
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
print("Value at pos0:", CODES[0])
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue