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