diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-02 20:23:40 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-02 20:23:40 +0100 |
commit | 826ed886b5caa62e5f46a189bde7e5434cb49356 (patch) | |
tree | 2a53597251de67faf665eea9f5117d1fc1182593 /2015/07/puzzles.py | |
parent | feca43e796fbe8bde441f017f4c94642392bc40f (diff) |
Add day 7 solutions
Diffstat (limited to '2015/07/puzzles.py')
-rw-r--r-- | 2015/07/puzzles.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/2015/07/puzzles.py b/2015/07/puzzles.py new file mode 100644 index 0000000..37347c7 --- /dev/null +++ b/2015/07/puzzles.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# START PART 2 +from copy import deepcopy +# END PART 2 + +from numpy import uint16 + + +recipes: dict[str, list[str] | uint16] = {} + + +def solve(wire: str) -> uint16: + if wire.isdigit(): + return int(wire) + if type(recipes[wire]) == uint16: + return recipes[wire] + + match recipes[wire]: + case [x]: + recipes[wire] = solve(x) + case ["NOT", x]: + recipes[wire] = ~solve(x) + case [x, "AND", y]: + recipes[wire] = solve(x) & solve(y) + case [x, "OR", y]: + recipes[wire] = solve(x) | solve(y) + case [x, "LSHIFT", y]: + recipes[wire] = solve(x) << solve(y) + case [x, "RSHIFT", y]: + recipes[wire] = solve(x) >> solve(y) + + return recipes[wire] + + +def main() -> None: + global recipes + with open("input", "r", encoding="utf-8") as f: + for line in f.readlines(): + line = line.strip().split(" ") + recipes[line[-1]] = line[:-2] + + # START PART 2 + backup = deepcopy(recipes) + backup["b"] = solve("a") + recipes = backup + # END PART 2 + print(solve("a")) + + +if __name__ == "__main__": + main() |