-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay22.hs
184 lines (148 loc) · 4.92 KB
/
Day22.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{-# LANGUAGE TupleSections #-}
module Day22
( part1
, part2
) where
import Control.Monad (void)
import Data.Bifunctor (bimap)
import Data.ByteString (ByteString)
import Data.Hashable (Hashable, hashWithSalt)
import Data.HashMap.Strict (HashMap, member, (!))
import qualified Data.HashMap.Strict as M (insert, singleton)
import Data.HashPSQ (HashPSQ, minView)
import qualified Data.HashPSQ as Q (insert, singleton)
import Data.Maybe (mapMaybe)
import FlatParse.Basic (anyAsciiDecimalInt, isDigit,
runParser, skipSatisfy, some)
import Helpers.Parsers.FlatParse (Parser, extract)
data Boss = Boss
{ hpb :: HP
, damage :: Damage
} deriving (Show, Eq, Ord)
data Wizard = Wizard
{ hpw :: HP
, mana :: Mana
, shield :: Shield
, poison :: Poison
, recharge :: Recharge
, armor :: Armor
} deriving (Show, Eq, Ord)
instance Hashable Boss where
hashWithSalt salt (Boss h d) = hashWithSalt salt (h, d)
instance Hashable Wizard where
hashWithSalt salt (Wizard h m s p r a) = hashWithSalt salt (h, m, s, p, r, a)
class Hurtable a where
hp :: a -> HP
instance Hurtable Boss where
hp = hpb
instance Hurtable Wizard where
hp = hpw
type HP = Int
type Damage = Int
type Mana = Int
type Shield = Int
type Poison = Int
type Recharge = Int
type Armor = Int
type Pair = (Wizard, Boss)
type WinLose = (Bool, Bool)
type Turn = (Pair -> (Pair -> Pair) -> (Pair, (Bool, Bool)))
me :: Wizard
me = Wizard 50 500 0 0 0 0
actions :: [(Int, Pair -> Pair)]
actions =
[ (53, castMagicMissile)
, (73, castDrain)
, (113, castShield)
, (173, castPoison)
, (229, castRecharge)
]
update :: Pair -> Pair
update (wizard, boss) =
( wizard
{ mana = mana'
, shield = max (shield wizard - 1) 0
, poison = max (poison wizard - 1) 0
, recharge = max (recharge wizard - 1) 0
, armor = armor'
}
, boss {hpb = hp'})
where
hp'
| poison wizard /= 0 = hp boss - 3
| otherwise = hp boss
armor'
| shield wizard /= 0 = 7
| otherwise = 0
mana'
| recharge wizard /= 0 = mana wizard + 101
| otherwise = mana wizard
castMagicMissile :: Pair -> Pair
castMagicMissile (wizard, boss) =
(wizard {mana = mana wizard - 53}, boss {hpb = hp boss - 4})
castDrain :: Pair -> Pair
castDrain (wizard, boss) =
( wizard {mana = mana wizard - 73, hpw = hp wizard + 2}
, boss {hpb = hp boss - 2})
castShield :: Pair -> Pair
castShield (wizard, boss) =
(wizard {mana = mana wizard - 113, shield = 6}, boss) -- should the timer be 5 ?
castPoison :: Pair -> Pair
castPoison (wizard, boss) =
(wizard {mana = mana wizard - 173, poison = 6}, boss)
castRecharge :: Pair -> Pair
castRecharge (wizard, boss) =
(wizard {mana = mana wizard - 229, recharge = 5}, boss)
bossAttack :: Pair -> Pair
bossAttack (wizard, boss) = (wizard {hpw = hp wizard - hurt}, boss)
where
hurt = max (damage boss - armor wizard) 1
doTurn :: Turn
doTurn (wizard, boss) action = ((wizard'', boss''), (win, lose))
where
(wizard', boss') = action . update $ (wizard, boss)
(wizard'', boss'') = bossAttack . update $ (wizard', boss')
win = hp boss' <= 0 || hp boss'' <= 0
lose = hp wizard'' <= 0
hurtTurn :: Turn
hurtTurn (wizard, boss) action
| hp wizard' <= 0 = ((wizard', boss), (False, True))
| otherwise = doTurn (wizard', boss) action
where
wizard' = wizard {hpw = hp wizard - 1}
winGame :: Turn -> HashPSQ (Pair, WinLose) Int () -> HashMap Pair Int -> Int
winGame turn queue dists
| win = usedMana
| lost || null availableActions = winGame turn rest dists
| otherwise = winGame turn queue' dists'
where
Just ((pair, (win, lost)), usedMana, _, rest) = minView queue
curMana = mana . fst $ pair
availableActions = filter ((< curMana) . fst) actions
nextTurns =
mapMaybe (consider . bimap (usedMana +) (turn pair)) availableActions
queue' =
foldr
(\(doublePair, newMana) -> Q.insert doublePair newMana ())
rest
nextTurns
dists' =
foldr (\((pair, _), newMana) -> M.insert pair newMana) dists nextTurns
consider (newMana, p@(pair, winlose))
| not (member pair dists) || newMana < dists ! pair = Just (p, newMana)
| otherwise = Nothing
consume :: Parser ()
consume = void . some $ skipSatisfy (not . isDigit)
parseInput :: Parser Boss
parseInput = do
consume
hp <- anyAsciiDecimalInt
consume
Boss hp <$> anyAsciiDecimalInt
play :: Turn -> Pair -> Int
play turn pair =
winGame turn (Q.singleton (pair, (False, False)) 0 ()) (M.singleton pair 0)
part1 :: Bool -> ByteString -> String
part1 _ = show . play doTurn . (me, ) . extract . runParser parseInput
part2 :: Bool -> ByteString -> String
part2 _ = show . play hurtTurn . (me, ) . extract . runParser parseInput