From 4b528f20962543f49f34b63f80c6268bcd03155e Mon Sep 17 00:00:00 2001 From: Refaktor Date: Tue, 21 Jan 2025 12:21:59 +0100 Subject: [PATCH 1/2] A little tuned in and updated Rye example p98.rye Jared made a great example! I tried to tune it in a little more, and use some more already available (but updated) functions like `group` and `contains` and `unpack`. He also proposed intersection\by and sort\by , which I added to Rye, but didn't use here at the end. Maybe he can and make code even nicer :) --- src/p98.rye | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/p98.rye b/src/p98.rye index a5c3e76..2ca1124 100644 --- a/src/p98.rye +++ b/src/p98.rye @@ -4,44 +4,35 @@ ; 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 "," `"` ;" |find-anagrams |map { .get-transformation } , @@ -49,16 +40,15 @@ word-anagrams: ; 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 , - From 1ddb9a896dbf7c7f701250d5d40e922c48e91ab1 Mon Sep 17 00:00:00 2001 From: Refaktor Date: Tue, 21 Jan 2025 12:23:14 +0100 Subject: [PATCH 2/2] Update p98.rye - indentation --- src/p98.rye | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/p98.rye b/src/p98.rye index 2ca1124..5d336c9 100644 --- a/src/p98.rye +++ b/src/p98.rye @@ -15,8 +15,8 @@ find-anagrams: fn { l } { |map { .second } |sort |partition { , } |filter { .length? > 1 } |map { .first } :anagrams info .filter { .second .contains* anagrams } |map { .first } - |group { .sort } - |values .to-block + |group { .sort } + |values .to-block |map { .permute-pairs } |unpack } @@ -25,8 +25,8 @@ find-anagrams: fn { l } { get-transformation: fn { pair } { .second :b , .first :a |split "" |vals\with { - .map { .position?* a } |join , - .map { .position?* b } |join + .map { .position?* a } |join , + .map { .position?* b } |join } |join\with "-" }