-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_02.py
More file actions
75 lines (46 loc) · 1.86 KB
/
Day_02.py
File metadata and controls
75 lines (46 loc) · 1.86 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
68
69
70
71
72
73
74
75
from time import perf_counter
start_time = perf_counter()
LIMITS = {"red": 12, "green": 13, "blue": 14}
def open_file(file_name: str = "Day_02.txt") -> str:
with open(file_name) as f:
return f.read()
def get_game(line: str) -> list[str]:
line_split = line.split(": ")
return line_split[1].split("; ")
def is_round_possible(round: str) -> bool:
result_dict = get_round_dict(round)
for color in LIMITS:
if result_dict[color] > LIMITS[color]:
return False
return True
def get_round_dict(round: str) -> dict[str, int]:
result_dictionary = {"red": 0, "green": 0, "blue": 0}
for colors in round.split(", "):
for color in result_dictionary:
if color in colors:
result_dictionary[color] = int(colors.split(" ")[0])
return result_dictionary
def get_game_value(game_number: int, game: list[str]) -> int:
is_game_possible = all(is_round_possible(round) for round in game)
return game_number * is_game_possible
def get_game_power(game: list[str]) -> int:
min_blocks = {"red": 0, "green": 0, "blue": 0}
for round in game:
round_dict = get_round_dict(round)
for color in min_blocks:
min_blocks[color] = max(round_dict[color], min_blocks[color])
return min_blocks["red"] * min_blocks["green"] * min_blocks["blue"]
def part_one(games_list: list[str]) -> int:
return sum(
get_game_value(game_number + 1, get_game(line))
for game_number, line in enumerate(games_list)
)
def part_two(games_list: list[str]) -> int:
return sum(get_game_power(get_game(line)) for line in games_list)
def main():
games_list = open_file().splitlines()
print("Part 1: ", part_one(games_list))
print("Part 2: ", part_two(games_list))
if __name__ == "__main__":
main()
print("Time elapsed: ", perf_counter() - start_time)