{-# Language QuasiQuotes, TemplateHaskell #-}
{-|
Module      : Main
Description : Day 2 solution
Copyright   : (c) Eric Mertens, 2022
License     : ISC
Maintainer  : emertens@gmail.com

<https://adventofcode.com/2022/day/2>

>>> :main + "A Y\nB X\nC Z\n"
15
12

-}
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 [(A, B)]
input <- [format|2022 2 (@A @B%n)*|]
    Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> IO ()) -> Int -> IO ()
forall a b. (a -> b) -> a -> b
$ [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [A -> B -> Int
outcome A
a B
b Int -> Int -> Int
forall a. Num a => a -> a -> a
+ B -> Int
shapeScore B
b | (A
a,B
b) <- [(A, B)]
input]
    Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> IO ()) -> Int -> IO ()
forall a b. (a -> b) -> a -> b
$ [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [A -> B -> Int
outcome A
a B
b Int -> Int -> Int
forall a. Num a => a -> a -> a
+ B -> Int
shapeScore B
b | (A
a,B
b') <- [(A, B)]
input, let b :: B
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 ]