Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit77edc53

Browse files
author
calypso
authored
03 - Haskell
03 - Haskell
2 parents26e5fff +dde0a55 commit77edc53

File tree

6 files changed

+167
-26
lines changed

6 files changed

+167
-26
lines changed

‎2018/01_1/haskell/appositum.hs‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
importData.Foldable (foldl')
2-
importSystem.Environment (getArgs)
1+
importData.Foldable (foldl')
2+
importSystem.Environment (getArgs)
33

44
readInt::String->Int
55
readInt=read
@@ -8,13 +8,11 @@ parse :: String -> Int -> Int
88
parse ('+':n)= (+ readInt n)
99
parse ('-':n)=subtract (readInt n)
1010

11-
loop::Int-> [String]->Int
12-
loop acc lst=
13-
foldl' (flip($)) acc (map parse lst)
14-
1511
main::IO()
1612
main=do
1713
args<- getArgs
1814
case argsof
19-
[]->putStrLn"Usage: ./appositum input.txt"
20-
(arg:_)->readFile arg>>=print. loop0.lines
15+
[]->
16+
putStrLn"Usage: ./appositum input.txt"
17+
(arg:_)->
18+
readFile arg>>=print. foldl' (flip($))0.map parse.lines

‎2018/01_2/haskell/appositum.hs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
importData.List (scanl')
2-
importData.Map (Map)
3-
importqualifiedData.MapasM
4-
importSystem.Environment (getArgs)
1+
importData.List (scanl')
2+
importData.Map (Map)
3+
importqualifiedData.MapasM
4+
importSystem.Environment (getArgs)
55

66
readInt::String->Int
77
readInt=read
@@ -11,20 +11,20 @@ parse ('+':n) = (+ readInt n)
1111
parse ('-':n)=subtract (readInt n)
1212

1313
loop::Int-> [String]-> [Int]
14-
loop acc lst=
15-
scanl' (flip($)) acc$cycle (map parse lst)
14+
loop acc lst= scanl' (flip($)) acc$cycle (map parse lst)
1615

1716
appearTwice::Orda=> [a]->Maybea
1817
appearTwice=recM.emptywhere
18+
rec::Orda=>Mapaa-> [a]->Maybea
1919
rec seen[]=Nothing
2020
rec seen (x:xs)=
2121
caseM.lookup x seenof
22-
Nothing->rec (M.insert x x seen) xs
22+
Nothing->rec (M.insert x x seen) xs
2323
Just found->Just found
2424

2525
main::IO()
2626
main=do
2727
args<- getArgs
2828
case argsof
29-
[]->putStrLn"Usage: ./appositum input.txt"
29+
[]->putStrLn"Usage: ./appositum input.txt"
3030
(arg:_)->readFile arg>>=print. appearTwice. loop0.lines

‎2018/02_1/haskell/appositum.hs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
importData.Map (Map)
2-
importqualifiedData.MapasM
3-
importSystem.Environment
1+
importData.Map (Map)
2+
importqualifiedData.MapasM
3+
importSystem.Environment
44

55
typeDict=MapIntChar
66

77
main::IO()
88
main=do
99
args<- getArgs
1010
case argsof
11-
[]->putStrLn"Usage: ./appositum input.txt"
11+
[]->putStrLn"Usage: ./appositum input.txt"
1212
(arg:_)->readFile arg>>=print. checksum.lines
1313

1414
count::Eqa=>a-> [a]->Int
@@ -25,6 +25,6 @@ threes = M.filterWithKey (\k _ -> k == 3)
2525

2626
checksum:: [String]->Int
2727
checksum strings=length threes'*length twos'where
28-
get f=filter (/=M.empty)$map (f. zipped) strings
28+
get f=filter (/=M.empty)$map (f. zipped) strings
2929
threes'= get threes
3030
twos'= get twos

‎2018/02_2/haskell/appositum.hs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
importSystem.Environment
1+
importSystem.Environment
22

33
main::IO()
44
main=do
55
args<- getArgs
66
case argsof
7-
[]->putStrLn"Usage: ./appositum input.txt"
7+
[]->putStrLn"Usage: ./appositum input.txt"
88
(arg:_)->readFile arg>>=putStrLn. common.lines
99

10+
common:: [String]->String
1011
common strs= common' x ywhere
11-
[x,y]= [x| x<- strs, y<- strs, exactlyOne x y]
12+
[x,y]= [x| x<- strs, y<- strs, exactlyOne x y]
1213

14+
common'::String->String->String
1315
common'"" _=""
1416
common' _""=""
1517
common' (x:xs) (y:ys)
1618
| x== y= x: common' xs ys
1719
|otherwise= common' xs ys
1820

21+
exactlyOne::Eqa=> [a]-> [a]->Bool
1922
exactlyOne xs ys=length filtered==1where
20-
filtered=filter (not.snd)$
21-
zipWith (\x y-> (x, x== y)) xs ys
23+
filtered=filter (not.snd)$zipWith (\x y-> (x, x== y)) xs ys

‎2018/03_1/haskell/appositum.hs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{-#LANGUAGE RecordWildCards #-}
2+
3+
importControl.Applicative (some)
4+
importData.Map (Map)
5+
importqualifiedData.MapasM
6+
importSystem.Environment (getArgs)
7+
importText.Parsec (Parsec,char,digit,parse,spaces)
8+
9+
dataClaim=MkClaim
10+
{claimId,leftEdge,topEdge,width,height::!Integer
11+
}deriving (Eq,Show)
12+
13+
typeFabric=Map (Integer,Integer)Integer
14+
typeParser=ParsecString()
15+
16+
main::IO()
17+
main=do
18+
args<- getArgs
19+
case argsof
20+
[]->putStrLn"Usage: ./appositum input.txt"
21+
(arg:_)->do
22+
readFile arg>>=print. overlap. cut. getClaims.lines
23+
24+
cut:: [Claim]->Fabric
25+
cut= occurrences.concatMap coords
26+
27+
overlap::Fabric->Integer
28+
overlap= count (>1)
29+
30+
count::Foldablet=> (a->Bool)->ta->Integer
31+
count p=foldr f0where
32+
f x z=if p xthen z+1else z
33+
34+
occurrences::Orda=> [a]->MapaInteger
35+
occurrences=M.fromListWith(+).map (\x-> (x,1))
36+
37+
coords::Claim-> [(Integer,Integer)]
38+
coordsMkClaim {..}=do
39+
x<- [leftEdge.. leftEdge+ width-1]
40+
y<- [topEdge.. topEdge+ height-1]
41+
pure (x, y)
42+
43+
getClaims:: [String]-> [Claim]
44+
getClaims=map (justs. parse parseClaimmempty)
45+
where justs (Right x)= x
46+
47+
parseClaim::ParserClaim
48+
parseClaim=do
49+
char'#'
50+
claimID<- integer
51+
symbolic'@'
52+
left<- integer
53+
symbolic','
54+
top<- integer
55+
symbolic':'
56+
width<- integer
57+
char'x'
58+
height<- integer
59+
pure$MkClaim claimID left top width height
60+
61+
integer::ParserInteger
62+
integer=read<$> some digit
63+
64+
symbolic::Char->ParserChar
65+
symbolic c= spaces*> char c<* spaces

‎2018/03_2/haskell/appositum.hs‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{-#LANGUAGE RecordWildCards #-}
2+
3+
importControl.Applicative (some)
4+
importControl.Monad (guard)
5+
importData.Map (Map)
6+
importqualifiedData.MapasM
7+
importSystem.Environment (getArgs)
8+
importText.Parsec (Parsec,char,digit,parse,spaces)
9+
10+
dataClaim=MkClaim
11+
{claimId,leftEdge,topEdge,width,height::!Integer
12+
}deriving (Eq,Show)
13+
14+
typeFabric=Map (Integer,Integer)Integer
15+
typeParser=ParsecString()
16+
17+
main::IO()
18+
main=do
19+
args<- getArgs
20+
case argsof
21+
[]->putStrLn"Usage: ./appositum input.txt"
22+
(arg:_)->do
23+
f<-readFile arg
24+
let claims= getClaims$lines f
25+
let fabric= cut claims
26+
print$ noOverlap fabric claims
27+
28+
cut:: [Claim]->Fabric
29+
cut= occurrences.concatMap coords
30+
31+
overlap::Fabric->Integer
32+
overlap= count (>1)
33+
34+
noOverlap::Fabric-> [Claim]->Integer
35+
noOverlap fabric getClaims=head$do
36+
claim<- getClaims
37+
let intersec=M.intersection fabric (cut [claim])
38+
guard$all (==1) intersec
39+
pure$ claimId claim
40+
41+
count::Foldablet=> (a->Bool)->ta->Integer
42+
count p=foldr f0where
43+
f x z=if p xthen z+1else z
44+
45+
occurrences::Orda=> [a]->MapaInteger
46+
occurrences=M.fromListWith(+).map (\x-> (x,1))
47+
48+
coords::Claim-> [(Integer,Integer)]
49+
coordsMkClaim {..}=do
50+
x<- [leftEdge.. leftEdge+ width-1]
51+
y<- [topEdge.. topEdge+ height-1]
52+
pure (x, y)
53+
54+
getClaims:: [String]-> [Claim]
55+
getClaims=map (justs. parse parseClaimmempty)
56+
where justs (Right x)= x
57+
58+
parseClaim::ParserClaim
59+
parseClaim=do
60+
char'#'
61+
claimID<- integer
62+
symbolic'@'
63+
left<- integer
64+
symbolic','
65+
top<- integer
66+
symbolic':'
67+
width<- integer
68+
char'x'
69+
height<- integer
70+
pure$MkClaim claimID left top width height
71+
72+
integer::ParserInteger
73+
integer=read<$> some digit
74+
75+
symbolic::Char->ParserChar
76+
symbolic c= spaces*> char c<* spaces

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp