Skip to main content
saved another char
Source Link
Justin
  • 21.3k
  • 9
  • 67
  • 114

Ruby, 206 200200 199

R=0q,i,*R=*-2..27
q=0
i=-1
puts"#{i+=1}: #{$.,s=(0-2..q).map{x=R.map{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]};[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}.max;q=99;smax;q=97;s.join} -- score: #$."until$.>27

The first line is simply a fancy way to define q=-2, i=-1, and R=(0..27).to_a. All the work is done in the 4th2nd line:

puts"..."until$.>27 # Prints the string in quotes until we reach the desired score
     ^
     |
 +---+
 |
"#{i+=1}: #{...} -- score: #$."
   ^        ^               ^  
   +--------|---------------|-- Generation counter
 +----------+---------------|-- Current string
 |                          +-- Score of current string (interpolates the `$.` variable)
 |   
 #{$.,s=(0-2..q).map{...}.max;q=99;smax;q=97;s.join} # Generate the score & string
   ^         ^  ^   ^    ^   ^    ^
   +---------|--|---|----|---|----|------- Store the score; this variable makes
             |  |   |    |   |    |        string interpolation shorter.
             +--|---|----|---+----|------- `q` automatically takes care of generating
                |   |    |        |        the string vs randomizing the string.
                +---|----|--------|------  Make 100 (or 1 the first time) strings,
                    |    |        |        and compute their score.
                    |    +--------|------- Take the string with the max score.
 +------------------+             +------- `s` is stored as an array
 |
 x=R.map{...};[R.count{...},x] # Compute string and its score, store in array
   ^     ^    ^^       ^
   +-----|----|+-------|------ `R` is 0..27, we use the constant to save chars.
         |    +--------|------ `max` orders by first element, then second. We clearly want
         |             |       the highest score, so make the score first.
 +-------+-------------|------ Generate the string, store in `x`.
 |                     +------ Count the number of chars that overlap with 'METHINKS...'
 |                     |
 |                    {|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]}
{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]}
    ^   ^         ^             ^       ^
    +---+---------|-------------|-------|---- 5% chance of randomizing, or 100% for
                  |             |       |     first string.
                  +-------------+-------|---- Sample from alphabet + ' '.
                                        +---- Don't alter the string 95% of the time

Ruby, 206 200

R=0..27
q=0
i=-1
puts"#{i+=1}: #{$.,s=(0..q).map{x=R.map{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]};[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}.max;q=99;s.join} -- score: #$."until$.>27

All the work is done in the 4th line:

puts"..."until$.>27 # Prints the string in quotes until we reach the desired score
     ^
     |
 +---+
 |
"#{i+=1}: #{...} -- score: #$."
   ^        ^               ^  
   +--------|---------------|-- Generation counter
 +----------+---------------|-- Current string
 |                          +-- Score of current string (interpolates the `$.` variable)
 |   
 #{$.,s=(0..q).map{...}.max;q=99;s.join} # Generate the score & string
   ^        ^  ^   ^    ^   ^    ^
   +--------|--|---|----|---|----|------- Store the score; this variable makes
            |  |   |    |   |    |        string interpolation shorter.
            +--|---|----|---+----|------- `q` automatically takes care of generating
               |   |    |        |        the string vs randomizing the string.
               +---|----|--------|------  Make 100 (or 1 the first time) strings,
                   |    |        |        and compute their score.
                   |    +--------|------- Take the string with the max score.
 +-----------------+             +------- `s` is stored as an array
 |
 x=R.map{...};[R.count{...},x] # Compute string and its score, store in array
   ^     ^    ^^       ^
   +-----|----|+-------|------ `R` is 0..27, we use the constant to save chars.
         |    +--------|------ `max` orders by first element, then second. We clearly want
         |             |       the highest score, so make the score first.
 +-------+-------------|------ Generate the string, store in `x`.
 |                     +------ Count the number of chars that overlap with 'METHINKS...'
 |                     |
 |                    {|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]}
{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]}
    ^   ^         ^             ^       ^
    +---+---------|-------------|-------|---- 5% chance of randomizing, or 100% for
                  |             |       |     first string.
                  +-------------+-------|---- Sample from alphabet + ' '.
                                        +---- Don't alter the string 95% of the time

Ruby, 206 200 199

q,i,*R=*-2..27
puts"#{i+=1}: #{$.,s=(-2..q).map{x=R.map{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]};[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}.max;q=97;s.join} -- score: #$."until$.>27

The first line is simply a fancy way to define q=-2, i=-1, and R=(0..27).to_a. All the work is done in the 2nd line:

puts"..."until$.>27 # Prints the string in quotes until we reach the desired score
     ^
     |
 +---+
 |
