Skip to content

Commit 01f51c1

Browse files
committed
day02
1 parent 59c7f37 commit 01f51c1

File tree

2 files changed

+2539
-0
lines changed

2 files changed

+2539
-0
lines changed

2022/02/__init__.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def cast_input(inputs: str):
2+
# A for Rock, B for Paper, and C for Scissors - your opponent
3+
# X for Rock, Y for Paper, and Z for Scissors - you
4+
return [list(map(ord, x.split())) for x in inputs.splitlines()]
5+
6+
7+
N = 3
8+
A, X = ord('A'), ord('X')
9+
10+
SHIFT = N - X % N
11+
12+
13+
def _score_shape(x):
14+
# 1 for Rock, 2 for Paper, and 3 for Scissors
15+
return x - X + 1
16+
17+
18+
def _score_outcome(a, x): # outcome: 0, 1, 2 -> score: 0, 3, 6
19+
return (((x - a) + SHIFT) % N) * N
20+
21+
22+
def part1(inputs):
23+
def score(a, x):
24+
return _score_shape(x) + _score_outcome(a, x)
25+
26+
return sum(score(*x) for x in inputs)
27+
28+
29+
def _choose_shape(a, e):
30+
# X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win
31+
return X + (a + A + e % N) % N
32+
33+
34+
def part2(inputs):
35+
def score(a, x):
36+
s = _choose_shape(a, x)
37+
return _score_shape(s) + _score_outcome(a, s)
38+
39+
return sum(score(*x) for x in inputs)

0 commit comments

Comments
 (0)