Days 1 and 2

This commit is contained in:
Adrien Abraham 2019-12-02 13:59:18 +01:00
commit 7e025a3721
4 changed files with 60 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
day*.txt

11
2019/day1.py Normal file
View file

@ -0,0 +1,11 @@
def get_fuel(mass):
return mass // 3 - 2
needed_fuel = 0
with open("day1.txt", "rt") as input_file:
for line in input_file.readlines():
mass = int(line.strip())
fuel = get_fuel(mass)
needed_fuel += fuel
print(needed_fuel)

31
2019/day2.py Normal file
View file

@ -0,0 +1,31 @@
import sys
CODES = None
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
while True:
code = CODES[ip]
if code == 1:
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 == 2:
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
else:
print("Wrong opcode:", code)
sys.exit()
print("Value at pos0:", CODES[0])

17
2019/fetch.py Normal file
View file

@ -0,0 +1,17 @@
import os
import requests
import sys
if len(sys.argv) != 2:
print("Usage: <fetch> day")
sys.exit()
day = sys.argv[1]
URL = "https://adventofcode.com/2019/day/{}/input"
SESSION_ID = os.environ["AOC_SESSION"]
response = requests.get(URL.format(day), cookies={"session": SESSION_ID})
response.raise_for_status()
with open("day{}.txt".format(day), "wt") as output_file:
output_file.write(response.text)