Last active
January 12, 2017 21:08
-
-
Save davidallsopp/00579cafac67a16aa536f787ef20b540 to your computer and use it in GitHub Desktop.
Solution in Haskell to puzzle from https://twitter.com/ValdisKrebs/status/819301951972265985
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
let numAndPlace xs ys = length $ (zip xs [0..]) `intersect` (zip ys [0..]) | |
let numNotPlace xs ys = (length $ xs `intersect` ys) - (numAndPlace xs ys) | |
let test xs ys = (numAndPlace xs ys, numNotPlace xs ys) | |
let match xs = map (test xs) [[6,8,2],[6,1,4],[2,0,6],[7,3,8],[8,7,0]] == [(1,0),(0,1),(0,2),(0,0),(0,1)] | |
filter match [[x,y,z] | x<-[0..9], y<-[0..9], z<-[0..9]] |
You can also shorten the code by removing the " - (numAndPlace xs ys)" and adjusting the desired totals to compensate [(1,1),(0,1),(0,2),(0,0),(0,1)], although I think this makes the configuration less clear. This change has therefore been applied to the obfuscated short version only!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above can be shortened somewhat if you are prepared to obfuscate! For example:
import Data.List
let l=length;i=intersect;f x=map(\y->(l$i(zip x[0..])(zip y[0..]),l$i x y))["682","614","206","738","870"]==[(1,1),(0,1),(0,2),(0,0),(0,1)];d=['0'..'9']
filter f[[x,y,z]|x<-d,y<-d,z<-d]