"#{i+=1}: #{...} -- score: #$."
   ^        ^               ^  
   +--------|---------------|-- Generation counter
 +----------+---------------|-- Current string
 |                          +-- Score of current string (interpolates the `$.` variable)
 |   
 #{$.,s=(-2..q).map{...}.max;q=97;s.join} # Generate the score & string
   ^         ^  ^   ^    ^   ^    ^
   +---------|--|---|----|---|----|------ Store the score; this variable makes
             |  |   |    |   |    |       string interpolation shorter.
             +--|---|----|---+----|------ `q` automatically takes care of generating
                |   |    |        |        the string vs randomizing the string.
                +---|----|--------|------  Make 100 (or 1 the first time) strings,
                    |    |        |        and compute their score.
                    |    +--------|------- Take the string with the max score.
 +------------------+             +------- `s` is stored as an array
 |
 x=R.map{...};[R.count{...},x] # Compute string and its score, store in array
   ^     ^    ^^       ^
   +-----|----|+-------|------ `R` is 0..27, we use the constant to save chars.
         |    +--------|------ `max` orders by first element, then second. We clearly want
         |             |       the highest score, so make the score first.
 +-------+-------------|------ Generate the string, store in `x`.
 |                     +------ Count the number of chars that overlap with 'METHINKS...'
 |                     |
 |                    {|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]}
{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]}
    ^   ^         ^             ^       ^
    +---+---------|-------------|-------|---- 5% chance of randomizing, or 100% for
                  |             |       |     first string.
                  +-------------+-------|---- Sample from alphabet + ' '.
                                        +---- Don't alter the string 95% of the time
added 170 characters in body
Source Link
Justin
  • 21.3k
  • 9
  • 67
  • 114

Ruby, 206206 200

R=0..27
q=0
i=-1
puts"#{i+=1}: #{$.,s=(0..q).map{Rx=R.map{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]}}.map{|x|[R;[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}.max;q=99;s.join} -- score: #$."until$.>27
puts"..."until$."                   >27 # Prints the string in quotes until we reach the desired score
     #^
 Prints the string in quotes|
   +---+
 |
"#{i+=1}: #{...} -- score: #$."until$.>27 # Counter, string, score (which is stored in"
                             ^        ^         # $.) and keep printing until our^ score is
         +--------|---------------|-- Generation counter
 +----------+---------------|-- Current string
 |                          +-- Score of current string #(interpolates biggerthe than`$.` 27variable)
 |   
 #{$.,s=(0..q).map{...}.map{..max;q=99;s.join}.max;q=99;       # Generate the score and the& string.
            ^        ^  ^   ^    ^   ^    ^
   +--------|--|---|----|---|----|------- Store the score; this variable makes
 # IE create a number ( (0..q).map )
    |  |   |    |   |    |        string interpolation shorter.
            +--|---|----|---+----|------- `q` automatically takes #care of stringsgenerating
 (generated in the first map),
           |   |    |        |        the string vs randomizing the string.
       # compute the score and pair it with itself
 +---|----|--------|------  Make 100 (or 1 the first time) strings,
                   |    |        |     # (second map), and find thecompute maxtheir elementscore.
                     |    +--------|------- Take the string with the max score.
 +-----------------+             #+------- `q``s` automaticallyis takesstored careas ofan whetherarray
 `s`|
 x=R.map{...};[R.count{...},x] # Compute string and its score, store in array
   ^     ^    ^^       ^
   +-----|----|+-------|------ `R` is 0..27, we use the constant to save chars.
    # should be generated from blank| or from 100
 +--------|------ `max` orders by first element, then second. We clearly want
         |             |       the highest score, so make the #score stringsfirst.
  +-------+-------------|------ Generate the string, store in `x`.
 |                     +------ Count the number of chars that overlap with s'METHINKS.join # `s` is actually an array. Join the chars.'
     |                     |
 |                   # together{|i|x[i]=='METHINKS forIT whatIS weLIKE actuallyA print
WEASEL'[i]}
 (0..q).map{R.map{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]}}
#        ^   ^        ^   ^         ^    ^
#       ^
    +--------|---|+---------|----|------ R is 0..27
#                    +---|---------|----|------ If5% `s`chance isn'tof definedrandomizing,
# or 100% for
                  |   +---------|----|------ or otherwise 5% of the time,
#    |       |     first string.
                  +----|------ generate a random char
#                                  ^    ---+------ Else leave the char be.
#                                  -|
       ---- Sample from alphabet + ' '.
              [*?A..?Z,' '].sample # generate by sampling from chars

