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

Log from browser

const splitLineBreak = t => t.split(/\r?\n/);
const text = document.querySelector("pre").textContent;

const convertToName = c => {
  switch (c) {
    case 'A':
    case 'X':
      return "Rock"
    case 'B':
    case 'Y':
      return "Paper"
    default:
      return "Scissors"
  }
}

const shapeScore = you => {
  switch (you) {
    case "Rock":
      return 1
    case "Paper":
      return 2
    default:
      return 3
  }
}

const compareScore = (opponent, you) =>
  opponent === you ? 3 :
    opponent === "Rock" && you === "Paper"
      || opponent === "Paper" && you === "Scissors"
      || opponent === "Scissors" && you === "Rock" ? 6 : 0

const compare = ([opponent, you]) =>
  compareScore(opponent, you) + shapeScore(you)

const listOfScores = splitLineBreak(text).filter(x => x != "").map(x => x.split(" ").map(convertToName)).map(compare)

const sum = listOfScores.reduce((acc, curr) => acc + curr, 0)

console.log(sum)

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.