aboutsummaryrefslogtreecommitdiff
path: root/2022/02
diff options
context:
space:
mode:
authorMarvin Borner2022-12-02 07:45:14 +0100
committerMarvin Borner2022-12-02 07:51:12 +0100
commita1819f735e5939bad4f92ca90fd8163f4723eb31 (patch)
tree3793342622851f50b5cb93cbc81600958f1b8c9a /2022/02
parent0c2933d2432b73cca27fc01f0fa67f0bd96d66ab (diff)
Improvements
Diffstat (limited to '2022/02')
-rw-r--r--2022/02/solve.py34
1 files changed, 7 insertions, 27 deletions
diff --git a/2022/02/solve.py b/2022/02/solve.py
index 00d4a5f..ded08c8 100644
--- a/2022/02/solve.py
+++ b/2022/02/solve.py
@@ -1,40 +1,20 @@
-data = [dat.split(" ") for dat in open("input").read().split("\n") if dat != '']
+data = [[ord(x) - (ord("X") if i == 1 else ord("A")) for i,x in enumerate(dat.split(" "))] for dat in open("input").read().split("\n") if dat != '']
-# X Y Z = Rock Paper Scissors
-# A B C = Rock Paper Scissors
-
-def part1():
+def solve(choice, mat, factors):
res = 0
- choice=[1,2,3]
- mat = [3,6,0]
- factors = [1, 0, 2]
- mat = [0,3,6]
for line in data:
- res += choice[ord(line[1]) - ord("X")]
- factor = factors[ord(line[0]) - ord("A")]
+ factor = factors[line[0]]
rot = mat[factor:] + mat[:factor]
- res += rot[ord(line[1]) - ord("X")]
+ res += choice[line[1]] + rot[line[1]]
return res
-# X Y Z = lose draw win
-# 1 2 3 = Rock Paper Scissors
-# A B C = Rock Paper Scissors
+def part1():
+ return solve([1,2,3], [0,3,6], [1,0,2])
def part2():
- res = 0
-
- choice = [0,3,6]
- mat = [3,1,2]
-
- for line in data:
- res += choice[ord(line[1]) - ord("X")]
- factor = ord(line[0]) - ord("A")
- rot = mat[factor:] + mat[:factor]
- res += rot[ord(line[1]) - ord("X")]
-
- return res
+ return solve([0,3,6], [3,1,2], [0,1,2])
print(f"Part 1: {part1()}")
print(f"Part 2: {part2()}")