top | item 9485237

PM of Singapore Shares His C++ Sudoku Solver Source Code

209 points| doppp | 11 years ago |drive.google.com | reply

150 comments

order
[+] nothrabannosir|11 years ago|reply
I like this one a lot:

    Entry[Square] = BLANK; // Could be moved out of the loop
Maybe I'm reading too much into it, but this type of thinking makes me smile. The guilty perfectionism, "this can be improved."

Were I from Singapore, I'd be happy to see it in the person in charge of my country.

[+] arianvanp|11 years ago|reply
Please remember that Signapore has no free speech. I am kind of angry we're cirlclejerking about a PM whose a criminal according to my view http://en.wikipedia.org/wiki/Human_rights_in_Singapore

Can we please just ignore this dictator and be amazed about code of other people instead?

It just feels wrong.... I think freedom of speech is a very very important part of the hacker community. I think no sane person would want this person in charge of his country. Coder or not.

[+] steinnes|11 years ago|reply
You would be happy to have a person who mixes tabs and spaces as your PM? ... mixes tabs and spaces. Argh.
[+] personjerry|11 years ago|reply
Ok I believe the phrasing of this comment makes its point a little unclear. Some people are saying how this demonstration doesn't guarantee that the president is a good one. This is true, but I believe the original comment here is merely saying that it is better to have a president WITH this characteristic than without.
[+] dagw|11 years ago|reply
I'd be happy to see it in the person in charge of my country.

Would you be equally happy to see Herman Cain as President of the US? He also has Masters in CS and spent the early parts of his career as a programmer.

[+] casion|11 years ago|reply
While I find the code to be interesting, I wouldn't exactly claim to want this person as my PM.

Great, he used to program and displayed a handful of positive character qualities in doing so. A lot more than that is required to be an effective leader of a country.

[+] Theodores|11 years ago|reply
Clinton could play the Saxophone, after that I am struggling to think of any politicians able to demonstrate a useful skill.

Programming might not be that hard to pick up if you had done it before, couldn't this code have been written on a 25 year old computer to compile with a Borland era compiler?

Because the wider population don't understand programming and see it as magic, I fully expect his popularity to do well, he is practically a hacker in their eyes, able to conjure up magic and demonstrate skill.

[+] roel_v|11 years ago|reply
Vladimir Putin is a bona fide Judo expert. I guess it's debatable how 'useful' that is - just like playing the sax.

"Because the wider population don't understand programming and see it as magic, I fully expect his popularity to do well, he is practically a hacker in their eyes, able to conjure up magic and demonstrate skill."

The circle jerking in this thread rubs me the wrong way - "this guy must be awesome, he can pass a 1st year 'introduction to computer programming' class!". Must be the cognitive dissonance - there are people still saying Hans Reiser was surely framed, because how could a programmer be anything but God's gift to mankind?

[+] DanBC|11 years ago|reply
Margeret Thatcher was a chemist. She's variously credited with inventing "raspberry ripple" icecream, or "Mr Whippy" soft-serve style icecream. (The last one is definitely not true, it was invented a long time before she started working).
[+] k-mcgrady|11 years ago|reply
>> "Because the wider population don't understand programming and see it as magic, I fully expect his popularity to do well, he is practically a hacker in their eyes, able to conjure up magic and demonstrate skill."

The general public don't think of you as some sort of magic hacker because you code for a living, sorry.

[+] trequartista|11 years ago|reply
Manmohan Singh (former PM of India) was an economist par excellence. As the finance minister, he was responsible for the liberalization of the Indian economy and arguably responsible for the economic boom in the country today. But his tenure as a Prime Minister is largely remembered for his missteps and his general nonchalance to everything that happened around him. His legacy was tarnished and now he is just remembered as a puppet PM.

So yeah, extra curricular skills and fancy degrees and academic credentials may not translate into a good leader of the country.

[+] userbinator|11 years ago|reply
demonstrate a useful skill

