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/proto_science Dec 02 '22

Spent way too long trying to figure out a less hardcody way. But still a little proud :) Julia โ†’ 75 + 75 ยตs

function day02_1(parsed)
    op_move = ["A", "B", "C"]
    my_move = ["X", "Y", "Z"]
    scores = map(parsed) do g
        op_ind = findfirst(isequal(g[1]), op_move)
        my_ind = findfirst(isequal(g[2]), my_move)
        ((mod(my_ind - op_ind + 1, 3) * 3) % 9 + my_ind)
    end
    sum(scores)
end

function day02_2(parsed)
    op_move = ["A", "B", "C"]
    my_move = ["X", "Y", "Z"]
    scores = map(parsed) do g
        op_ind = findfirst(isequal(g[1]), op_move)
        offset_ind = findfirst(isequal(g[2]), my_move)
        (offset_ind - 1) * 3 +  mod1(op_ind + offset_ind - 2, 3)
    end
    sum(score)
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.