Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (38 sloc) 1.02 KB
TO WORDS :LINE
MAKE "NEW (WORD)
IF EMPTY? :LINE [OUTPUT (LIST (WORD))]
MAKE "REST WORDS BUTFIRST :LINE
IFELSE ((FIRST :LINE) = CHAR 32) [
OUTPUT (FPUT (WORD) :REST)
] [
OUTPUT (FPUT (WORD (FIRST :LINE) (FIRST :REST)) (BUTFIRST REST))
]
END
TO LINEVALID? :WDS
IF EMPTY? :WDS [OUTPUT "TRUE]
MAKE "FST FIRST :WDS
MAKE "REST BUTFIRST :WDS
IF EMPTY? :REST [OUTPUT "TRUE]
MAKE "VALIDS MAP [? <> :FST] :REST
OUTPUT (AND (REDUCE "AND :VALIDS) (LINEVALID? :REST))
END
TO SORT :WD
IF EMPTY? WD [OUTPUT WD]
OUTPUT (WORD
(SORT (FILTER [(RAWASCII ?) < (RAWASCII (FIRST :WD))] :WD))
(FIRST :WD)
(SORT (FILTER [(RAWASCII ?) > (RAWASCII (FIRST :WD))] :WD)))
END
TO VALIDCOUNT :FILENAME
OPENREAD :FILENAME
SETREAD :FILENAME
MAKE "VALID 0
MAKE "VALIDANAGRAMS 0
FOREVER [
MAKE "LINE READWORD
IF EMPTY? :LINE [OUTPUT (LIST :VALID :VALIDANAGRAMS)]
IF LINEVALID? WORDS :LINE [MAKE "VALID :VALID + 1]
IF LINEVALID? (MAP [SORT ?] WORDS :LINE) [MAKE "VALIDANAGRAMS :VALIDANAGRAMS + 1]
]
END
PRINT VALIDCOUNT "/home/benkraft/Downloads/input4