Added 2018 day 4 to 5

This commit is contained in:
Akumatic 2019-11-15 01:12:58 +01:00
parent f7972bbce8
commit 0723fd7dd2
4 changed files with 1183 additions and 0 deletions

73
2018/day04.py Normal file
View File

@ -0,0 +1,73 @@
""" https://adventofcode.com/2018/day/4 """
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:
l = [line[:-1] for line in f.readlines()]
l.sort()
return l
class Guard:
def __init__(self, id):
self.id = int(id)
self.idStr = id
self.timeAsleep = 0
self.minutes = [0 for min in range(60)]
def sleep(self, start, end):
self.timeAsleep += (end - start)
for i in range(start, end):
self.minutes[i] += 1
def evaluateLines(vals : list):
curID = -1
start = -1
guards = {}
for val in vals:
timestamp = val[1:17]
event = val[19:]
if "#" in event:
curID = event.split()[1][1:]
if curID not in guards:
guards[curID] = Guard(curID)
elif event == "falls asleep":
start = int(timestamp[14:16])
else: # event == "wakes up"
end = int(timestamp[14:16])
guards[curID].sleep(start, end)
return guards
def part1(guards : dict):
maxID = ""
maxSleep = 0
for guard in guards:
if guards[guard].timeAsleep > maxSleep:
maxSleep = guards[guard].timeAsleep
maxID = guard
guard = guards[maxID]
return guard.id * guard.minutes.index(max(guard.minutes))
def part2(guards : dict):
maxID = ""
maxAmount = 0
for guard in guards:
cur = max(guards[guard].minutes)
if cur > maxAmount:
maxAmount = cur
maxID = guard
guard = guards[maxID]
return guard.id * guard.minutes.index(max(guard.minutes))
if __name__ == "__main__":
vals = readFile()
guards = evaluateLines(vals)
print(f"Part 1: {part1(guards)}")
print(f"Part 2: {part2(guards)}")

32
2018/day05.py Normal file
View File

@ -0,0 +1,32 @@
""" https://adventofcode.com/2018/day/5 """
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 list(f.read())
def part1(vals):
i = 0
while i < len(vals) - 1:
if vals[i].lower() == vals[i+1].lower() and vals[i] != vals[i+1]:
vals.pop(i)
vals.pop(i)
i = max(i - 1, 0)
else:
i += 1
return len(vals)
def part2(vals):
l = []
for i in range(65, 91):
temp = list("".join(vals).replace(chr(i), "").replace(chr(i+32), ""))
l.append(part1(temp))
return min(l)
if __name__ == "__main__":
vals = readFile()
print(f"Part 1: {part1(vals)}")
print(f"Part 2: {part2(vals)}")

1077
2018/input/day04.txt Normal file

File diff suppressed because it is too large Load Diff

1
2018/input/day05.txt Normal file

File diff suppressed because one or more lines are too long