From 24fae371545eb2fcbfab2c6b648b3bb1762e9b9d Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Mon, 5 Dec 2022 10:51:59 -0500 Subject: [PATCH] day02 solutions --- day01/{day01.py => solve.py} | 2 + day02/solve.py | 79 +++++++++++++++++++++++++++++++ day02/solve.pyc | Bin 0 -> 2596 bytes day02/tests.py | 43 +++++++++++++++++ makefile | 14 ++++++ template.py => solve.py.template | 2 + tests.py.template | 12 +++++ 7 files changed, 152 insertions(+) rename day01/{day01.py => solve.py} (97%) create mode 100644 day02/solve.py create mode 100644 day02/solve.pyc create mode 100644 day02/tests.py create mode 100644 makefile rename template.py => solve.py.template (85%) create mode 100644 tests.py.template 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 0000000000000000000000000000000000000000..1cef8e934b7b6cdf005ac98ef02cfcef2c6534f4 GIT binary patch literal 2596 zcmd5;>uwuW5dL;Ic4EiQ{Yrp_OTd6qx0Xny2vsd^)I@+C&ZbIP30Y>(NwdNB8t+M! zO3APCi$A;o55Obv96SjU{D+y@buOhKBqD+B{dQ)~oY~pA9ecl)i$5=Y7lz0_9^LQJ zmHbAa>F1yW1Vp&_mIw#hL*PP>An>4b2u7gu2y)P)2=dTl2u7g`2*#j`2nx{S2#Sb2 zm@$|fHcSB~Pq!k>C^mv|=n{gGsFk712qvH>?&C^0Izqo?blqoj#2# zL8)_4ZV*`D$ib02Bs|y=Jg2xgc<#clxTMoa#t3!Wwr|=KTgmY@md6R{0{yl<6yhk3 zdUljmqd~9THI}!vS_e_bv|1ISaRJF4VylO7*Hj1LVY}Opk58&$tWCA8pPHUsj}O+F zU9a7$)vCJv>{hLs#GR++X8)PkePV2jpUmDQ>EW#S)&Mh&ZL6gC`OmGC%}{J=wk_rzP)Y@)6ivCLip<=0TAmQ_G9~6`SXK zf4FLMxHq{hC!0eb20wzraBkBUE@-NAzrwHCs1yk;gKE}4Y3($2AMM%kA=`YkH{^Hk z?{2AVy+rFNpD9d`z_0K4epC6G&g5lLrdA-?-p*FbuQ$~e`9o<5g;zZx-3D83H&<@+}ST?tf7LR6N6kzL@Bj z76!#=SQ!7j02F6b($49jY}K2eZ7I&GQf!i zOZGdFRZB!wSWR*)M$%%JA$`!7+1#OqlHiAjaTJ>5nu5={m$6K 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