Start solving 2018 problems
I have also updated get.py to download the problems as `d<day>.txt` instead of `i<day>.txt`. That allows me to get the day input via `__input__.replace('.py', '.txt')` which is a little more concise. I don't know why I didn't do this earlier.
This commit is contained in:
parent
dcfae2cb13
commit
3a915cb9e3
28
2018/d1.py
Normal file
28
2018/d1.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
def part_1(data):
|
||||||
|
r = 0
|
||||||
|
for x in data.splitlines():
|
||||||
|
r += int(x)
|
||||||
|
print(r)
|
||||||
|
|
||||||
|
|
||||||
|
def part_2(data):
|
||||||
|
seen = set()
|
||||||
|
r = 0
|
||||||
|
while True:
|
||||||
|
for x in data.splitlines():
|
||||||
|
r += int(x)
|
||||||
|
if r in seen:
|
||||||
|
print(r)
|
||||||
|
return
|
||||||
|
seen.add(r)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with open("i1.txt") as f:
|
||||||
|
data = f.read()
|
||||||
|
part_1(data)
|
||||||
|
part_2(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
53
2018/d2.py
Normal file
53
2018/d2.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
from lib import LETTERS_LOWER
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
two_count, three_count = 0, 0
|
||||||
|
for line in data.splitlines():
|
||||||
|
count = [line.count(c) for c in LETTERS_LOWER if line.count(c) > 1]
|
||||||
|
if 2 in count:
|
||||||
|
two_count += 1
|
||||||
|
if 3 in count:
|
||||||
|
three_count += 1
|
||||||
|
r = two_count * three_count
|
||||||
|
print(r)
|
||||||
|
|
||||||
|
|
||||||
|
def equal(a, b):
|
||||||
|
c = 0
|
||||||
|
if len(a) != len(b):
|
||||||
|
return False
|
||||||
|
for i in range(len(a)):
|
||||||
|
if a[i] != b[i]:
|
||||||
|
c += 1
|
||||||
|
if c == 1:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def strip(a, b):
|
||||||
|
r = ""
|
||||||
|
for a, b in zip(a, b):
|
||||||
|
if a == b:
|
||||||
|
r += a
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def part_2(data):
|
||||||
|
lines = list(data.splitlines())
|
||||||
|
for i in range(len(lines)):
|
||||||
|
for j in range(i + 1, len(lines)):
|
||||||
|
a, b = lines[i], lines[j]
|
||||||
|
if equal(a, b):
|
||||||
|
print(strip(a, b))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with open("i2.txt") as f:
|
||||||
|
data = f.read()
|
||||||
|
part_1(data)
|
||||||
|
part_2(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
44
2018/d3.py
Normal file
44
2018/d3.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
from lib import str_to_ints
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
claims = defaultdict(int)
|
||||||
|
for line in data.splitlines():
|
||||||
|
id, x, y, w, h = str_to_ints(line)
|
||||||
|
for dx in range(w):
|
||||||
|
for dy in range(h):
|
||||||
|
claims[(x + dx, y + dy)] += 1
|
||||||
|
|
||||||
|
r = sum([1 for v in claims.values() if v > 1])
|
||||||
|
print(r)
|
||||||
|
|
||||||
|
|
||||||
|
def part_2(data):
|
||||||
|
claims = defaultdict(list)
|
||||||
|
all_ids = set()
|
||||||
|
for line in data.splitlines():
|
||||||
|
id, x, y, w, h = str_to_ints(line)
|
||||||
|
for dx in range(w):
|
||||||
|
for dy in range(h):
|
||||||
|
claims[(x + dx, y + dy)].append(id)
|
||||||
|
all_ids.add(id)
|
||||||
|
|
||||||
|
for xs in claims.values():
|
||||||
|
if len(xs) > 1:
|
||||||
|
for x in xs:
|
||||||
|
if x in all_ids:
|
||||||
|
all_ids.remove(x)
|
||||||
|
assert len(all_ids) == 1
|
||||||
|
print(all_ids.pop())
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with open("i3.txt") as f:
|
||||||
|
data = f.read()
|
||||||
|
part_1(data)
|
||||||
|
part_2(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
67
2018/d4.py
Normal file
67
2018/d4.py
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
from lib import str_to_ints
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
shifts = sorted(data.splitlines())
|
||||||
|
guards = defaultdict(list)
|
||||||
|
guard = None
|
||||||
|
for s in shifts:
|
||||||
|
try:
|
||||||
|
_, _, _, h, m, guard = str_to_ints(s)
|
||||||
|
except ValueError:
|
||||||
|
_, _, _, h, m = str_to_ints(s)
|
||||||
|
assert guard is not None
|
||||||
|
guards[guard].append((h, m))
|
||||||
|
|
||||||
|
guard_sleep_times = {}
|
||||||
|
guard_heat_maps = {}
|
||||||
|
for guard, times in guards.items():
|
||||||
|
heat_map = [0 for _ in range(60)]
|
||||||
|
total_time = 0
|
||||||
|
start = None
|
||||||
|
for h, m in times:
|
||||||
|
if start is None:
|
||||||
|
start = (h, m)
|
||||||
|
else:
|
||||||
|
delta = (h - start[0]) * 60 + (m - start[1])
|
||||||
|
total_time += delta
|
||||||
|
for t in range(start[1], m):
|
||||||
|
heat_map[t] += 1
|
||||||
|
start = None
|
||||||
|
guard_sleep_times[guard] = total_time
|
||||||
|
guard_heat_maps[guard] = heat_map
|
||||||
|
|
||||||
|
max_guard, max_time = None, 0
|
||||||
|
for guard, time in guard_sleep_times.items():
|
||||||
|
if time > max_time:
|
||||||
|
max_guard = guard
|
||||||
|
max_time = time
|
||||||
|
|
||||||
|
assert type(max_guard) is int
|
||||||
|
max_minute, max_asleep = 0, 0
|
||||||
|
for minute, asleep in enumerate(guard_heat_maps[max_guard]):
|
||||||
|
if asleep > max_asleep:
|
||||||
|
max_minute = minute
|
||||||
|
max_asleep = asleep
|
||||||
|
print(max_guard * max_minute)
|
||||||
|
|
||||||
|
max_guard, max_minute, max_asleep = 0, 0, 0
|
||||||
|
for guard in guard_sleep_times.keys():
|
||||||
|
for minute, asleep in enumerate(guard_heat_maps[guard]):
|
||||||
|
if asleep > max_asleep:
|
||||||
|
max_minute = minute
|
||||||
|
max_asleep = asleep
|
||||||
|
max_guard = guard
|
||||||
|
print(max_guard * max_minute)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
input_file = __file__.replace(".py", ".txt")
|
||||||
|
with open(input_file) as f:
|
||||||
|
data = f.read()
|
||||||
|
part_1(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
1
2018/lib.py
Symbolic link
1
2018/lib.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../lib.py
|
@ -86,3 +86,11 @@ written in Python.
|
|||||||
- Day 23: Multiple days... but super fun.
|
- Day 23: Multiple days... but super fun.
|
||||||
- Day 24: 15:45 (48th)
|
- Day 24: 15:45 (48th)
|
||||||
- Day 25: 41:00
|
- Day 25: 41:00
|
||||||
|
|
||||||
|
# 2018
|
||||||
|
|
||||||
|
- Day 1: 1:49
|
||||||
|
- Day 2: 10:53
|
||||||
|
- Day 3: 6:16
|
||||||
|
- Day 4: 25:16
|
||||||
|
- Day 5:
|
||||||
|
4
get.py
4
get.py
@ -12,12 +12,14 @@ year = sys.argv[1]
|
|||||||
day = sys.argv[2]
|
day = sys.argv[2]
|
||||||
|
|
||||||
session_cookie = get_password('aoc-session-cookie', 'felixm')
|
session_cookie = get_password('aoc-session-cookie', 'felixm')
|
||||||
|
assert session_cookie is not None
|
||||||
|
|
||||||
url = f"https://adventofcode.com/{year}/day/{day}/input"
|
url = f"https://adventofcode.com/{year}/day/{day}/input"
|
||||||
cookies = {'session': session_cookie}
|
cookies = {'session': session_cookie}
|
||||||
response = requests.get(url, cookies=cookies)
|
response = requests.get(url, cookies=cookies)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
filename = f"i{day}.txt"
|
filename = f"d{day}.txt"
|
||||||
with open(filename, 'w') as file:
|
with open(filename, 'w') as file:
|
||||||
file.write(response.text)
|
file.write(response.text)
|
||||||
print(f"Year {year} {filename} written.")
|
print(f"Year {year} {filename} written.")
|
||||||
|
Loading…
Reference in New Issue
Block a user