mirror of
https://github.com/RussellChamp/advent-of-code-2022.git
synced 2026-10-27 21:52:30 +00:00
day02 solutions
This commit is contained in:
1 parent
17831e00f1
commit
24fae37154
7 files changed
+152
No files matched your search
@@ -1,3 +1,5 @@
|
|||||||
|
# Day 01: Calorie Counting
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
def get_path():
|
def get_path():
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
# Day 02: Rock Paper Scissors
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
def get_path():
|
||||||
|
return os.path.join(os.path.dirname(__file__), 'data.txt')
|
||||||
|
|
||||||
|
ROCK_INPUT = 'A'
|
||||||
|
PAPER_INPUT = 'B'
|
||||||
|
SCISSORS_INPUT = 'C'
|
||||||
|
ROCK_OUTPUT = 'X'
|
||||||
|
PAPER_OUTPUT = 'Y'
|
||||||
|
SCISSORS_OUTPUT = 'Z'
|
||||||
|
ROCK_SCORE = 1
|
||||||
|
PAPER_SCORE = 2
|
||||||
|
SCISSORS_SCORE = 3
|
||||||
|
scoreDict = { ROCK_OUTPUT: ROCK_SCORE, PAPER_OUTPUT: PAPER_SCORE, SCISSORS_OUTPUT: SCISSORS_SCORE }
|
||||||
|
LOSS_SCORE = 0
|
||||||
|
TIE_SCORE = 3
|
||||||
|
WIN_SCORE = 6
|
||||||
|
|
||||||
|
# calculate a score based on their choice and my choice
|
||||||
|
def get_score(theirs, mine):
|
||||||
|
if [theirs, mine] == [ROCK_INPUT, ROCK_OUTPUT] \
|
||||||
|
or [theirs, mine] == [PAPER_INPUT, PAPER_OUTPUT] \
|
||||||
|
or [theirs, mine] == [SCISSORS_INPUT, SCISSORS_OUTPUT]: # Tie
|
||||||
|
return TIE_SCORE + scoreDict[mine]
|
||||||
|
elif [theirs, mine] == [ROCK_INPUT, PAPER_OUTPUT] \
|
||||||
|
or [theirs, mine] == [PAPER_INPUT, SCISSORS_OUTPUT] \
|
||||||
|
or [theirs, mine] == [SCISSORS_INPUT, ROCK_OUTPUT]: # Win
|
||||||
|
return WIN_SCORE + scoreDict[mine]
|
||||||
|
else: # Loss
|
||||||
|
return LOSS_SCORE + scoreDict[mine]
|
||||||
|
|
||||||
|
LOSS_OUTCOME = 'X'
|
||||||
|
TIE_OUTCOME = 'Y'
|
||||||
|
WIN_OUTCOME = 'Z'
|
||||||
|
outcomeDict = { LOSS_OUTCOME: LOSS_SCORE, TIE_OUTCOME: TIE_SCORE, WIN_OUTCOME: WIN_SCORE }
|
||||||
|
# calculate a score based on their choice and the desired outcome
|
||||||
|
def get_score_beta(theirs, outcome):
|
||||||
|
if [theirs, outcome] == [ROCK_INPUT, TIE_OUTCOME] \
|
||||||
|
or [theirs, outcome] == [PAPER_INPUT, LOSS_OUTCOME] \
|
||||||
|
or [theirs, outcome] == [SCISSORS_INPUT, WIN_OUTCOME]: # Rock play
|
||||||
|
return ROCK_SCORE + outcomeDict[outcome]
|
||||||
|
elif [theirs, outcome] == [PAPER_INPUT, TIE_OUTCOME] \
|
||||||
|
or [theirs, outcome] == [SCISSORS_INPUT, LOSS_OUTCOME] \
|
||||||
|
or [theirs, outcome] == [ROCK_INPUT, WIN_OUTCOME]: # Paper play
|
||||||
|
return PAPER_SCORE + outcomeDict[outcome]
|
||||||
|
else: # Scissors play
|
||||||
|
return SCISSORS_SCORE + outcomeDict[outcome]
|
||||||
|
|
||||||
|
# Calculate the total score from the playbook
|
||||||
|
def solve_part1():
|
||||||
|
total_score = 0
|
||||||
|
|
||||||
|
with open(get_path()) as fp:
|
||||||
|
for line in fp:
|
||||||
|
choices = line.strip().split(' ')
|
||||||
|
total_score += get_score(choices[0], choices[1])
|
||||||
|
|
||||||
|
print('total score: %s' % (total_score))
|
||||||
|
|
||||||
|
# Calculate the alternate total score from the playbook
|
||||||
|
def solve_part2():
|
||||||
|
total_score = 0
|
||||||
|
|
||||||
|
with open(get_path()) as fp:
|
||||||
|
for line in fp:
|
||||||
|
choices = line.strip().split(' ')
|
||||||
|
total_score += get_score_beta(choices[0], choices[1])
|
||||||
|
|
||||||
|
print('total score: %s' % (total_score))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Part 1:')
|
||||||
|
solve_part1()
|
||||||
|
|
||||||
|
print('Part 2:')
|
||||||
|
solve_part2()
|
||||||
Binary file not shown.
@@ -0,0 +1,43 @@
|
|||||||
|
import unittest
|
||||||
|
import solve
|
||||||
|
|
||||||
|
class Day02TestMethods(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_get_score(self):
|
||||||
|
get_score_input = [
|
||||||
|
[ solve.ROCK_INPUT, solve.ROCK_OUTPUT, solve.TIE_SCORE + solve.ROCK_SCORE ],
|
||||||
|
[ solve.ROCK_INPUT, solve.PAPER_OUTPUT, solve.WIN_SCORE + solve.PAPER_SCORE ],
|
||||||
|
[ solve.ROCK_INPUT, solve.SCISSORS_OUTPUT, solve.LOSS_SCORE + solve.SCISSORS_SCORE ],
|
||||||
|
[ solve.PAPER_INPUT, solve.ROCK_OUTPUT, solve.LOSS_SCORE + solve.ROCK_SCORE ],
|
||||||
|
[ solve.PAPER_INPUT, solve.PAPER_OUTPUT, solve.TIE_SCORE + solve.PAPER_SCORE],
|
||||||
|
[ solve.PAPER_INPUT, solve.SCISSORS_OUTPUT, solve.WIN_SCORE + solve.SCISSORS_SCORE ],
|
||||||
|
[ solve.SCISSORS_INPUT, solve.ROCK_OUTPUT, solve.WIN_SCORE + solve.ROCK_SCORE],
|
||||||
|
[ solve.SCISSORS_INPUT, solve.PAPER_OUTPUT, solve.LOSS_SCORE + solve.PAPER_SCORE ],
|
||||||
|
[ solve.SCISSORS_INPUT, solve.SCISSORS_OUTPUT, solve.TIE_SCORE + solve.SCISSORS_SCORE],
|
||||||
|
];
|
||||||
|
|
||||||
|
for case in get_score_input:
|
||||||
|
[theirs, mine, expectedScore] = case
|
||||||
|
self.assertEqual(solve.get_score(theirs, mine), expectedScore, '%s vs %s should get %d points' % (theirs, mine, expectedScore))
|
||||||
|
|
||||||
|
def test_get_score_beta(self):
|
||||||
|
get_score_input = [
|
||||||
|
[ solve.ROCK_INPUT, solve.TIE_OUTCOME, solve.TIE_SCORE + solve.ROCK_SCORE ],
|
||||||
|
[ solve.ROCK_INPUT, solve.LOSS_OUTCOME, solve.LOSS_SCORE + solve.SCISSORS_SCORE ],
|
||||||
|
[ solve.ROCK_INPUT, solve.WIN_OUTCOME, solve.WIN_SCORE + solve.PAPER_SCORE ],
|
||||||
|
[ solve.PAPER_INPUT, solve.TIE_OUTCOME, solve.TIE_SCORE + solve.PAPER_SCORE],
|
||||||
|
[ solve.PAPER_INPUT, solve.LOSS_OUTCOME, solve.LOSS_SCORE + solve.ROCK_SCORE ],
|
||||||
|
[ solve.PAPER_INPUT, solve.WIN_OUTCOME, solve.WIN_SCORE + solve.SCISSORS_SCORE ],
|
||||||
|
[ solve.SCISSORS_INPUT, solve.TIE_OUTCOME, solve.TIE_SCORE + solve.SCISSORS_SCORE ],
|
||||||
|
[ solve.SCISSORS_INPUT, solve.LOSS_OUTCOME, solve.LOSS_SCORE + solve.PAPER_SCORE],
|
||||||
|
[ solve.SCISSORS_INPUT, solve.WIN_OUTCOME, solve.WIN_SCORE + solve.ROCK_SCORE],
|
||||||
|
];
|
||||||
|
|
||||||
|
for case in get_score_input:
|
||||||
|
[theirs, outcome, expectedScore] = case
|
||||||
|
self.assertEqual(solve.get_score_beta(theirs, outcome), expectedScore, '%s and %s should get %d points' % (theirs, outcome, expectedScore))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
default:
|
||||||
|
# This is my 2022 Advent of Code project
|
||||||
|
|
||||||
|
init:
|
||||||
|
mkdir day$(day)
|
||||||
|
sed 's/#DAY#/$(day)/g' solve.py.template | sed 's/#TITLE#/$(title)/g' > day$(day)/solve.py
|
||||||
|
sed 's/#DAY#/$(day)/g' tests.py.template | sed 's/#TITLE#/$(title)/g' > day$(day)/tests.py
|
||||||
|
touch day$(day)/data.txt
|
||||||
|
|
||||||
|
run:
|
||||||
|
python day$(day)/solve.py
|
||||||
|
|
||||||
|
test:
|
||||||
|
python day$(day)/tests.py
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
# Day #DAY#: #TITLE#
|
||||||
|
|
||||||
def solve_part1():
|
def solve_part1():
|
||||||
print('hello')
|
print('hello')
|
||||||
def solve_part2():
|
def solve_part2():
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
class Day#DAY#TestMethods(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Does a thing
|
||||||
|
def test_true(self):
|
||||||
|
self.assertEqual(True, False)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in new issue
Block a user