.map{|x|[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}
#       ^^
#       |+---------------------------------+---- compute the score
#      Don't +--------------------------------------alter andthe placestring with95% `x`of inthe arraytime

Ruby, 206

R=0..27
q=0
i=-1
puts"#{i+=1}: #{$.,s=(0..q).map{R.map{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]}}.map{|x|[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}.max;q=99;s.join} -- score: #$."until$.>27
puts"...."                                    # Prints the string in quotes
    "#{i+=1}: #{...} -- score: #$."until$.>27 # Counter, string, score (which is stored in
                                              # $.) and keep printing until our score is
                                              # bigger than 27
    
$.,s=(0..q).map{...}.map{...}.max;q=99;       # Generate the score and the string.
                                              # IE create a number ( (0..q).map )
                                              # of strings (generated in the first map),
                                              # compute the score and pair it with itself
                                              # (second map), and find the max element.
                                              # `q` automatically takes care of whether `s`
                                              # should be generated from blank or from 100
                                              # strings.
                                       s.join # `s` is actually an array. Join the chars
                                              # together for what we actually print

 (0..q).map{R.map{|j|!s||rand<0.05?...: s[j]}}
#           ^        ^   ^         ^    ^
#           +--------|---|---------|----|------ R is 0..27
#                    +---|---------|----|------ If `s` isn't defined,
#                        +---------|----|------ or otherwise 5% of the time,
#                                  +----|------ generate a random char
#                                  ^    +------ Else leave the char be.
#                                  |
                           [*?A..?Z,' '].sample # generate by sampling from chars

.map{|x|[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}
#       ^^
#       |+------------------------------------- compute the score
#       +-------------------------------------- and place with `x` in array

Ruby, 206 200

R=0..27
q=0
i=-1
puts"#{i+=1}: #{$.,s=(0..q).map{x=R.map{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]};[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}.max;q=99;s.join} -- score: #$."until$.>27
puts"..."until$.>27 # Prints the string in quotes until we reach the desired score
     ^
     |
 +---+
 |
"#{i+=1}: #{...} -- score: #$."
   ^        ^               ^  
   +--------|---------------|-- Generation counter
 +----------+---------------|-- Current string
 |                          +-- Score of current string (interpolates the `$.` variable)
 |   
 #{$.,s=(0..q).map{...}.max;q=99;s.join} # Generate the score & string
   ^        ^  ^   ^    ^   ^    ^
   +--------|--|---|----|---|----|------- Store the score; this variable makes
            |  |   |    |   |    |        string interpolation shorter.
            +--|---|----|---+----|------- `q` automatically takes care of generating
               |   |    |        |        the string vs randomizing the string.
               +---|----|--------|------  Make 100 (or 1 the first time) strings,
                   |    |        |        and compute their score.
                   |    +--------|------- Take the string with the max score.
 +-----------------+             +------- `s` is stored as an array
 |
 x=R.map{...};[R.count{...},x] # Compute string and its score, store in array
   ^     ^    ^^       ^
   +-----|----|+-------|------ `R` is 0..27, we use the constant to save chars.
         |    +--------|------ `max` orders by first element, then second. We clearly want
         |             |       the highest score, so make the score first.
 +-------+-------------|------ Generate the string, store in `x`.
 |                     +------ Count the number of chars that overlap with 'METHINKS...'
 |                     |
 |                    {|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]}
{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]}
    ^   ^         ^             ^       ^
    +---+---------|-------------|-------|---- 5% chance of randomizing, or 100% for
                  |             |       |     first string.
                  +-------------+-------|---- Sample from alphabet + ' '.
                                        +---- Don't alter the string 95% of the time
Source Link
Justin
  • 21.3k
  • 9
  • 67
  • 114

Ruby, 206

R=0..27
q=0
i=-1
puts"#{i+=1}: #{$.,s=(0..q).map{R.map{|j|!s||rand<0.05?[*?A..?Z,' '].sample: s[j]}}.map{|x|[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}.max;q=99;s.join} -- score: #$."until$.>27

All the work is done in the 4th line:

puts"...."                                    # Prints the string in quotes
    "#{i+=1}: #{...} -- score: #$."until$.>27 # Counter, string, score (which is stored in
                                              # $.) and keep printing until our score is
                                              # bigger than 27
    
$.,s=(0..q).map{...}.map{...}.max;q=99;       # Generate the score and the string.
                                              # IE create a number ( (0..q).map )
                                              # of strings (generated in the first map),
                                              # compute the score and pair it with itself
                                              # (second map), and find the max element.
                                              # `q` automatically takes care of whether `s`
                                              # should be generated from blank or from 100
                                              # strings.
                                       s.join # `s` is actually an array. Join the chars
                                              # together for what we actually print

 (0..q).map{R.map{|j|!s||rand<0.05?...: s[j]}}
#           ^        ^   ^         ^    ^
#           +--------|---|---------|----|------ R is 0..27
#                    +---|---------|----|------ If `s` isn't defined,
#                        +---------|----|------ or otherwise 5% of the time,
#                                  +----|------ generate a random char
#                                  ^    +------ Else leave the char be.
#                                  |
                           [*?A..?Z,' '].sample # generate by sampling from chars

.map{|x|[R.count{|i|x[i]=='METHINKS IT IS LIKE A WEASEL'[i]},x]}
#       ^^
#       |+------------------------------------- compute the score
#       +-------------------------------------- and place with `x` in array