Skip to content

Instantly share code, notes, and snippets.

@nick-paul
Created December 10, 2016 17:42
Show Gist options
  • Save nick-paul/469a268a181a6d1cedf6820418bd4989 to your computer and use it in GitHub Desktop.
Save nick-paul/469a268a181a6d1cedf6820418bd4989 to your computer and use it in GitHub Desktop.
Two Wrongs Sometimes Equal a Right
NB. Nicholas Paul
NB. 9 Dec 2016
NB. Suppose every letter below represents a distinct positive
NB. integer, non of which are zero. What is a possible value
NB. for each letter?
NB.
NB. W R O N G
NB. + W R O N G
NB. -----------
NB. R I G H T
NB. Table of all permutations
NB. For example,
NB. tap 3
NB. 0 1 2
NB. 0 2 1
NB. 1 0 2
NB. 1 2 0
NB. 2 0 1
NB. 2 1 0
tap=: i.@! A. i.
NB. Test function
NB. Return true if 2* the first 5 numbers (w,r,o,n,g) is
NB. equal to the 1,5,4,6,7 (r,i,g,h,t) number
NB.
NB. (( (2&*) Multiply By 2
NB. @: (10&#.) List to num: 1 2 3 => 123
NB. @: (5&{.) Let w,r,o,n,g be the first 5 numbers
NB. ) = ( Test if equal to ...
NB. (10&#.) List to num: 1 2 3 => 123
NB. @: (1 5 4 6 7 &{) Indices for r,i,g,h,t
NB. )) "1 Rank 1 so we can apply it to lists in a table
NB.
two_wrongs =: (((2&*) @: (10&#.)@:(5&{.)) = ((10&#.) @: (1 5 4 6 7 &{))) "1
NB. Filter using the test function on all combinations
NB. 1 + (tap 9) generates permutations of the list (1 2 3 4 5 6 7 8 9)
NB. two_wrongs # ] filters all lists that make the statement true
NB. 8 {."1 takes the first 8 elements of each row in the list
res =: 8 {."1 ( two_wrongs # ] ) 1 + (tap 9)
NB. Pretty Print
,./ ((< & ((5&{.,:5&{.)) ,: (<&(1 5 4 6 7 &{)))) "1 [ res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment