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)
|
||||||
|
|
56
2019/day2.py
56
2019/day2.py
|
@ -1,31 +1,35 @@
|
||||||
import sys
|
import itertools
|
||||||
|
|
||||||
CODES = None
|
from intcode import Intcode
|
||||||
with open("day2.txt", "rt") as input_file:
|
|
||||||
CODES = [int(i) for i in input_file.read().strip().split(",")]
|
|
||||||
|
|
||||||
CODES[1] = 12
|
|
||||||
CODES[2] = 2
|
|
||||||
|
|
||||||
ip = 0
|
def main():
|
||||||
while True:
|
with open("day2.txt", "rt") as input_file:
|
||||||
code = CODES[ip]
|
text = input_file.readlines()[0].rstrip()
|
||||||
if code == 1:
|
codes = Intcode.parse_input(text)
|
||||||
operand1_pos = CODES[ip + 1]
|
|
||||||
operand2_pos = CODES[ip + 2]
|
# Part 1
|
||||||
output_pos = CODES[ip + 3]
|
codes_p1 = codes.copy()
|
||||||
CODES[output_pos] = CODES[operand1_pos] + CODES[operand2_pos]
|
codes_p1[1] = 12
|
||||||
ip += 4
|
codes_p1[2] = 2
|
||||||
elif code == 2:
|
ic = Intcode(codes_p1)
|
||||||
operand1_pos = CODES[ip + 1]
|
ic.run()
|
||||||
operand2_pos = CODES[ip + 2]
|
print(f"Value at pos 0: {ic._memory[0]}.")
|
||||||
output_pos = CODES[ip + 3]
|
|
||||||
CODES[output_pos] = CODES[operand1_pos] * CODES[operand2_pos]
|
# Part 2
|
||||||
ip += 4
|
noun, verb = 0, 0
|
||||||
elif code == 99:
|
for x, y in itertools.permutations(range(1, 100), 2):
|
||||||
|
codes_p2 = codes.copy()
|
||||||
|
codes_p2[1] = x
|
||||||
|
codes_p2[2] = y
|
||||||
|
ic = Intcode(codes_p2)
|
||||||
|
ic.run()
|
||||||
|
if ic._memory[0] == 19690720:
|
||||||
|
noun, verb = x, y
|
||||||
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