Skip to content

Commit 874c9ee

Browse files
committed
Added solution for Day2 Part1
1 parent 557b9be commit 874c9ee

File tree

4 files changed

+2620
-2
lines changed

4 files changed

+2620
-2
lines changed

Day2/Part1.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from pathlib import Path
2+
from typing import List
3+
4+
def solve(file_path:Path) -> int:
5+
''' Gives you the sum of all your points for the given RPS file.
6+
Arguments:
7+
file_path(pathlib.Path): The filepath in which to read elf data from.
8+
9+
Returns:
10+
Integer representing number of points if following the guide that is input_file
11+
'''
12+
return sum(get_RPS_scores(file_path))
13+
14+
def get_RPS_scores(file_path:Path) -> List[int]:
15+
''' Reads rock-paper-scissors plays from a file and returns the total "points"
16+
17+
Naming:
18+
A, X: rock
19+
B, Y: paper
20+
C, Z: scissors
21+
22+
Scoring:
23+
By Choice:
24+
Rock: 1
25+
Paper: 2
26+
Scissors: 3
27+
By outcome:
28+
Win: 6
29+
Draw: 3
30+
Loss: 0
31+
32+
Arguments:
33+
file_path(pathlib.Path): The filepath in which to read elf data from.
34+
35+
Returns:
36+
List of integer representing number of points if following the guide that is input_file
37+
'''
38+
39+
# Reading in our file and splitting on newlines
40+
round_data = []
41+
with open(file_path, 'r', encoding='UTF-8') as input_file:
42+
round_data = input_file.readlines()
43+
44+
# Lets iterate through each of our rounds finding the results
45+
score_data = []
46+
for round in round_data:
47+
score_data.append(score_RPS(round))
48+
49+
return score_data
50+
51+
def score_RPS(round_data) -> int:
52+
''' Takes in a single game of rock paper scissors and returns the score.
53+
54+
Naming:
55+
A, X: rock
56+
B, Y: paper
57+
C, Z: scissors
58+
59+
Scoring:
60+
By Choice:
61+
Rock: 1
62+
Paper: 2
63+
Scissors: 3
64+
By outcome:
65+
Win: 6
66+
Draw: 3
67+
Loss: 0
68+
69+
Arguments:
70+
String representing two choices, your opponents and yours, split by a space.
71+
72+
Returns:
73+
Integer representing number of point you would get for that round.
74+
'''
75+
''' Making both answers the same value + 1
76+
77+
Alright, listen up
78+
79+
ord is a python function that turns alphabetical characters into ordinals.
80+
chr reverses that
81+
82+
i.e. ord('X') is 88, ord('A') is 65
83+
Here is a table for continence https://www.johndcook.com/ascii.png
84+
85+
Now lets talk about scoring. If we ordinate our answer and minus 87, we just get our score. i.e. ord('X') - 87 == 1.
86+
87+
Now lets talk about solving. To start (stay with me), we'll ordinate our vote, remove 23, then characterize. This results in our votes being in the same field as our opponents. i.e. rock = A, paper = B...
88+
Dual is simple, if we match, we draw
89+
To check for winning, we can simply check if ours is one higher or two lower
90+
a - b (+1)
91+
b - c (+1)
92+
or
93+
c - a (-2)
94+
else this and we have our no win.
95+
'''
96+
score = 0
97+
# Splitting and iterating our values
98+
data = [ord(value) for value in round_data.strip().split(' ')]
99+
100+
# Alright, lets move our choice to the same as our opponents
101+
data[1] = data[1] - 23
102+
103+
# Counting our score
104+
score += data[1] - 64
105+
106+
# Checking for draw
107+
if data[0] == data[1]:
108+
return score + 3
109+
110+
# Checking for win
111+
if data[1] in [data[0] + 1, data[0] - 2]:
112+
return score + 6
113+
114+
# If we got here, we've lost, lets just return our score
115+
return score

Day2/example.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A Y
2+
B X
3+
C Z

0 commit comments

Comments
 (0)