day15: not working
to be discarded, but cool enough to keep around in git I guess
This commit is contained in:
parent
2b6d44b137
commit
be9d88184c
|
@ -1,16 +1,14 @@
|
||||||
{-# LANGUAGE LambdaCase #-}
|
|
||||||
|
|
||||||
import Debug.Trace
|
|
||||||
import Data.List
|
|
||||||
import Text.Read
|
|
||||||
import Control.Arrow
|
import Control.Arrow
|
||||||
|
import Data.Function
|
||||||
|
import Data.List
|
||||||
import Data.List.Split
|
import Data.List.Split
|
||||||
import qualified Data.Map as M
|
import Data.Maybe
|
||||||
|
import Text.Read
|
||||||
|
|
||||||
type HASH = (String, Maybe Int)
|
type HASH = (String, Maybe Int)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = interact $ show . parse . init
|
main = interact $ show . genBuckets . parse . init
|
||||||
|
|
||||||
parse :: String -> [HASH]
|
parse :: String -> [HASH]
|
||||||
parse = map (second (readMaybe . tail) . break (`elem` "=-")) . splitOn ","
|
parse = map (second (readMaybe . tail) . break (`elem` "=-")) . splitOn ","
|
||||||
|
@ -21,12 +19,25 @@ day15a = sum . map (foldl' hashAlg 0) . splitOn ","
|
||||||
hashAlg :: Int -> Char -> Int
|
hashAlg :: Int -> Char -> Int
|
||||||
hashAlg x y = (17 * (fromEnum y + x)) `mod` 256
|
hashAlg x y = (17 * (fromEnum y + x)) `mod` 256
|
||||||
|
|
||||||
removeHash :: [HASH] -> HASH -> [HASH]
|
removeHashes :: [String] -> [HASH] -> [HASH]
|
||||||
removeHash hs (s,n) = undefined
|
removeHashes _ [] = []
|
||||||
|
removeHashes rs ((label, Nothing):xs) = removeHashes (label : rs) xs
|
||||||
|
removeHashes rs ((label, Just x):xs)
|
||||||
|
| label `elem` rs = removeHashes (delete label rs) xs
|
||||||
|
| otherwise = (label, Just x) : removeHashes rs xs
|
||||||
|
|
||||||
|
genBuckets :: [HASH] -> Int
|
||||||
f1 n (x,_) = foldl hashAlg 0 x == n
|
genBuckets input =
|
||||||
f2 s (x,_) = x==s
|
sum
|
||||||
|
[ sum $ zipWith (*) [1 ..] $ map ((* (succ x)) . fromJust . snd) (sorted x)
|
||||||
|
| x <- [0 .. 255]
|
||||||
-- input <- lines <$> readFile "testinput"
|
]
|
||||||
|
where
|
||||||
|
filterBucket x f =
|
||||||
|
nubBy ((==) `on` fst) $
|
||||||
|
f $ removeHashes [] (reverse $ filter (f1 x) input)
|
||||||
|
sorted x =
|
||||||
|
sortOn
|
||||||
|
(\y -> fst y `elemIndex` (map fst (filterBucket x reverse)))
|
||||||
|
(filterBucket x id)
|
||||||
|
f1 n (x, _) = foldl hashAlg 0 x == n
|
||||||
|
|
Loading…
Reference in New Issue
Block a user