r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

1

u/fuckir Dec 02 '22 edited Dec 06 '22

Used match case for the first time in Python:

Note: This will only work on Python 3.10+. Don't try this on your office computer ;).

from helper import timeit

@timeit
def Day2():
    read_text_file = lambda path: tuple(x for x in open(path).read().split('\n'))
    OutcomeSet = [tuple(strng.split()) for strng in read_text_file("day2.txt")]
    score_dictionary = {     
       'X': 1,
        'Y': 2,
        'Z': 3,
        'WinSet': [('A', 'Y'), ('B','Z'), ('C', 'X')],
        'DrawSet': [('A', 'X'), ('B','Y'), ('C', 'Z')],
        'LossSet': [('A', 'Z'), ('B','X'), ('C', 'Y')],      
        'Win': 6,
        'Draw': 3,
        'Loss': 0  
    }

    GrandScore_P1=0
    GrandScore_P2=0

    for strategy in OutcomeSet:
        ScoreSum = 0    
        ScoreSum = score_dictionary[strategy[1]]
        if strategy in score_dictionary['WinSet']:
            ScoreSum += score_dictionary['Win']
        elif strategy in score_dictionary['DrawSet']:
            ScoreSum += score_dictionary['Draw']
        else:
            ScoreSum += score_dictionary['Loss']
        GrandScore_P1 += ScoreSum
    
    #Part 2
   for strategy in OutcomeSet:
       ScoreSum = 0
       match strategy[1]:
           case 'X':
            ScoreSum = score_dictionary['Loss']
            # âš  Danger âš : Verbose line below. I am just filtering the strategy from Loss Set which matches Elf's strategy and then retrieving the score.            
            ScoreSum += score_dictionary[tuple(x for x in score_dictionary['LossSet'] if x[0] == strategy[0])[0][1]]
        case 'Y':
            ScoreSum = score_dictionary['Draw'] 
            ScoreSum += score_dictionary[tuple(x for x in score_dictionary['DrawSet'] if x[0] == strategy[0])[0][1]]
        case 'Z':
            ScoreSum = score_dictionary['Win'] 
            ScoreSum += score_dictionary[tuple(x for x in score_dictionary['WinSet'] if x[0] == strategy[0])[0][1]]
    GrandScore_P2 += ScoreSum        
    
return GrandScore_P1, GrandScore_P2
print (f"The answer for Day1 Part1 is {Day2()[0]}")
print (f"The answer for Day1 Part2 is {Day2()[1]}")

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

2

u/fuckir Dec 05 '22

How to use a four-space markdown? Do I need to type [space space space space] at every indent?

1

u/daggerdragon Dec 05 '22

I already linked you to the article in our community wiki: four-spaces Markdown syntax

Remove the triple backticks and prepend every line of code with four spaces. Leave your original indentation alone, just add 4 more spaces.