-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask15.hs
More file actions
71 lines (55 loc) · 2.15 KB
/
Task15.hs
File metadata and controls
71 lines (55 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Task15 where
import System.IO
import Data.List
import Data.List.Split
data Operation = Remove | Add Int deriving (Show, Eq)
type Label = String
type Hash = Int
type Lens = (Label, Hash, Operation)
type LInBox = (Label, Int)
type Box = (Int, [LInBox])
main :: IO ()
main = do
--handle <- openFile "test15.txt" ReadMode
handle <- openFile "task15.txt" ReadMode
contents <- hGetContents handle
let parsed_data = splitOn "," $ head $ lines contents
hashed = map calcHash parsed_data
lens = map toLens parsed_data
boxes = foldl insertLens generateEmptyBoxes lens
calc = map calcBox boxes
print $ calcHash "HASH"
--print $ hashed
--print $ sum hashed
-------part 2------------
--print $ parsed_data
--print $ lens
print $ boxes
print $ calc
print $ sum calc
hClose handle
toLens :: String -> Lens
toLens str = (label, hash, operation) where
label = takeWhile (\x -> x /= '-' && x /= '=') str
hash = calcHash label
operation = if (str !! (length label)) == '-' then Remove else Add (read $ drop (length label + 1) str)
calcHash :: String -> Int
calcHash = foldl proceedOneChar 0
proceedOneChar :: Int -> Char -> Int
proceedOneChar accum c = ((accum + (fromEnum c)) * 17) `rem` 256
generateEmptyBoxes :: [Box]
generateEmptyBoxes = zip [0..255] (repeat [])
insertLens :: [Box] -> Lens -> [Box]
insertLens boxes lens@(label, hash, operation) = map (\box@(i, lenses) -> if i == hash then insertLens' box lens else box) boxes
insertLens' :: Box -> Lens -> Box
insertLens' box lens@(label, _, Remove) = remLens box lens
insertLens' box@(_,lenses) lens@(label, _, Add n ) = replaceLens box lens
remLens :: Box -> Lens -> Box
remLens (i, lenses) (label, _, Remove) = (i, filter (\(lab,_) -> lab /= label) lenses)
replaceLens :: Box -> Lens -> Box
replaceLens (i, lenses) lens@(label, hash, Add n ) = (i, helper lenses) where
helper :: [LInBox] -> [LInBox]
helper [] = [(label, n)]
helper (l@(lab,_):ls) = if lab /= label then l:(helper ls) else (lab, n):ls
calcBox :: Box -> Int
calcBox (i, lenses) = ((i+1)*) $ sum $ zipWith (\slot (_,focal) -> slot * focal) [1..] lenses