diff --git a/2022/02/main.py b/2022/02/main.py new file mode 100644 index 0000000..c78f0d8 --- /dev/null +++ b/2022/02/main.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + +# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string + + +def readinput(): + lines = [] + with open("input", "r", encoding="utf-8") as file: + for line in file.readlines(): + lines.append(line.split()) + return lines + + +def main(): + guide = readinput() + + shapes1 = { + "X": {"beats": "C", "tie": "A", "score": 1}, # ROCK + "Y": {"beats": "A", "tie": "B", "score": 2}, # PAPER + "Z": {"beats": "B", "tie": "C", "score": 3}, # SCISSORS + } + + shapes2 = { + "A": {"beats": "Z", "beat_by": "Y", "tie": "X"}, # ROCK + "B": {"beats": "X", "beat_by": "Z", "tie": "Y"}, # PAPER + "C": {"beats": "Y", "beat_by": "X", "tie": "Z"}, # SCISSORS + } + + # part 1 + totalscore = 0 + for you, me in guide: + if shapes1[me]["beats"] == you: # won + totalscore += shapes1[me]["score"] + 6 + elif shapes1[me]["tie"] == you: # tie + totalscore += shapes1[me]["score"] + 3 + else: # lost + totalscore += shapes1[me]["score"] + print("Totalscore (Part 1): %d" % totalscore) + + # part2 + totalscore = 0 + for you, me in guide: + if me == "X": # need to lose + choosen = shapes2[you]["beats"] + totalscore += shapes1[choosen]["score"] + elif me == "Y": # need tie + choosen = shapes2[you]["tie"] + totalscore += shapes1[choosen]["score"] + 3 + else: # need win + choosen = shapes2[you]["beat_by"] + totalscore += shapes1[choosen]["score"] + 6 + print("Totalscore (Part 2): %d" % totalscore) + + +if __name__ == "__main__": + main()