diff --git a/day2/common.py b/day2/common.py new file mode 100644 index 0000000..fc718c5 --- /dev/null +++ b/day2/common.py @@ -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()] diff --git a/day2/part1.py b/day2/part1.py new file mode 100644 index 0000000..3410ff8 --- /dev/null +++ b/day2/part1.py @@ -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() diff --git a/day2/part2.py b/day2/part2.py new file mode 100644 index 0000000..bd911f1 --- /dev/null +++ b/day2/part2.py @@ -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() diff --git a/get_input.sh b/get_input.sh index 853f2d1..29e0336 100755 --- a/get_input.sh +++ b/get_input.sh @@ -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 diff --git a/template/common.py b/template/common.py new file mode 100644 index 0000000..8814171 --- /dev/null +++ b/template/common.py @@ -0,0 +1,5 @@ +from typing import Any + + +def parse(filename: str) -> Any | None: + pass diff --git a/template/part1.py b/template/part1.py new file mode 100644 index 0000000..8d3baac --- /dev/null +++ b/template/part1.py @@ -0,0 +1,14 @@ +from common import parse + + +def solve(): + pass + + +def main(): + r = solve() + print(r) + + +if __name__ == "__main__": + main() diff --git a/template/part2.py b/template/part2.py new file mode 100644 index 0000000..8d3baac --- /dev/null +++ b/template/part2.py @@ -0,0 +1,14 @@ +from common import parse + + +def solve(): + pass + + +def main(): + r = solve() + print(r) + + +if __name__ == "__main__": + main()