This commit is contained in:
Gattix 2022-12-02 17:48:13 +01:00
parent 8e4decd15f
commit 317f01b171
2 changed files with 2529 additions and 0 deletions

29
2022/day02/day02.hs Normal file
View File

@ -0,0 +1,29 @@
data Hand = Rock | Paper | Scissor deriving (Eq,Enum,Bounded,Show)
instance Ord Hand where
compare x y = case fromEnum x - fromEnum y `mod` 3 of
0 -> EQ
1 -> GT
(-2) -> GT
_ -> LT
(+.) :: Hand -> Int -> Hand
(+.) a b = toEnum $ flip mod 3 $ (+) (fromEnum a) b
main :: IO ()
main = do
input <- map (concat . words) . lines <$> readFile "input"
print $ day2 id input
print $ day2 realStrat input
toHands :: [Char] -> [Hand]
toHands x = map toEnum $ (zipWith subtract [65,88]) (map fromEnum x)
realStrat :: [Hand] -> [Hand]
realStrat (a:b:[]) = [a,a +. fromEnum (b +. 2)]
day2 :: ([Hand] -> [Hand]) -> [[Char]] -> Int
day2 f = sum . map (play . f . toHands)
play :: [Hand] -> Int
play (x:y:[]) = fromEnum y + 1 + if x == y then 3 else fromEnum (x<y) * 6

2500
2022/day02/input Normal file

File diff suppressed because it is too large Load Diff