News for hackers. Stuff that matters.
Re:Sudoku solver in 1-line of CoffeeScript (with explanation) by lvh on Wednesday October 10, 2012 @05:53AM (#4635686)
Can we stop calling things like these 1-liners? Most languages can have arbitrary programs reduced to one line. That doesn't mean it's legible or a good idea. It's a concise, possibly even elegant (although that's an opinion) Sudoku solver in Coffeescript. Isn't that good enough?
Re:Sudoku solver in 1-line of CoffeeScript (with explanation) by kjp on Wednesday October 10, 2012 @08:19AM (#4635971)
This is not an one-liner by any means. By this definition the entire linux kernel can probably be compressed into one very long line too.
Re:Sudoku solver in 1-line of CoffeeScript (with explanation) by beatgammit on Wednesday October 10, 2012 @12:27PM (#4637016)
The only sortof unique thing about coffee-script is the lack of semi-colons. They just abused the comma operator though.
Re:Sudoku solver in 1-line of CoffeeScript (with explanation) by power on Wednesday October 10, 2012 @05:43AM (#4635673)
From a quick glance it looks to only check conflicts at the box level, not row and column. This is the first CoffeeScript I've seen, it's nice and concise.
Re:Sudoku solver in 1-line of CoffeeScript (with explanation) by pathikrit on Wednesday October 10, 2012 @06:13AM (#4635719)
`g in [s[x][i], s[i][y], box i]` - the first 2 arguments check in row and column level and third in box level
Re:Sudoku solver in 1-line of CoffeeScript (with explanation) by power on Wednesday October 10, 2012 @07:21AM (#4635850)
Thanks for clarifying. If you've the time, could you tell me how I should read: non_conflicting = (g) -> ![0...9].some (i) -> g in [s[x][i], s[i][y], box i]
Re:Sudoku solver in 1-line of CoffeeScript (with explanation) by pathikrit on Wednesday October 10, 2012 @05:13PM (#4638226)
[0...9].some (i) -> g in [s[x][i], s[i][y], box i] returns true iff it finds some i in [0,9) such that given a guess g, g equals either s[x][i] or s[i][y] or box i or in other words it finds a conflict in either a row or column or box for g assuming it g is at s[x][y] The ! at the start negates it - so the whole method non_conflicting returns true iff g does not conflict with any i.

If Arrays in JavaScript had an "all" reducer it could be written like this:

non_conflicting = (g) -> [0...9].all (i) -> g not in [s[x][i], s[i][y], box i]