Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Somewhat tuned Rye solution #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions src/p98.rye
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,51 @@
; characters--note that the pattern of the source word must also be included to
; distinguish "abc" from "abb" or "aba"), and finally find the intersection of
; word and square transformations and take the largest square.
; Original by Jared Krinke, updated by Janko Metelko

; Utilities
split\characters: fn { s } { s |split "" }
contains?: fn { l i } { ( filter l { = i } ) .length? > 0 }
concat\lists: fn { list } { reduce list 'acc { .concat* acc } }
pairs\permutations: fn { list } { list |map { ::x list |filter { = x |not } |map { ::y [ x y ] } } |concat\lists }
group: fn { list get-key } {
info: list |map { ::o [ o ( do concat { o } get-key ) ] } ,
groups: info |map { .second } |sort |unique ,
groups |map { ::key , [ key ( info |filter { .second = key } ) |map { .first } ] }
}
permute-pairs: fn { list } { .map { ::x list |filter { = x |not } |map { ::y [ x y ] } } |unpack }

; Anagram finder (list of lists)
sort-word: fn { word } { word |split\characters |sort |join }
find-anagrams: fn { l } {
info: l |map { ::w [ w sort-word w ] } ,
anagrams: info |map { .second } |sort |partition { , } |filter { .length? > 1 } |map { .first } ,
info
|filter { ::x contains? anagrams second x }
.map { ::w [ w sort w ] } :info
|map { .second } |sort |partition { , } |filter { .length? > 1 } |map { .first } :anagrams
info .filter { .second .contains* anagrams }
|map { .first }
|group { .sort-word }
|map { .second .pairs\permutations }
|concat\lists
|group { .sort }
|values .to-block
|map { .permute-pairs }
|unpack
}

; "Normalize" two words (could be digits or letters) by calculating the position of characters of a in b
get-transformation: fn { pair } {
a: first pair ,
b: second pair ,
al: ( a |split\characters ) ,
a1: al |map { .position?* a } |join ,
a2: al |map { .position?* b } |join ,
join [ a1 "-" a2 ]
.second :b , .first :a
|split "" |vals\with {
.map { .position?* a } |join ,
.map { .position?* b } |join
} |join\with "-"
}

; Find anagrams from words file and keep transformations only
word-anagrams:
( split\quoted read %0098_words.txt "," `"` )
split\quoted read %0098_words.txt "," `"` ;"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No :) .... Emacs Rye syntax highlighting did't detect the end of the string with "so it colored all next lines as string, this comment was small hack to stop that. I need to fix the highlighter.

|find-anagrams
|map { .get-transformation }
,

; Find square anagrams and compute each pair's transformation, keeping that and the larger square
square-anagrams:
range 1 1000
|map { ::n n * n }
|map { .to-string }
|map { ::n , n * n |to-string }
|find-anagrams
|map { ::pair [ ( max ( pair |map { .to-integer } ) ) ( get-transformation pair ) ] }
|map { .vals\with { .map { .to-integer } |max , .get-transformation } }
,

; Intersect the two lists
anagramic-squares:
square-anagrams
|filter { ::o contains? word-anagrams ( second o ) }
|filter { .second .contains* word-anagrams }
|map { .first }
|print
,