-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
123 lines (101 loc) · 3.17 KB
/
run.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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
import AoC
import AoC.Grid
import Data.Int
import Control.Monad (guard)
import Data.List.Split (splitOn)
import Data.Maybe (mapMaybe)
import Text.Read (readMaybe)
type N = Int8
-- TODO: Write up an explanation for WTH is happening here.
data Block = Push N N
| Pop N N
deriving (Show, Eq)
isPop :: Block -> Bool
isPop = \case Pop _ _ -> True
_ -> False
pp :: N -> Block -> String
pp i = unlines . \case
Push t1 t2 -> [ "if " ++ show t1 ++ " + z != w_" ++ show i
, " then push z; z = w_" ++ show i ++ " + " ++ show t2
]
Pop t1 t2 -> [ "if " ++ show t1 ++ " + z != w_" ++ show i
, " then z = w_" ++ show i ++ " + " ++ show t2
, " else pop z"
]
blocks :: [String] -> [Block]
blocks = map translate
. filter (not . null)
. splitOn ["inp w"]
translate :: [String] -> Block
translate =
toOp
. filter (/= 0)
. mapMaybe (readMaybe @N)
. concatMap words
toOp :: [N] -> Block
toOp [26, pop, term1, 25, 1, term2]
| pop == 1 = Push term1 term2
| otherwise = Pop term1 term2
toOp mismatch = error $ "fail on: " ++ show mismatch
zval :: [N] -> N
zval = \case [] -> 0
z:_ -> z
zset :: N -> [N] -> [N]
zset z = \case [] -> [z]
_:zs -> z:zs
zpop :: [N] -> [N]
zpop = \case [] -> []
_:zs -> zs
zpushset :: N -> [N] -> [N]
zpushset z = (z:)
findAll :: [N] -> [Block] -> [[N]]
findAll digits startBlocks = go 0 popBlocks [] [] startBlocks
where go pushes remPops zs ws = \case
[] | sum zs == 0 -> [reverse ws]
| otherwise -> []
Pop t1 t2:bs -> do
let z = zval zs
if remPops > pushes
then do
w <- digits
if t1 + z /= w
then go pushes (remPops - 1) (zset (w + t2) zs) (w:ws) bs
else go (pushes - 1) (remPops - 1) (zpop zs) (w:ws) bs
else do
w <- digits
guard $ t1 + z == w
go (pushes - 1) (remPops - 1) (zpop zs) (w:ws) bs
Push t1 t2:bs -> do
let z = zval zs
if remPops > pushes
then do
w <- digits
if t1 + z /= w
then go (pushes + 1) remPops (zpushset (w + t2) zs) (w:ws) bs
else go pushes remPops zs (w:ws) bs
else do
w <- digits
guard $ t1 + z == w
go pushes remPops zs (w:ws) bs
popBlocks = length . filter isPop $ startBlocks
test = [Push 1 2, Pop 2 3]
parseAll :: String -> [Block]
parseAll = blocks . lines--map parse . lines
solve :: [N] -> [Block] -> Integer
solve digits = read @Integer . concatMap show . head . findAll digits
part1 :: [Block] -> Integer
part1 = solve [9,8..1]
part2 :: [Block] -> Integer
part2 = solve [1..9]
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)