Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
Last active January 12, 2017 21:08
Show Gist options
  • Save davidallsopp/00579cafac67a16aa536f787ef20b540 to your computer and use it in GitHub Desktop.
Save davidallsopp/00579cafac67a16aa536f787ef20b540 to your computer and use it in GitHub Desktop.
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]]
@davidallsopp
Copy link
Author

davidallsopp commented Jan 12, 2017

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]

@davidallsopp
Copy link
Author

davidallsopp commented Jan 12, 2017

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