[Solution] Facebook Hacker Cup 2012 – Qualification round – Alphabet soup (letters)

Was bored so decided to optimize my solution a little bit more.

You can find the original solution here.

 

$file_content = explode("\n",(fread(fopen("input.txt",'r'), filesize("input.txt")));

$output = fopen("output.txt", 'w');
$hacker = array('h','a','c','k','e','r','u','p');

for ($j = 1; $j <= intval($file_content[0]); $j++){
for ($zzz = 0; $zzz < count($hacker); $zzz++){
$freq[$hacker[$zzz]] = 0;
}
	$word = strtolower(str_replace(" ","",$file_content[$j]));
	$len = strlen($word);
		for ($i=0; $i<$len; $i++) {
		  $letter = $word[$i];
		  if (in_array($letter,$hacker)){
		     if ($letter =="c") {$freq[$letter] +=  0.5;}
		     else {$freq[$letter]++;}
		}
		}
	$stringData = "Case #" . $j . ": " . intval(min($freq)) . "\n";
	fwrite($output, $stringData);
}
fclose($output);

 

Smallest solder I’ve ever done… PSP USB charging mod

I have a first generation PSP (PlayStation Portable) and after it’s charger over-heated and died I started using a variable adapter to charge it. Once while charging, I set the polarity on the adapter the wrong way around and well it burnt out the diode on the PSP circuit board!

So now I had a dead PSP because of a hardware problem. (Hint: I’m more of a software guy!)

After wasting a lot of time on google, I came across this one post on how to charge a first gen PSP via USB. Originally a first gen PSP does not support USB charging. All I had to do was solder a wire from the mini-USB port to the PSP power-port. Easy. Only that after opening up the case, it didn’t look so easy. And this is why:

For comparison, I took the photo next to a 5p coin. Yes that’s a 5 pence coin which is 18mm in diameter! It is the best and the neatest solder I’ve done so far without burning a hole through the circuit board or any other components!

 

[Solution] Facebook Hacker Cup 2012 – Qualifying round – Alphabet Soup

This was by far the easiest problem out of the three.

Here’s my solution, written in PHP. It reads a file called input.txt and outputs the results to output.txt!

UPDATE – a much more optimized solution here.

$filename = "input.txt";
$input = fopen($filename, 'r');
$theData = fread($input, filesize($filename));
fclose($input);

$file_content = explode("\n",$theData);
$test_cases = intval($file_content[0]);
$outputFile = "output.txt";
$output = fopen($outputFile, 'w') or die("can't open file");

for ($j = 1; $j <= $test_cases; $j++){
	$freq = array();
	$word = strtolower(str_replace(" ","",$file_content[$j]));
	$len = strlen($word);
		for ($i=0; $i<$len; $i++) {
		  $letter = $word[$i];
		  if (array_key_exists($letter, $freq)) {
			$freq[$letter]++;
		  } else {
			$freq[$letter] = 1;
		  }
		}
      if (!isset($freq['h']) || !isset($freq['a']) || !isset($freq['c'])
       || !isset($freq['k']) || !isset($freq['e']) || !isset($freq['r'])
       || !isset($freq['u']) || !isset($freq['p'])
       || (intval($freq['c'] == 1)))
                {
		$stringData =  "Case #" . $j . ": 0\n";
		fwrite($output, $stringData);
		}
	else {
		$counting = array();
		$letters = array('h','a','c','k','e','r','u','p');
			for ($z = 0; $z < count($letters); $z++){
			$var_name = "count".$letters[$z];
				if ($var_name == "countc"){
				    $$var_name = $freq[$letters[$z]]/2;
				    $counting[] = intval($$var_name);
				}
				else {
				    $$var_name = $freq[$letters[$z]];
				    $counting[] = intval($$var_name);
				}
			}
	$stringData = "Case #" . $j . ": " . min($counting) . "\n";
	fwrite($output, $stringData);
	}
}

fclose($output);

Download link: alphabet-soup

 

aPcBuild – because its technically not my first!

Technically I built my first PC 6 years ago. It was made from scrap parts from random old PCs I had lying around and any PCs that had been just given to me.

Didn’t have a proper case for it though, so it just sat on my desk with all parts lying bare. It looked something like this…

The specs were something like a dual core 1.8 GHz processor and 512Mb RAM with a 256Mb graphics card. Nothing amazing, I don’t even remember what the exact specs were.

Three years ago, I built my current PC that I’m using now. It was a budget build and total cost was kept under £400! Running Windows 7 Ultimate x64 and I do all photo-editing, web-designing and gaming on it! Not a gaming PC but runs them well.

The case is a Galaxy Black Mid Tower case with blue bubble lighting and clear side available here at ebuyer -> exactly what I wanted, cheap, some lighting and a side panel allowing for simple mods! for £35

Processor is an AMD Athlon II X4 620 -> a quad core 2.6 GHZ processor …great for < £70!

Motherboard is an ECS GeForce6100PM-M2 -> memory support up to 16GB however only 2 DIMM slots but < £60

RAM -> Kingston 4GB (2x2GB) DDR2 CL6 for around £50

A cheap DVD player/writer for £15 from ebuyer.com

Front panel card reader for £10

NVIDIA GeForce GT 440 1GB (DDR5) < £70

HDD1 WD Caviar Blue 500GB – 600 MBps – 7200 rpm < £65

Led strip lighting < £5 ebay!

Cabling for around £10

The DELL screen was given by a friend and the HP screen by another friend.

Mouse and Keyboard were just lying around?

Total cost of a beautiful budget build < £400

Since then I’ve added the following:

HDD2 WD Caviar Blue 1TB – 600MBps – 7200 rpm < £100

Slimline Widescreen Tablet = £60

 

It’s not the most powerful PC but it does the job and does it well! I use it mainly for web development, and all I use for web-dev is a local installation of Wamp Server and Notepad ++ Runs games pretty well on the highest settings, will put up benchmark stats if interested, but I’m not much of a gamer.

This allows me to cross off number 15 of my things to do before I die list.

 

19th May 2011 – my first blood donation …

Nothing special really, Y’all should donate blood!

Click here to find out more about blood donations

I’d wanted to donate blood for a long time, especially after signing up to the Organ Donor list when I got my provisional driving license. I mean that does show that they think people are likely to die while driving and thus ask them to sign up to the donor list.

Anyway, there was a petite woman also donating blood and half way through her donation she fainted. No not because she was scared/nervous, etc., but from what I heard because she was too small and her body couldn’t cope with blood loss.

Imagine if it was somebody like her in an accident? Somebody who’ll need blood immediately. . .

Do something amazing today. Give Blood!

This completed number 41 off my things to do before I die list.

 

Things to do before I die . . . The Bucket List

I made this list a few years ago, since then I’ve added a lot of things to it. I’m making it public so that I really follow through and do it all and of course to write a post about each thing I’ve done so far and end up doing.

Here’s the list:
1. climb everest
2. publish autobiography
3. become a multi-millionaire
4. visit every continent
5. visit both poles
6. collect 365 ties – one for each day
7. release a music album
8. publish a book
9. go sky diving
10. create my website – Completed Feb 2011
11. build a web-service – Completed 1st Jan 2012
12. do charity work for a good few weeks in an unknown place
13. visit grand canyon
14. visit amazon rainforest
15. build a PC – Completed sometime in 2009
16. make a mobile phone app and sell ten thousand copies
17. build a swords and knives collection
18. act in a film/or be an extra
19. find an amazing stranger and tell them my life story from scratch
20. go around a world cruise
21. be on a live tv show
22. visit the space
23. walk the great wall of china
24. visit sahara desert
25. sit in a jury
26. dramatically change someones life
27. open an orphanage or adopt upto 10 kids and change their life
28. have a lot of things to give away in my will
29. go rafting in grand canyon
30. go up in a hot air balloon
31. win big in vegas
32. give a huge donation to charity – anonymously
33. run to the top of statue of liberty
34. go skiing
35. create a family tree
36. run a marathon
37. visit a disaster struck area to help out
38. build a house for homeless people
39. visit the 7 wonders of the world
40. dunk a basketball
41. give blood – Completed 19th May 2011 – regular donor since then
42. find a firefly and put it in a jar
43. go tornado hunting
44. go on an international bike ride
45. own an island
46. own a yacht
47. climb an active volcano
48. must visit italy
49. do deep sea diving/scuba diving
50. get a pilots license
51. learn how to juggle at least three balls!
52. fire a gun
53. fight in MMA
54. be able to do the splits
55. break a world record
56. do the rubic’s cube in less than 15 seconds
57. own a talking parrot
58. Sit on the floor when a perfectly good chair is available in an important meeting
59. watch the launch of a space shuttle live! – be there
60. go bungee jumping
61. adopt an animal from an animal shelter
62. release an internationally recognised computer program
63. build a mirrored ceiling
64. see the northern lights
65. join a bowling league for a season
66. restore a classic car
67. get a PhD
68. open a Swiss bank account
69. Learn to program in Brainfuck and Piet =) for fun
70. create a turing complete programming language