Browse Source

further improvements

master
Arranun 2 years ago
parent
commit
aec425b1e0
  1. 58
      day18rw.hs

58
day18rw.hs

@ -10,9 +10,9 @@ main = do
mapIn <- lines <$> getContents
let connections = concat $ [(getConnections mapIn a b) | a <-"@abcdefghijklmnop", b <- "@abcdefghijklmnop", a /= b]
let state = State ['@'] 0 '@'
let result = part1 [state] connections
let result = part1 [state] [] connections 999
mapM putStrLn(map show connections)
mapM putStrLn(map show result)
putStrLn(show result)
data Connection =Connection { key1 :: Char,
key2 :: Char,
@ -32,20 +32,30 @@ data State = State { elements :: [Char],
} deriving (Show,Eq)
part1 :: [State] -> [Connection] -> [State]
part1 states conns
|length (filter(\(State elm _ _) -> (length elm) >= 17) (Trace.traceShowId((states)))) >= 1 =
states
part1 :: [State] -> [State] -> [Connection] -> Int -> Int
part1 states deadStates conns minimum
|length finished > 3 =
min
|otherwise = do
let choose = chooseNext states
let newStates = runState choose conns
let nextStates = newStates:(states)
part1 nextStates conns
chooseNext :: [State] -> State
chooseNext states = do
let possible = filter(\(State elm len pos) -> (length elm) < 17) states
last ( sortBy sortElm possible )
let choose = chooseNext (filtered)(min)
let newStates = runState (choose) deadStates conns min
if (newStates) == choose
then do
let newDeadStates = newStates:deadStates
part1 states newDeadStates conns min
else do
let nextStates = newStates:(states)
part1 nextStates deadStates conns min
where finished = (filter(\(State elm _ _) -> (length elm) >= 17) (states))
min = if length finished > 0
then List.minimum (map(\(State _ l _) -> l) finished)
else minimum
filtered = states \\ deadStates
chooseNext :: [State] -> Int -> State
chooseNext states min = do
let possible' = filter(\(State elm len pos) -> (length elm) < 17) states
let possible = filter(\(State elm len pos) -> len < min) possible'
last ( sortBy sortElm (possible))
sortLen :: State -> State -> Ordering
@ -62,8 +72,8 @@ sortElm (State e1 l1 _) (State e2 l2 _)
| l1 < l2 = LT
| otherwise = EQ
runState :: State -> [Connection] -> State
runState (State elm len pos) conns
runState :: State -> [State] -> [Connection] -> Int -> State
runState (State elm len pos) deadStates conns min
| length possible == 0 = (State elm len pos)
| length possible == 1 = do
let c = head possible
@ -72,15 +82,19 @@ runState (State elm len pos) conns
let choose = head (sortBy sortConn possible)
-- let newStates = map(\c -> stepState (State elm len pos) c) possible
stepState (State elm len pos) choose
where possible'' = filter(\(Connection k1 k2 l b) -> length ( b \\ notBlocked) == 0 )possible'''
possible = filter(\(Connection k1 k2 l b) -> notElem k2 elm) possible''
possible''' = filter(\(Connection k1 k2 l b) -> k1 == pos) conns
notBlocked = concat $ map(\x -> (toUpper x):[x]) elm
where possible''' = filter(\(Connection k1 k2 l b) -> length ( b \\ notBlocked) == 0 )possible''''
possible'' = filter(\(Connection _ _ l _) -> len + l < min) possible'''
possible' = filter(\(Connection k1 k2 l b) -> notElem k2 (deadPath)) possible''
possible = filter(\(Connection k1 k2 l b) -> notElem k2 elm) possible'
possible'''' = filter(\(Connection k1 k2 l b) -> k1 == pos) conns
notBlocked = concat $ map(\x -> (toUpper x):[x]) elm
dead = filter(\(State el _ _) -> (tail el) == elm) deadStates
deadPath = map(\(State el _ _) -> head el) dead
sortConn :: Connection -> Connection -> Ordering
sortConn (Connection _ _ l1 _) (Connection _ _ l2 _)
| l1 < l2 = LT
| l1 > l2 = LT
| l1 > l2 = GT
| l1 == l2 = EQ
stepState :: State -> Connection -> State

Loading…
Cancel
Save