-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.py
More file actions
61 lines (44 loc) · 1.41 KB
/
day05.py
File metadata and controls
61 lines (44 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
from aocd import get_data
from dotenv import load_dotenv
from utils import generate_readme
day = int(os.getcwd()[-2:])
year = int(os.getcwd()[-10:][:4])
def get_session() -> str:
load_dotenv()
return os.getenv('SESSION_COOKIE')
def run(data: str = None, part: int = None):
if not data:
if part == 1:
return part1(get_data(get_session(), day=day, year=year).splitlines())
else:
return part2(get_data(get_session(), day=day, year=year).splitlines())
else:
if part == 1:
return part1(data.splitlines())
else:
return part2(data.splitlines())
def part1(data):
return calc(data)
def calc(data):
maximum = 0
for line in data:
line = refactor_line(line)
maximum = max(maximum, int(line, 2))
return maximum
def refactor_line(line):
line = line.replace('F', '0').replace('B', '1')
line = line.replace('R', '1').replace('L', '0')
return line
def part2(data):
possible = set(range(1024))
for line in data:
line = refactor_line(line)
possible.discard(int(line, 2))
for candidate in possible:
if candidate - 1 not in possible and candidate + 1 not in possible:
return candidate
if __name__ == '__main__':
print(f'Part 1: {run(data=None, part=1)}')
print(f'Part 2: {run(data=None, part=2)}')
generate_readme("README", year, day, '../')