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!

105 Upvotes

1.5k comments sorted by

View all comments

1

u/matteosan1 Dec 02 '22 edited Dec 05 '22

Julia

function part1(games::Vector{Tuple{String, String}})
    results = Dict("A"=>("Z", "X", "Y"), "B"=>("X", "Y", "Z"), "C"=>("Y", "Z", "X"))
    points_per_game = [0, 3, 6]
    points_per_card = Dict("X"=>1, "Y"=>2, "Z"=>3)
    points = 0

    for g in games
        idx = findfirst(x->x==g[2], results[g[1]])
        points += points_per_card[g[2]] + points_per_game[idx]
    end

    println("🎄 Part 1: $(points)")
end

function part2(games::Vector{Tuple{String, String}}) 
    points = 0 
    results = Dict("A"=>("Z", "X", "Y"), "B"=>("X", "Y", "Z"), "C"=>("Y", "Z", "X")) 
    points_per_card = Dict("X"=>1, "Y"=>2, "Z"=>3)

    for g in games
        if g[2] == "X"
          points += points_per_card[results[g[1]][1]]
        elseif g[2] == "Y"
          points += points_per_card[results[g[1]][2]] + 3
        elseif g[2] == "Z"
          points += points_per_card[results[g[1]][3]] + 6
       end
    end

    println("🎄🎅 Part 2: $(points)")
end

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.