Day 2 completed

This commit is contained in:
Fedaya 2024-12-01 15:22:59 +01:00
parent 5e782cf3d7
commit 42bab0cb9e
7 changed files with 114 additions and 0 deletions

6
day2/common.py Normal file
View File

@ -0,0 +1,6 @@
from typing import Any
def parse(filename: str) -> Any | None:
with open(filename) as f:
return [tuple(a[:-1].split()) for a in f.readlines()]

37
day2/part1.py Normal file
View File

@ -0,0 +1,37 @@
from common import parse
def round(game: tuple[str, str]) -> int:
if game[0] == "A":
if game[1] == "X":
return 1 + 3
elif game[1] == "Y":
return 2 + 6
else:
return 3 + 0
elif game[0] == "B":
if game[1] == "X":
return 1 + 0
elif game[1] == "Y":
return 2 + 3
else:
return 3 + 6
else:
if game[1] == "X":
return 1 + 6
elif game[1] == "Y":
return 2 + 0
else:
return 3 + 3
def solve(strategy: list[tuple[str, str]]) -> int:
return sum(map(round, strategy))
def main():
print(solve(parse("input")))
if __name__ == "__main__":
main()

37
day2/part2.py Normal file
View File

@ -0,0 +1,37 @@
from common import parse
def round(game: tuple[str, str]) -> int:
if game[0] == "A":
if game[1] == "X":
return 3
elif game[1] == "Y":
return 3 + 1
else:
return 6 + 2
elif game[0] == "B":
if game[1] == "X":
return 1
elif game[1] == "Y":
return 3 + 2
else:
return 6 + 3
else:
if game[1] == "X":
return 2
elif game[1] == "Y":
return 3 + 3
else:
return 6 + 1
def solve(strategy: list[tuple[str, str]]) -> int:
return sum(map(round, strategy))
def main():
print(solve(parse("input")))
if __name__ == "__main__":
main()

View File

@ -1,4 +1,5 @@
#!/bin/bash
day=$1
mkdir day${day}
cp template/* day${day}
curl -o day${day}/input --cookie ~/AdventOfCode/2022/cookies.txt https://adventofcode.com/2022/day/$day/input

5
template/common.py Normal file
View File

@ -0,0 +1,5 @@
from typing import Any
def parse(filename: str) -> Any | None:
pass

14
template/part1.py Normal file
View File

@ -0,0 +1,14 @@
from common import parse
def solve():
pass
def main():
r = solve()
print(r)
if __name__ == "__main__":
main()

14
template/part2.py Normal file
View File

@ -0,0 +1,14 @@
from common import parse
def solve():
pass
def main():
r = solve()
print(r)
if __name__ == "__main__":
main()