2024-23 part 1 in haskell
This commit is contained in:
parent
f95feae2a0
commit
ee4ba011c5
3 changed files with 3482 additions and 0 deletions
32
2024/23-LAN_Party/example
Normal file
32
2024/23-LAN_Party/example
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
kh-tc
|
||||||
|
qp-kh
|
||||||
|
de-cg
|
||||||
|
ka-co
|
||||||
|
yn-aq
|
||||||
|
qp-ub
|
||||||
|
cg-tb
|
||||||
|
vc-aq
|
||||||
|
tb-ka
|
||||||
|
wh-tc
|
||||||
|
yn-cg
|
||||||
|
kh-ub
|
||||||
|
ta-co
|
||||||
|
de-co
|
||||||
|
tc-td
|
||||||
|
tb-wq
|
||||||
|
wh-td
|
||||||
|
ta-ka
|
||||||
|
td-qp
|
||||||
|
aq-cg
|
||||||
|
wq-ub
|
||||||
|
ub-vc
|
||||||
|
de-ta
|
||||||
|
wq-aq
|
||||||
|
wq-vc
|
||||||
|
wh-yn
|
||||||
|
ka-de
|
||||||
|
kh-ta
|
||||||
|
co-tc
|
||||||
|
wh-qp
|
||||||
|
tb-vc
|
||||||
|
td-yn
|
70
2024/23-LAN_Party/first.hs
Normal file
70
2024/23-LAN_Party/first.hs
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
-- requires cabal install --lib megaparsec parser-combinators heap vector
|
||||||
|
module Main (main) where
|
||||||
|
|
||||||
|
import Control.Monad (void, when)
|
||||||
|
import Data.Functor
|
||||||
|
import qualified Data.List as L
|
||||||
|
import qualified Data.Map as M
|
||||||
|
import Data.Maybe
|
||||||
|
import Data.Ord (comparing)
|
||||||
|
import qualified Data.Set as S
|
||||||
|
import Data.Void (Void)
|
||||||
|
import Text.Megaparsec
|
||||||
|
import Text.Megaparsec.Char
|
||||||
|
|
||||||
|
import Debug.Trace
|
||||||
|
|
||||||
|
exampleExpectedOutput = 7
|
||||||
|
|
||||||
|
type Computer = String
|
||||||
|
type Link = (Computer, Computer)
|
||||||
|
type Input = [Link]
|
||||||
|
|
||||||
|
type Parser = Parsec Void String
|
||||||
|
|
||||||
|
parseLink :: Parser Link
|
||||||
|
parseLink = (,) <$> some letterChar <* char '-'
|
||||||
|
<*> some letterChar
|
||||||
|
|
||||||
|
parseInput' :: Parser Input
|
||||||
|
parseInput' = some (parseLink <* eol) <* eof
|
||||||
|
|
||||||
|
parseInput :: String -> IO Input
|
||||||
|
parseInput filename = do
|
||||||
|
input <- readFile filename
|
||||||
|
case runParser parseInput' filename input of
|
||||||
|
Left bundle -> error $ errorBundlePretty bundle
|
||||||
|
Right input' -> return input'
|
||||||
|
|
||||||
|
type Network = M.Map Computer [Computer]
|
||||||
|
type NetGroup = S.Set [Computer]
|
||||||
|
|
||||||
|
compute :: Input -> Int
|
||||||
|
compute links = length tgroups
|
||||||
|
where
|
||||||
|
tgroups :: [[Computer]]
|
||||||
|
tgroups = L.nub $ L.filter (\(a:b:c:[]) -> and [isConnected a b, isConnected a c, isConnected b c]) $ L.sort $ map L.sort $ concatMap tgroup thosts
|
||||||
|
tgroup :: Computer -> [[Computer]]
|
||||||
|
tgroup c = map (\ns -> (c:ns)) $ tgroup' (network M.! c)
|
||||||
|
tgroup' :: [Computer] -> [[Computer]]
|
||||||
|
tgroup' (n:ns) = map (\c -> [n,c]) ns ++ tgroup' ns
|
||||||
|
tgroup' _ = []
|
||||||
|
isConnected :: Computer -> Computer -> Bool
|
||||||
|
isConnected c1 c2 = L.elem c1 $ network M.! c2
|
||||||
|
thosts :: [Computer]
|
||||||
|
thosts = M.keys $ M.filterWithKey (\k _ -> head k == 't') network
|
||||||
|
network = L.foldl' connect M.empty links
|
||||||
|
connect :: Network -> Link -> Network
|
||||||
|
connect net (c1, c2) = connectOne c1 c2 $ connectOne c2 c1 net
|
||||||
|
connectOne :: Computer -> Computer -> Network -> Network
|
||||||
|
connectOne c1 c2 net = case M.lookup c1 net of
|
||||||
|
Just l -> M.insert c1 (c2:l) net
|
||||||
|
Nothing -> M.insert c1 [c2] net
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
example <- parseInput "example"
|
||||||
|
let exampleOutput = compute example
|
||||||
|
when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
|
||||||
|
input <- parseInput "input"
|
||||||
|
print $ compute input
|
3380
2024/23-LAN_Party/input
Normal file
3380
2024/23-LAN_Party/input
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue