Day 6: Make it somewhat more readable

This commit is contained in:
shu 2019-12-06 20:28:10 +01:00
parent d09b565d9c
commit 50431be565

View File

@ -9,13 +9,16 @@ main = do
day6a l = sum $ map (day6a' (transpose l) l) l
day6a' l l2 (s:t)
| s `elem` (l!!1)=1+day6a' l l2 (l2 !! (fromJust (elemIndex s (l!!1))))
| otherwise=1
| s `elem` (l!!1) = 1 + day6a' l l2 next_pair
| otherwise = 1
where next_pair = l2 !! (fromJust $ elemIndex s (l!!1))
day6b :: [[String]] -> Int
day6b l = day6b' l "YOU" "SAN" [] - 2
day6b' l s t visited
| [s,t]`elem`l||[t,s]`elem`l=1
| otherwise = 1 + minimum [if x `elem` visited then maxBound -1 else day6b' l x t (x:visited)| x<-neighbours s]
| otherwise = 1 + minimum next_nodes
where neighbours a = filter (/=a) $ concat
[if x==a then xs else [] | xs<-l, x<-xs]
[if x==a then xs else [] | xs<-l, x<-xs]
next_nodes = [if x `elem` visited then maxBound -1
else day6b' l x t (x:visited)| x<-neighbours s]