Speaking in public (especially to very large crowds) and not turning into a nervous wreck is not a useful skill?

[+] kiiski|11 years ago|reply
Out of curiosity I checked what kind of degrees Finnish presidents have had. There was (not in any particular order):

  3 law, doctor
  2 law, master
  2 law, candidate (~bachelor)
  1 agriculture, doctor
  1 social sciences, doctor
  1 none, farmer + bank manager
  1 military, field marshal 
  1 teacher (primary school)
[+] emodendroket|11 years ago|reply
Many politicians are former lawyers and it seems to me like passing a bar exam is a pretty useful skill (more useful than playing the sax, even) if you're going to endeavor to make laws.
[+] blakecaldwell|11 years ago|reply
Can someone (less lazy than me) try running it against this puzzle?

http://www.telegraph.co.uk/news/science/science-news/9359579...

It's been over a year since I've thought about solving sudoku, but here's how I solved it then:

https://github.com/wblakecaldwell/c-sudoku-solver

[+] pegler|11 years ago|reply
https://gist.github.com/pegler/752ae571c5d6028ebe72

Row[1] : 800000000

Row[2] : 003600000

Row[3] : 070090200

Row[4] : 050007000

Row[5] : 000045700

Row[6] : 000100030

Row[7] : 001000068

Row[8] : 008500010

Row[9] : 090000400

8-- --- ---

--3 6-- ---

-7- -9- 2--

-5- --7 ---

--- -45 7--

--- 1-- -3-

--1 --- -68

--8 5-- -1-

-9- --- 4--

812 753 649

943 682 175

675 491 283

154 237 896

369 845 721

287 169 534

521 974 368

438 526 917

796 318 452

Level Counts:

(8, 7): 1 (7, 7): 2 (7, 2): 3 (7, 5): 5 (2, 2): 7 (6, 2): 11

(1, 2): 20 (1, 7): 33 (6, 7): 51 (4, 7): 62 (2, 7): 86 (5, 2): 114

(8, 2): 160 (5, 3): 195 (5, 8): 217 (5, 9): 260 (5, 1): 317 (5, 4): 371

(4, 8): 398 (4, 9): 456 (6, 9): 473 (6, 1): 440 (6, 3): 429 (6, 5): 407

(6, 6): 392 (4, 3): 364 (4, 1): 360 (7, 6): 330 (7, 1): 302 (2, 1): 264

(7, 4): 229 (8, 5): 200 (4, 5): 164 (1, 5): 134 (2, 5): 115 (2, 6): 92

(1, 6): 71 (3, 6): 63 (4, 4): 47 (1, 3): 41 (8, 6): 37 (3, 4): 28

(3, 1): 20 (8, 9): 15 (2, 9): 12 (2, 8): 7 (1, 8): 4 (3, 9): 4

(3, 8): 1 (8, 1): 1 (1, 9): 1 (9, 1): 1 (1, 4): 1 (9, 3): 1

(9, 4): 1 (9, 5): 1 (9, 6): 1 (3, 3): 1 (9, 8): 1 (9, 9): 1

Count = 7826

Total Count = 13080

[+] taeric|11 years ago|reply
Decided to try this puzzle against an implementation of the DLX algorithm I put up a while back.[1] Looks at 2,825 board states, in total. If I stop it at the solution, only looks at 1,489 states.

[1] http://taeric.github.io/Sudoku.html

[+] kenrick95|11 years ago|reply
Remarkable. He uses lots of bitwise operations in the application.
[+] edpichler|11 years ago|reply
For people who are criticizing this, try to find something good on it: it's just a curiosity about a powerful man of a well succeed country, that can inspire someone.

This also can be considered as a positive fact for me, once in my country, it's common to find politicians that even doesn't have any degree (including a very recent president).

