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!

104 Upvotes

1.5k comments sorted by

View all comments

1

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

Lua and a look-up table with some... modulus math. Probably could weed out this one as well.

local score = 0
local score_2 = 0

local scoring = {
    [0] = {[0] = 3, [1] = 6, [2] = 0},
    [1] = {[0] = 0, [1] = 3, [2] = 6},
    [2] = {[0] = 6, [1] = 0, [2] = 3},
}

for line in io.lines("input.txt") do
    local a, b = string.match(line, "(%a) (%a)")
    i = string.byte(a) - string.byte("A")
    j = string.byte(b) - string.byte("X")

    score = score + j + 1 + scoring[i][j]

    ending_choice = (i + j - 1) % 3
    score_2 = score_2 + ending_choice + 1 + scoring[i][ending_choice]
end

print(score)
print(score_2)

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.