diff options
author | Akshay <[email protected]> | 2020-12-08 07:03:22 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-08 07:03:22 +0000 |
commit | 06b14ed84c0a8ec11462f0a6f7397c3e3a52654d (patch) | |
tree | 5d28d13ede49b48f63b42fd31816f0e98a68aca6 /execs | |
parent | 90ee9892e2e0fd188bac09c50987c967cf282333 (diff) |
add initial day08 solution
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day08.hs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/execs/Day08.hs b/execs/Day08.hs new file mode 100644 index 0000000..7d3fce8 --- /dev/null +++ b/execs/Day08.hs | |||
@@ -0,0 +1,45 @@ | |||
1 | module Main where | ||
2 | |||
3 | import Data.Set (Set) | ||
4 | import qualified Data.Set as Set | ||
5 | import Data.Map (Map) | ||
6 | import qualified Data.Map as Map | ||
7 | import Utils | ||
8 | |||
9 | type Op = (String, Int) | ||
10 | |||
11 | parseLine :: [String] -> Op | ||
12 | parseLine [s, j] = (s, read (dropWhile (== '+') j)) | ||
13 | |||
14 | run :: Int -> Int -> Set Int -> Map Int Op -> Int | ||
15 | run acc pc visited operations = if Set.member pc visited then acc else handleCase | ||
16 | where visited' = Set.insert pc visited | ||
17 | handleCase = | ||
18 | case Map.lookup pc operations of | ||
19 | Just ("acc", v) -> run (acc + v) (pc + 1) visited' operations | ||
20 | Just ("nop", _) -> run acc (pc + 1) visited' operations | ||
21 | Just ("jmp", j) -> run acc (pc + j) visited' operations | ||
22 | _ -> acc | ||
23 | |||
24 | doesEnd :: Int -> Int -> Set Int -> Map Int Op -> Bool | ||
25 | doesEnd acc pc visited operations = not (Set.member pc visited) && handleCase | ||
26 | where visited' = Set.insert pc visited | ||
27 | handleCase = | ||
28 | case Map.lookup pc operations of | ||
29 | Just ("acc", v) -> doesEnd (acc + v) (pc + 1) visited' operations | ||
30 | Just ("nop", _) -> doesEnd acc (pc + 1) visited' operations | ||
31 | Just ("jmp", j) -> doesEnd acc (pc + j) visited' operations | ||
32 | _ -> True -- pc has crossed the end! | ||
33 | |||
34 | genAll :: [Op] -> [[Op]] | ||
35 | genAll [] = [] | ||
36 | genAll (n@("nop",v):rest) = (("jmp",v):rest) : map (n:) (genAll rest) | ||
37 | genAll (j@("jmp",v):rest) = (("nop",v):rest) : map (j:) (genAll rest) | ||
38 | genAll (acc:rest) = map (acc:) $ genAll rest | ||
39 | |||
40 | main :: IO () | ||
41 | main = do | ||
42 | n <- map (parseLine . words) . lines <$> readFile "input/08" | ||
43 | let solve1 = run 0 0 Set.empty . Map.fromList . zip [0..] | ||
44 | print $ solve1 n | ||
45 | print $ solve1 $ head $ filter (doesEnd 0 0 Set.empty . Map.fromList . zip [0..]) $ genAll n | ||