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

2

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

Typescript

Straight forward, implemented game logic even though not necessary...

Source Code Run on Website

enum Shape {
  Rock = 1,
  Paper = 2,
  Scissors = 3,
}

enum MatchResult {
  Win = 6,
  Draw = 3,
  Loss = 0,
}

const matchEvalMap: Record<Shape, Record<MatchResult, Shape>> = {
  [Shape.Rock]: {
    [MatchResult.Win]: Shape.Scissors,
    [MatchResult.Loss]: Shape.Paper,
    [MatchResult.Draw]: Shape.Rock,
  },
  [Shape.Paper]: {
    [MatchResult.Win]: Shape.Rock,
    [MatchResult.Loss]: Shape.Scissors,
    [MatchResult.Draw]: Shape.Paper,
  },
  [Shape.Scissors]: {
    [MatchResult.Win]: Shape.Paper,
    [MatchResult.Loss]: Shape.Rock,
    [MatchResult.Draw]: Shape.Scissors,
  },
};
...

2

u/strivingforsomething Dec 02 '22

Nice work!

I'm currently learning typescript and I'm curious to see how far you one can go with typing and making the code as clean/simple as possible!

I'll definitely follow your solutions for the upcoming puzzles!

1

u/ChasmoGER Dec 02 '22

Thanks! Feel free to watch my Github Repo if you want to be notified ;-)

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.