-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.py
More file actions
67 lines (52 loc) · 1.52 KB
/
day03.py
File metadata and controls
67 lines (52 loc) · 1.52 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
62
63
64
65
66
67
import math
import os
from functools import lru_cache
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))
else:
return part2(get_data(get_session(), day=day, year=year))
else:
if part == 1:
return part1(data)
else:
return part2(data)
def part1(data):
return calc(data=data, right=3, down=1)
@lru_cache(maxsize=None)
def calc(data, right, down):
count = 0
position = [0, 0]
lines = data.splitlines()
width = len(lines[0])
while position[1] <= len(lines):
position[0] += right
position[1] += down
if position[0] >= width:
position[0] = position[0] - width
x, y = position
if y >= len(lines):
break
if lines[y][x] != '.':
count += 1
return count
def part2(data):
slopes = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]]
done = []
for slope in slopes:
x, y = slope
done.append(calc(data=data, right=x, down=y))
return math.prod(done)
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, '../')