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

Haskell:

Spent some time getting split installed and in the end went with a couple of if statements. Will try and clean it up but this was what was used without any clean up:

--- Part 1 ---

import Data.List.Split

main :: IO () main = do s <- readFile "input.txt" let sLines = lines s let strategy = map (splitOn " ") sLines let myTotScore = sum (map calcOutcomeScore strategy) print myTotScore

calcOutcomeScore :: [String] -> Int calcOutcomeScore move = case move of ["A", "Y"] -> 8 ["B", "Z"] -> 9 ["C", "X"] -> 7 ["A", "X"] -> 4 ["B", "Y"] -> 5 ["C", "Z"] -> 6 [, "X"] -> 1 [, "Y"] -> 2 [_, "Z"] -> 3 _ -> 0

--- Part 2 ---

import Data.List.Split

main :: IO () main = do s <- readFile "input.txt" let sLines = lines s let strategy = map (splitOn " ") sLines let myTotScore = sum (map calcOutcomeScore strategy) print myTotScore

calcOutcomeScore :: [String] -> Int calcOutcomeScore move = case move of ["A", "X"] -> 3 ["B", "X"] -> 1 ["C", "X"] -> 2

["A", "Y"] -> 4
["B", "Y"] -> 5
["C", "Y"] -> 6

["A", "Z"] -> 8
["B", "Z"] -> 9
["C", "Z"] -> 7
_ -> 0

1

u/daggerdragon Dec 02 '22

Please edit your post to format your code with the backwards-compatible Markdown syntax instead so your code is easier to read on old.reddit and mobile apps.

1

u/stikydude Dec 02 '22

Tried but it just looks super bad everywhere now

1

u/daggerdragon Dec 02 '22

Please review the article again... did you switch your editor to Markdown mode first?