-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNIC.hs
54 lines (43 loc) · 4.82 KB
/
NIC.hs
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
module NIC (main, moduli) where
import Data.List
moduli = map (2^64-) [0,3,5,7,9,11,15,17,33,35,39,45,47,53,57,59,63,75,77,83,87,89,95,99,105,113,117,119,125,129,143,147,153,155,165,173,179,183,189,195,197,209,215,243,249,255]
legal 18 = [63690581,44701699729693429,15332091188757329557096929,4940390007166855988604029802787401,1595505507686882715749002172414020741857729,515636677864163219524895211053049897531811801250821,166650920257024423462011294123136976471812572057940449253441,53860511232216228878845000028552928369632487096643854242441769229265,17407361950309381558491909847176225713327003640745414506101469276989824040893,5625944437839316725417034733484676655060046437494303568101196911011250778734538370313,1818268101063397348407524320338136087085311490583296300831347748142859813828927874950019600689,587652246754347739368657083786586921998972008934930695194600937915164703102566384006494100112174487205,189925326693899656453203047332679694920317003062440222340200761360400797080262175961405709113356773889177857525,61382611772199274334636269175293250152967293943474157074284463810371864197832046374971583974267484965949417106776293161,19838454899952826417949292466806306458168271646619328408135679476232634284924466314674347155249080739678632648220810574456275957,6411657657677713565232032263939927344282021848723432281335590691938564423567053274675106473315771865205000065292204622856107114049580445,2072205427619023303039587520236390121754274072718784609433998196933282608067036314403465202963700297341152216286750576593627459392979397487964077,669723114288829212892740188841706543509937780640178732810318337696945624428547218105214326012774371397184848890970111836283470468812827907149926502347633]
legal 19 = [ 176377839, 381468772192258129, 390125285764888664734203691, 374033672392630508524864795662961961, 359448796707478853662201398479404356886186983, 345681908391253482444700805766256890688374132702150281, 332456409626323552089753008110920578989152425860059980776167267, 319736046569415960553023346593909692027276993628971883863936977542620829, 307502197883843189728145070683195306634715300572715148091618574821423047802111371, 295736438538105920753848805101221518347169034846464241638380603304292532634709193160046357, 284420866155234999231087007390392444099790919003942969485429344438762552097336795489845181754603195, 273538254349547522259589180544472876860684075817857666308521432524590908173589166299381140508333672348231869, 263072036883964825093321085743751624592085890795667877968046914722839766002860060188356044538992936620738346077533371, 253006281532576453382590877761237615086708861593042289202522301490010243250555590112007735795468718567589774511008063937101981, 243325665673703086779483164699776334863653761239608010895221752077059186635289648588694650419857894777881877451704872426644446631397547, 234015452963882018736875664431930231575827140398694847657266616194101746493170926436132593907169134296767239208215341305209205413117620066366669 ]
eqlyx (_,_,_,a) (_,_,_,b) = a==b
addyx m (i0,n0,l0,yx) (i1,n1,l1,_) = ((i0+i1) `mod` m,
(n0+n1) `mod` m,
(l0+l1) `mod` m, yx)
eqlyx1 (a,_,_) (b,_,_) = a==b
addyx1 (yx,s0,x0) (_,s1,x1) = (yx,s0+s1,x0+x1)
read1 "-nan" = 0
read1 "nan" = 0
read1 a | '.' `elem` a = 1
read1 a = read a
main = do
inp <- getContents
let (width,modidx) = head [(read w::Int,read m::Int) |
line <- lines inp,
let (bin:w:m:_) = words line,
"/start" `isSuffixOf` bin]
putStrLn $ "width "++show width++" modidx "++show modidx
let modulus = moduli !! modidx
let sizes = [(read yx, read size, read1 xsize) |
line <- lines inp,
"(" `isPrefixOf` line,
let (yx:_:size:_:xsize:_) = words line] ::
[((Int,Int),Integer,Integer)]
let cumsizes = map (foldr1 addyx1) $ groupBy eqlyx1 sizes
let cnts = [(read newill, read needy, read legal, read yx) |
line <- lines inp,
"newillegal" `isPrefixOf` line,
let (_:newill:_:needy:_:legal:_:yx:_) = words line] ::
[(Integer,Integer,Integer,(Int,Int))]
let cumcnts = map (foldr1 (addyx modulus)) $ groupBy eqlyx cnts
let cnt1 = map (\(i,n,l,yx) -> i+n+l `mod` modulus) cumcnts
let cnt3 = map (\(i,n,l,yx) -> 3*(n+l) `mod` modulus) cumcnts
let diffs = zipWith (\x y-> (x-y) `mod` modulus) cnt1 (3:cnt3)
let check = zipWith3 (\(yx,s,x) (i,n,l,_) d -> (yx,s,x,l,d) ) cumsizes cumcnts diffs
mapM_ print check
let rowlegal = [l | (i,n,l,(y,x))<-cumcnts, x==0]
let check19 = zipWith (\x y-> (x-y) `mod` modulus) rowlegal (legal width)
mapM_ print $ zip [1..] check19