diff --git a/day01/day01.py b/day01/solve.py similarity index 97% rename from day01/day01.py rename to day01/solve.py index 863fa7e..27c9e47 100644 --- a/day01/day01.py +++ b/day01/solve.py @@ -1,3 +1,5 @@ +# Day 01: Calorie Counting + import os def get_path(): diff --git a/day02/solve.py b/day02/solve.py new file mode 100644 index 0000000..056fbfa --- /dev/null +++ b/day02/solve.py @@ -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() \ No newline at end of file diff --git a/day02/solve.pyc b/day02/solve.pyc new file mode 100644 index 0000000..1cef8e9 Binary files /dev/null and b/day02/solve.pyc differ diff --git a/day02/tests.py b/day02/tests.py new file mode 100644 index 0000000..0d93b3e --- /dev/null +++ b/day02/tests.py @@ -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() \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..df271b8 --- /dev/null +++ b/makefile @@ -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 \ No newline at end of file diff --git a/template.py b/solve.py.template similarity index 85% rename from template.py rename to solve.py.template index 02b6bc9..6e86d4a 100644 --- a/template.py +++ b/solve.py.template @@ -1,3 +1,5 @@ +# Day #DAY#: #TITLE# + def solve_part1(): print('hello') def solve_part2(): diff --git a/tests.py.template b/tests.py.template new file mode 100644 index 0000000..54adc7c --- /dev/null +++ b/tests.py.template @@ -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() \ No newline at end of file