-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay25.hs
84 lines (71 loc) · 1.83 KB
/
Day25.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
module Main where
import Intcode
import Data.List
-- Input processing
type Input = Memory
parse :: String -> Input
parse = readMemory
-- Part One
type Item = String
type Command = String
-- a tour of the ship (for my input), collecting all the safe items and
-- ending at Security Checkpoint
tour :: [Command]
tour = [
"north",
"west",
"west",
"take easter egg",
"east",
"take mug",
"east",
"south",
"south",
"south",
"west",
"north",
"take jam",
"south",
"east",
"north",
"take asterisk",
"east",
"take klein bottle",
"south",
"west",
"west",
"take cake",
"east",
"take tambourine",
"south",
"east",
"take polygon",
"north"]
-- all items picked up of the tour
allItems :: [Item]
allItems = [drop 5 cmd | cmd <- tour, take 5 cmd == "take "]
-- Collect all the safe items, move to the Security Checkpoint and try to
-- get past the Pressure-Sensitive Floor by trying all subsets of items.
script :: [String]
script = tour ++ concat
[map ("drop " ++) items ++ ["east"] ++ map ("take " ++) items |
items <- subsequences allItems]
-- quoted strings in the input
quotes :: String -> [String]
quotes s = case dropWhile (/= '"') s of
[] -> []
_:t -> case span (/= '"') t of
(_, []) -> error "Unbalanced quotes"
(front, _:back) -> front : quotes back
-- Error messages from the PSF start with "Alert!". Otherwise we have
-- a success message from the PSF, followed by a message with the password.
solve1 :: Input -> String
solve1 mem =
head $ tail $ dropWhile ("Alert!" `isPrefixOf`) $ quotes $
map fromValue . streamFunction mem . map toValue $ unlines $ script
-- there is no Part Two on Day 25
main :: IO ()
main = do
s <- readFile "input/25.txt"
let input = parse s
putStrLn (solve1 input)