{-# Language QuasiQuotes, TemplateHaskell #-}
module Main where
import Advent (format, stageTH)
data A = AA | AB | AC deriving Int -> A -> ShowS
[A] -> ShowS
A -> String
(Int -> A -> ShowS) -> (A -> String) -> ([A] -> ShowS) -> Show A
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> A -> ShowS
showsPrec :: Int -> A -> ShowS
$cshow :: A -> String
show :: A -> String
$cshowList :: [A] -> ShowS
showList :: [A] -> ShowS
Show
data B = BX | BY | BZ deriving Int -> B -> ShowS
[B] -> ShowS
B -> String
(Int -> B -> ShowS) -> (B -> String) -> ([B] -> ShowS) -> Show B
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> B -> ShowS
showsPrec :: Int -> B -> ShowS
$cshow :: B -> String
show :: B -> String
$cshowList :: [B] -> ShowS
showList :: [B] -> ShowS
Show
stageTH
main :: IO ()
IO ()
main =
do input <- [format|2022 2 (@A @B%n)*|]
print $ sum [outcome a b + shapeScore b | (a,b) <- input]
print $ sum [outcome a b + shapeScore b | (a,b') <- input, let b = A -> B -> B
pick A
a B
b' ]
shapeScore :: B -> Int
shapeScore :: B -> Int
shapeScore B
BX = Int
1
shapeScore B
BY = Int
2
shapeScore B
BZ = Int
3
outcome :: A -> B -> Int
outcome :: A -> B -> Int
outcome A
AA B
BX = Int
3
outcome A
AA B
BY = Int
6
outcome A
AA B
BZ = Int
0
outcome A
AB B
BX = Int
0
outcome A
AB B
BY = Int
3
outcome A
AB B
BZ = Int
6
outcome A
AC B
BX = Int
6
outcome A
AC B
BY = Int
0
outcome A
AC B
BZ = Int
3
desiredOutcome :: B -> Int
desiredOutcome :: B -> Int
desiredOutcome B
BX = Int
0
desiredOutcome B
BY = Int
3
desiredOutcome B
BZ = Int
6
pick :: A -> B -> B
pick :: A -> B -> B
pick A
a B
b = [B] -> B
forall a. HasCallStack => [a] -> a
head [ B
x | B
x <- [B
BX,B
BY,B
BZ], A -> B -> Int
outcome A
a B
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== B -> Int
desiredOutcome B
b ]