Added 2018 day 14, fixed url in day 12 and 13

This commit is contained in:
Akumatic 2019-11-20 12:24:20 +01:00
parent 8e98d6a1ff
commit 01927d65da
4 changed files with 63 additions and 2 deletions

View File

@ -1,4 +1,4 @@
""" https://adventofcode.com/2018/day/5 """
""" https://adventofcode.com/2018/day/12 """
def readFile():
import os.path as p

View File

@ -1,4 +1,4 @@
""" https://adventofcode.com/2018/day/5 """
""" https://adventofcode.com/2018/day/13 """
def readFile(test1 = False, test2 = False):
import os.path as p

60
2018/day14.py Normal file
View File

@ -0,0 +1,60 @@
""" https://adventofcode.com/2018/day/14 """
def readFile():
import os.path as p
dName = p.dirname(__file__)
fName = p.basename(__file__).split(".")[0]
with open(p.join(dName, "input", f"{fName}.txt"), "r") as f:
return f.read()
def part1(vals):
border = int(vals)
scores = [3, 7]
elf1 = 0
elf2 = 1
i = 2
while i < border + 10:
new = scores[elf1] + scores[elf2]
if new > 9:
scores.append(new // 10)
scores.append(new % 10)
i += 2
else:
scores.append(new)
i += 1
elf1 = (elf1 + 1 + scores[elf1]) % i
elf2 = (elf2 + 1 + scores[elf2]) % i
return "".join([str(i) for i in scores[border:border + 10]])
def part2(vals):
size = len(vals)
comp = [int(c) for c in vals]
scores = [3, 7]
elf1 = 0
elf2 = 1
i = 2
while True:
new = scores[elf1] + scores[elf2]
if new > 9:
scores.append(new // 10)
scores.append(new % 10)
i += 2
if scores[-(size + 1):-1] == comp:
return i - (size + 1)
else:
scores.append(new)
i += 1
if scores[-size:] == comp:
return i - size
elf1 = (elf1 + 1 + scores[elf1]) % i
elf2 = (elf2 + 1 + scores[elf2]) % i
if __name__ == "__main__":
vals = readFile()
print(f"Part 1: {part1(vals)}")
print(f"Part 2: {part2(vals)}")

1
2018/input/day14.txt Normal file
View File

@ -0,0 +1 @@
607331