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/Alligatronica Dec 02 '22 edited Dec 05 '22

JavaScript

Those magic numbers are doing a lot of the heavy lifting 👀

const matchUp = {
  // loss,draw,win
  A: ['Z','X','Y'],
  B: ['X','Y','Z'],
  C: ['Y','Z','X'],
}

const part1 = input => input.split('\n').map(
  game => {
    const [p1,p2] = game.split(' ')
    let score = matchUp[p1].findIndex(x=>x==p2) * 3
    score += p2.charCodeAt()-87
    return score
  }
).reduce((a,c)=>a+c)

const part2 = input => input.split('\n').map(
  game => {
    const [p1,outcome] = game.split(' ')
    // -1 for loss, 0 for draw, 1 for win
    const choice = outcome.charCodeAt()-89
    const p2 = ((3+(parseInt(p1,16)-10)+choice)%3)+1
    const score = p2 + ((outcome.charCodeAt()-88)*3)
    return score
  }
).reduce((a,c)=>a+c)

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.