[+] xwintermutex|11 years ago|reply
Looks more like C than idiomatic C++ at a first glance.
[+] experimental-|11 years ago|reply
A very quick glance suggests to me that the only thing C++ specific there is the filename extension (although a C compiler would do a more thorough check than I do; and faster, even!)
[+] tcholas|11 years ago|reply
Was going to comment exactly the same thing
[+] Lrigikithumer|11 years ago|reply
Capitalised identifiers? Global variables? Inconsistent indentation? PM of Singapore needs a better style guide.
[+] dragonbonheur|11 years ago|reply
Now, could someone with his age and experience get a job in Silicon Valley? The answer is precisely what is wrong in today's tech firms. Ageism must die.
[+] unfunco|11 years ago|reply
Yes. Absolutely.

Experience managing the resources of a population of 5.5 million, experience on the world stage, programming and problem solving ability, his alma mater include Cambridge and Harvard, and military service, able to maintain close relationships with important people (including the President of the United States.)

He would absolutely get a job in Silicon Valley.

[+] Lrigikithumer|11 years ago|reply
People with agendas making aggressive comments on barely related subjects to incite arguments which accomplish nothing are precisely what is wrong with todays online discussion.
[+] eru|11 years ago|reply
He has a lot of experience in management positions, and was the Senior Wrangler in Cambridge. I bet you dollars to donuts he could get a job in SV.
[+] amelius|11 years ago|reply
What strategy is this solver using? Is it a brute-force approach? Are there heuristics used?
[+] vasilipupkin|11 years ago|reply
I think it's neat and could draw more people to CS, but yeah, people, let's not get overexcited over this. Knowing how to write a Sudoku solver in C++ says nothing, positive or negative about him as a prime minister
[+] balabaster|11 years ago|reply
I've been a (reasonably successful) programmer for 20+ years and I don't even know how to play Sudoku, nor does learning interest me... not being able to write a Sudoku solver bears no resemblance on ones capabilities. This goes the same as being able to write one. So I agree, getting excited about this seems somewhat... irrelevant. Being able to write any kind of computer program doesn't demonstrate being capable of effective leadership. Having a compelling vision for one's country, understanding the issues you will need to overcome to get there and inspiring the people to help you take it there are far more relevant to me.

Knowing a bit of C++ and understanding an algorithm to make it solve a puzzle is largely useless in this context. Understanding a bit about technology at (close to) the lowest level, doesn't signify an understanding of how the internet works, how encryption systems work, how network security or the communications infrastructure works. Without an understanding of these in today's world of the communications and the internet, you may as well not understand how to program at all. Where we see technology failing the people (certainly in North America) are because legislators are largely incompetent in this context and seek to protect corporate interests over the needs of the people. I would say, being a systems administrator or systems analyst would make you far more capable of understanding the implications of technical bills and regulations than understanding a bit of C++.

[+] lugus35|11 years ago|reply
Buffer overflow on line 83. Input length is not checked but it must not be more than 79 characters long.
[+] lugus35|11 years ago|reply
Sudoku2.cpp: In function 'void PrintStats()': Sudoku2.cpp:98:12: warning: unused variable 'j' [-Wunused-variable] int i, j, S; ^
[+] yincrash|11 years ago|reply
Is there proof that this is his?
[+] codeulike|11 years ago|reply
He was the top of his year in Mathematics at Cambridge, which is pretty damn hardcore. So its definitely feasible that he could bang out some C++ code.
[+] noipv4|11 years ago|reply
Short of getting whipped, even the PM will learn to code. <sarcasm>
[+] Spooky23|11 years ago|reply
Meanwhile, in the U.S. a president barely has the ability to access a computer due to the ridiculous regulations surrounding them.
[+] peter303|11 years ago|reply
Can any of the 20-some candidates for 2016 code? (Not that it is important.)
[+] zedadex|11 years ago|reply
What a fascinating world this will be.
[+] rustyrubygone|11 years ago|reply
Politicians and important folk recognise the power and awesomeness of C++, and don't waste time on Ruby, Go, Rust or other time waster languages!
[+] Keyframe|11 years ago|reply
Globals and, more serious offense, braces on newline!