Skip to content

Commit f30a2a2

Browse files
committed
Added solution for Day2 Part2
1 parent 874c9ee commit f30a2a2

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

Day2/Part2.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from pathlib import Path
2+
3+
from Day1.Part1 import _parse_elf_data
4+
5+
def solve(file_path:Path) -> int:
6+
''' Gives you the solution for the appropriate advent of code question.
7+
Arguments:
8+
file_path(pathlib.Path): The filepath in which to read data from.
9+
10+
Returns:
11+
Answer for the appropriate advent of code questions.
12+
'''
13+
round_data = []
14+
with open(file_path, 'r', encoding='UTF-8') as input_file:
15+
round_data = input_file.readlines()
16+
17+
# Iterating through our rounds and finding the results
18+
score_data = []
19+
for round in round_data:
20+
score_data.append(score_RPS(round))
21+
22+
return sum(score_data)
23+
24+
def score_RPS(round_data) -> int:
25+
''' Takes in a single game of rock paper scissors and returns the score.
26+
27+
This is for the second part of day2_2022, the second column is for what the result of the round should be.
28+
29+
Naming:
30+
A: rock
31+
B: paper
32+
C: scissors
33+
34+
X: lose
35+
Y: draw
36+
Z: win
37+
38+
Scoring:
39+
By Choice:
40+
Rock: 1
41+
Paper: 2
42+
Scissors: 3
43+
By outcome:
44+
Win: 6
45+
Draw: 3
46+
Loss: 0
47+
48+
Arguments:
49+
String representing two choices, your opponents and yours, split by a space.
50+
51+
Returns:
52+
Integer representing number of point you would get for that round.
53+
'''
54+
''' See Day1 for ord and chr explanations.
55+
56+
We'll use the second column to make a key for calculating our choice score.
57+
key = x:0 y:1 z:2, so second column - 87
58+
59+
Lets draw a table for points by choice, then think about our scores being 0 indexed, then mod 3 by adding 3 to everything
60+
A | B | C A | B | C A | B | C
61+
X 3 | 1 | 2 x 2 | 0 | 1 x 2 | 3 | 4
62+
Y 1 | 2 | 3 y 0 | 1 | 2 y 3 | 4 | 5
63+
Z 2 | 3 | 1 z 1 | 2 | 0 z 4 | 5 | 6
64+
65+
So if we ordinate our first column (A-63=2), add the key from our second column, then mod 3, we have a 0 indexed score. Add 1 and we get our score.
66+
67+
Then using the key, can just multiply by 3 to get our win score.
68+
x:loss:0
69+
y:draw:3
70+
z:win:6
71+
'''
72+
data = [ord(value) for value in round_data.strip().split(' ')]
73+
74+
# Creating our key with the second column and ord our first column
75+
data[1] = data[1] - 88
76+
data[0] = data[0] - 64
77+
78+
# Capturing our score
79+
choice_score = ((data[0] + 1 + data[1]) % 3) + 1
80+
81+
# Now finding our status score
82+
status_score = (data[1] * 3)
83+
84+
return status_score + choice_score

run.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44

55

6-
from Day2 import Part1
6+
from Day2 import Part2
77

8-
print(Part1.solve(Path() / 'Day2' / 'input.txt'))
8+
print(Part2.solve(Path() / 'Day2' / 'input.txt'))

0 commit comments

Comments
 (0)