Archive for July, 2006

Java Sudoku Solver in 6 lines

Monday, July 24th, 2006

After playing around with writing Sudoku Solvers in the past, I was interested to see this page which details a number of tiny sudoku solvers in a variety of languages such as Perl, Ruby, etc.

They have a few basic rules – no line over 80 bytes, etc, etc. And the examples take their input as a string of 81 digits, which represents the 9 rows of 9 digits, with “0″ being a blank square.

As I apparently have way too much time on my hands ;) , I got up the motivation to convert one of these examples into Java. The results?

  • The end program is around twice as many lines as the Perl original, and
  • Yup – you can make Java programs as unreadable as anything else out there! ;)

There may well be shorter possibilities out there – I didn’t put in a great deal of effort past converting the Perl original. But, for the record:

import java.util.*;public class S{static char[] A;static void R(){int i,j;for(i=
0;i<81;i++){if(A[i]!='0')continue;HashMap h=new HashMap();for(j=0;j<81;j++){h.
put(j/9==i/9||j%9==i%9||(j/27==i/27)&&((j%9/3)==(i%9/3))?""+A[j]:"0","1");}for(j
=1;j<=9;j++){if(h.get(""+j)==null){A[i]=(char)('0'+j);R();}}A[i]='0';return;}for
(i=0;i<81;i++){System.out.print(A[i]);}System.out.println();System.exit(0);}
public static void main(String[] a){A=a[0].toCharArray();R();}}

Assuming you have an appropriate classpath, you can run it with a line like:

java S 006070800000000000078601520030405010400000002090302060052103690000000000003020100

This code was converted from the excellent Perl example at:

http://www.ecclestoad.co.uk/blog/2005/06/02/sudoku_solver_in_three_lines_explained.html

Prius Hacking

Friday, July 14th, 2006

Came across this link on CNN about a guy going into business selling a kit to “hack” or modify your Prius.

The kit apparently replaces the NiMh battery with a (greater capacity) Lithium Ion one, and also allows the car to be plugged in to a power point to charge it up. (For the Prius newbie’s, this is not the way they normally work – Priuses (or Priii? ;) ) charge themselves up when breaking and coasting, or by diverting power from the petrol engine).

Great idea! Some of my car-mad friends with hotted up cars have long delighted in buying modified engine management chips that squeeze more power out of the engines. This guy is modding a car to be more fuel efficient!

Obviously, some of the “green points” are lost if your house gets its electricity from a coal-powered generator plant – whenever you plug the car in to charge it up, you’re still polluting…indirectly. But if you subscribe to one of the green programs on offer, where your electricity provider sources your power from renewable sources like wind, its all good.

Interestingly, the story has Toyota being quite supportive of this kind of thing. Makes sense – as the manufacturer you want to encourage your customers to build a community around your vehicle and take it further.

Captain Planet…as you’ve never seen him before!

Monday, July 3rd, 2006

I have to admit that I never saw the original Captain Planet cartoon series – I gather it was about a superhero with a real environmental streak. Well, a friend put me onto a little video on the Net that has a whole new interpretation… :)

I should warn that its probably not for little kiddies – its only claymation animation, but some of the language is rather… earthy (pardon the pun)!

Enjoy!