
PM of Singapore Shares His C++ Sudoku Solver Source Code - doppp
https://drive.google.com/folderview?id=0B2G2LjIu7WbdfjhaUmVzc1lCR2hUdk5fZllCOHdtbFItbU5qYzdqZGVxdmlnRkJyYVQ4VU0&usp=sharing&usp=sharing&urp=https://drive.google.com/folderview?id%3D0B2G2LjIu7W#list
======
mmahemoff
Context from his speech: [http://www.pmo.gov.sg/mediacentre/transcript-speech-
prime-mi...](http://www.pmo.gov.sg/mediacentre/transcript-speech-prime-
minister-lee-hsien-loong-founders-forum-smart-nation-singapore)

Previously on HN:
[https://news.ycombinator.com/item?id=9419035](https://news.ycombinator.com/item?id=9419035)

------
nothrabannosir
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
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](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.

~~~
mikedmiked
Controls on speech were necessary for Singapore to move from a third world
country to a first world one in just 30 years

~~~
muglug
Interesting theory. Do you have any evidence whatsoever to support that
assertion?

~~~
tootie
It worked for South Korea and Taiwan. They were both ruled by strongmen on the
fence between dictatorship and autoritarian democracy. Park Chung-hee
industrialized S Korea in 20 years by ruling with a rubber-stamp parliament
and a disregard for human rights. The country emerged stronger and gave way to
a true democracy with 20 years of his death.

~~~
emodendroket
The trouble is that that doesn't prove that similar increases in prosperity
could not have occurred without widespread human rights abuses.

~~~
xbmcuser

       USA is built on human Rights abuses first of native Americans then African Americans today you might talk about how free speech got USa where it is. But that is not true is it if you look at your history same is for most of the western world.
       Today US consumerism is built on human rights abuses of people in other countries but free speech whatever. Forget people of other countries many of your local goverments and corporations are being run on human rights abuses of people of color and Hispanics. Either by incarceration or being used almost like slave labor in your large farms.

------
doppp
I've put it on GitHub in case this is ever removed. :)

[https://github.com/Doppp/LHL-Sudoku-Solver](https://github.com/Doppp/LHL-
Sudoku-Solver)

~~~
michael_h
Code without a license on it is not public domain. In this instance, he
probably won't care, but it's a bad habit to get into.

------
Theodores
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.

~~~
DanBC
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).

~~~
eru
Angela Merkel has a PhD in physical chemistry.

------
blakecaldwell
Can someone (less lazy than me) try running it against this puzzle?

[http://www.telegraph.co.uk/news/science/science-
news/9359579...](http://www.telegraph.co.uk/news/science/science-
news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html)

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](https://github.com/wblakecaldwell/c-sudoku-solver)

~~~
pegler
[https://gist.github.com/pegler/752ae571c5d6028ebe72](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

------
kenrick95
Remarkable. He uses lots of bitwise operations in the application.

~~~
efaref
He's using integers as sets of (small) integers,

    
    
        int Possibles = Block[BlockIndex] & Row[RowIndex] & Col[ColIndex];
        while (Possibles) {
            int valbit = Possibles & (-Possibles); // Lowest 1 bit in Possibles
            Possibles &= ~valbit;
            Entry[Square] = valbit;
            Block[BlockIndex] &= ~valbit;
            Row[RowIndex] &= ~valbit;
            Col[ColIndex] &= ~valbit;
    

is more generally:

    
    
        set<int> possibles = block[block_index].intersect(row[row_index]).intersect(col[col_index]);
        while (!possibles.is_empty())
        {
            int value = possibles.pop();
            entry[square] = value;
            block[block_index].remove(value);
            row[row_index].remove(value);
            col[col_index].remove(value);

------
edpichler
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
Looks more like C than idiomatic C++ at a first glance.

~~~
experimental-
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!)

~~~
unscaled
And the single-line comments, if not using a C99-compliant compiler. I
wouldn't be surprised if he was using Visual C++ and had to change the
extension to support a comment syntax that doesn't make you want to strangle
yourself.

------
Lrigikithumer
Capitalised identifiers? Global variables? Inconsistent indentation? PM of
Singapore needs a better style guide.

------
dragonbonheur
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
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.

~~~
dragonbonheur
He would, sure. But does anyone care about the thousands that have been
dismissed due to their age?

------
amelius
What strategy is this solver using? Is it a brute-force approach? Are there
heuristics used?

------
vasilipupkin
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
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++.

------
hiby007
Cool PM to have :)

------
lugus35
Buffer overflow on line 83. Input length is not checked but it must not be
more than 79 characters long.

~~~
lugus35
Sudoku2.cpp: In function 'void PrintStats()': Sudoku2.cpp:98:12: warning:
unused variable 'j' [-Wunused-variable] int i, j, S; ^

------
yincrash
Is there proof that this is his?

~~~
codeulike
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
Short of getting whipped, even the PM will learn to code. <sarcasm>

------
Spooky23
Meanwhile, in the U.S. a president barely has the ability to access a computer
due to the ridiculous regulations surrounding them.

~~~
peter303
Can any of the 20-some candidates for 2016 code? (Not that it is important.)

------
zedadex
What a fascinating world this will be.

------
rustyrubygone
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
Globals and, more serious offense, braces on newline!

------
rikf
This is awesome and I don't wanna hate but the question has to be asked why
c++?

There are tens more languages that are more modern and "aspirational"

------
whizzkid
Little bit off topic but, Google Drive is giving me 500 error.

It was long time ago I read that Google was directly rejecting the CVs that
have typos in them which I find it annoying and not nice. And today I see this
title typo in their error page which is totally fine and a mistake that i also
make sometimes (leaving shift button little early while pressing 1)

<title>Error 500 (Server Error)!!1</title>

"For Google — a company that sees 50,000 résumés a week — the typo is one of
five résumé mistakes that will immediately land yours in the "no" pile." [0]

[http://www.businessinsider.com/google-resume-
mistake-2014-9?...](http://www.businessinsider.com/google-resume-
mistake-2014-9?IR=T)

Human beings will make mistakes Google, there is no need to extremely force it
to otherwise.

~~~
schrodinger
I would suspect that was intentional.

~~~
whizzkid
hmm, when i think about it now, maybe it was!!1

~~~
balabaster
Occasionally you'll also come across someone who beats the joke to death just
to make their point!!!1111OneOneEleven

