projects
/
advent-of-code-16.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
1470230
)
Tidying
author
Neil Smith
<neil.git@njae.me.uk>
Sat, 17 Dec 2016 11:03:59 +0000
(11:03 +0000)
committer
Neil Smith
<neil.git@njae.me.uk>
Sat, 17 Dec 2016 11:03:59 +0000
(11:03 +0000)
advent17.hs
patch
|
blob
|
history
diff --git
a/advent17.hs
b/advent17.hs
index d255a09bf594b6c8d8dca64daa2f103778dd6ec5..c99b8da2aabdac93d5b5344110d0a3209510fece 100644
(file)
--- a/
advent17.hs
+++ b/
advent17.hs
@@
-1,11
+1,8
@@
-import Data.List (subsequences, (\\), sort, sortBy)
-import Data.Ord (comparing)
import Data.ByteString.Char8 (pack)
import Data.ByteString.Char8 (pack)
-import Crypto.Hash (hash, Digest, MD5)
-
+import qualified Crypto.Hash as C
type Position = (Int, Int)
type Position = (Int, Int)
-data Agendum = Agendum {position :: Position, path :: String, hsh :: String} deriving (Show, Eq)
+data Agendum = Agendum {position :: Position, path :: String, h
a
sh :: String} deriving (Show, Eq)
type Agenda = [Agendum]
-- input = "hijkl"
type Agenda = [Agendum]
-- input = "hijkl"
@@
-13,46
+10,39
@@
type Agenda = [Agendum]
input = "qljzarfv" -- my input
input = "qljzarfv" -- my input
-
-
main :: IO ()
main = do
part1
part2
main :: IO ()
main = do
part1
part2
-initialAgenda = [Agendum {position=(1, 1), path="", hsh=(getHash "")}]
-
-
part1 :: IO ()
part1 = print $ path $ extractJust $ bfs initialAgenda
part2 :: IO ()
part2 = print $ bfs2 initialAgenda 0
part1 :: IO ()
part1 = print $ path $ extractJust $ bfs initialAgenda
part2 :: IO ()
part2 = print $ bfs2 initialAgenda 0
+initialAgenda :: Agenda
+initialAgenda = [Agendum {position=(1, 1), path="", hash=(getHash "")}]
getHash :: String -> String
getHash :: String -> String
-getHash path = show (hash $ pack (input ++ path) :: Digest MD5)
-
+getHash path = show (C.hash $ pack (input ++ path) :: C.Digest C.MD5)
extractJust :: Maybe Agendum -> Agendum
extractJust Nothing = head initialAgenda
extractJust (Just x) = x
extractJust :: Maybe Agendum -> Agendum
extractJust Nothing = head initialAgenda
extractJust (Just x) = x
-
bfs :: Agenda -> Maybe Agendum
bfs [] = Nothing
bfs (current:agenda) =
if isGoal current then Just current
else bfs (agenda ++ (successors current))
bfs :: Agenda -> Maybe Agendum
bfs [] = Nothing
bfs (current:agenda) =
if isGoal current then Just current
else bfs (agenda ++ (successors current))
-
bfs2 :: Agenda -> Int -> Int
bfs2 [] l = l
bfs2 (current:agenda) l =
if isGoal current then bfs2 agenda (length $ path $ current)
else bfs2 (agenda ++ (successors current)) l
bfs2 :: Agenda -> Int -> Int
bfs2 [] l = l
bfs2 (current:agenda) l =
if isGoal current then bfs2 agenda (length $ path $ current)
else bfs2 (agenda ++ (successors current)) l
-
isGoal :: Agendum -> Bool
isGoal agendum = (position agendum) == (4, 4)
isGoal :: Agendum -> Bool
isGoal agendum = (position agendum) == (4, 4)
@@
-62,25
+52,23
@@
isLegalPos p = fst p >= 1 && fst p <= 4 && snd p >= 1 && snd p <= 4
successors :: Agendum -> Agenda
successors state = [Agendum {position = step p0 ld,
path = path0 ++ [ld],
successors :: Agendum -> Agenda
successors state = [Agendum {position = step p0 ld,
path = path0 ++ [ld],
- hsh = getHash (path0 ++ [ld])} | ld <- legalDoors ]
+ h
a
sh = getHash (path0 ++ [ld])} | ld <- legalDoors ]
where
p0 = position state
path0 = path state
where
p0 = position state
path0 = path state
- h0 = hsh state
+ h0 = h
a
sh state
doors = openDoors h0
legalDoors = filter (isLegalPos . (step p0)) doors
doors = openDoors h0
legalDoors = filter (isLegalPos . (step p0)) doors
-openDoors hash = (u hash) ++ (d hash) ++ (l hash) ++ (r hash)
+openDoors :: String -> String
+openDoors h = (u h) ++ (d h) ++ (l h) ++ (r h)
-u h
ash = if has
h!!0 `elem` "bcdef" then "U" else ""
-d h
ash = if has
h!!1 `elem` "bcdef" then "D" else ""
-l h
ash = if has
h!!2 `elem` "bcdef" then "L" else ""
-r h
ash = if has
h!!3 `elem` "bcdef" then "R" else ""
+u h
= if
h!!0 `elem` "bcdef" then "U" else ""
+d h
= if
h!!1 `elem` "bcdef" then "D" else ""
+l h
= if
h!!2 `elem` "bcdef" then "L" else ""
+r h
= if
h!!3 `elem` "bcdef" then "R" else ""
step (r, c) 'U' = (r-1, c)
step (r, c) 'D' = (r+1, c)
step (r, c) 'L' = (r, c-1)
step (r, c) 'R' = (r, c+1)
step (r, c) 'U' = (r-1, c)
step (r, c) 'D' = (r+1, c)
step (r, c) 'L' = (r, c-1)
step (r, c) 'R' = (r, c+1)
-
-
-