3.2
[advent18.git] / 3 / s2.icl
1 module s2
2
3 import StdEnv
4 import Data.Func
5 import qualified Data.Set as DS
6 import Data.List
7 import Text
8
9 getinput :: [Char] *File -> [[Char]]
10 getinput a f
11 # (ok, c, f) = freadc f
12 | not ok = if (a =: []) [] [reverse a]
13 | c == '\n' = [reverse a:getinput [] f]
14 = getinput [c:a] f
15
16 parse :: [Char] -> (Int, [(Int, Int)])
17 parse c = case split " " (toString c) of
18 [i,_, offset,size]
19 # [offsetx:offsety:_] = split "," offset
20 # offsety = offsety % (0, textSize offsety-2)
21 # [sizex:sizey:_] = split "x" size
22 = (toInt i, [ (x, y)
23 \\x<-[toInt offsetx+1..toInt offsetx+toInt sizex]
24 , y<-[toInt offsety+1..toInt offsety+toInt sizey]
25 ])
26 _ = abort "bork"
27
28 Start w
29 # (io, w) = stdio w
30 = proc $ map parse $ getinput [] io
31
32 proc [x:xs] = p x xs
33 where
34 p (i, cs) rest=:[x:xs]
35 | 'DS'.null $ 'DS'.intersection ('DS'.fromList cs) $ 'DS'.fromList $ flatten $ map snd rest = i
36 | not (trace_t ".") = undef
37 = p x $ xs ++ [(i,cs)]