Wikipedia: Philosophy! As many geeks may know most pages (95% i think) on wikipedia eventually lead to Philosophy like this:

Click on the first non-italic or in parentheses link that is to another normal article: (i.e. not File: or Special:, but things like Wikipedia: are OK) and repeat on that one until you hit philosophy.

The script must:


Take a first page as input
Print the name of each article it gets to
And print how many articles it took to get to Philosophy, and if it didn't say so.


You start with 1000 points, and lose one for each character in the code, Bonus points for:

Detecting looping articles and stopping: +50

Detecting looping articles and requesting if the user should go to the next link on the article: +170

Allowing for a default on the former check as a command-line arg or similar: +140

Highest score wins.
Ruby, 1000 - 303 299 337 - 50 373 - 170 382 - 170 - 140 379 - 170 - 140 chars = 697 701 713 797 928 931

I'm sure there are many improvements to be made.

(This requires Nokogiri)

require'open-uri'
require'nokogiri'
x="/wiki/"+gets.chomp
r=[n=i=0]
until x=~/\/Philosophy/
d=Nokogiri.HTML open"http://en.wikipedia.org#{x}"
x=d.css('p a').select{|a|t=a.xpath('preceding::text()').map(&amp;:text)*'';t.count('(')==t.count(')')&amp;&amp;a.attr('href')=~/^.wiki[^:]+$/}[i].attr'href'
i=0
puts r.index(x)?"#{$&gt;&lt;&lt;'i=';i=($*[0]||gets).to_i;''}": r.push(x)[-1][6..-1]
n+=1
end
p n


Example run:

c:\a\ruby&gt;wikipedia_crawl_philosophy
Latin (note: this is my input)
Classical_antiquity
History
Umbrella_term
Terminology
Word
Linguistics
Science
Knowledge
Fact
Proof_(truth)
Argument
Logic
Reasoning
Consciousness
Quality_(philosophy)
Property_(philosophy)
Modern_philosophy
Philosophy
18


Sample one where I had to go to a different link

c:\a\ruby&gt;wikipedia_crawl_philosophy
Snow
Precipitation_(meteorology)
Meteorology
Atmospheric_physics
Synoptic_scale_meteorology
i=2 // I put the 0-indexed number of the link I wanted to go to (so, the third link)

Weather
Atmosphere
Gas
State_of_matter#The_four_fundamental_states
Physics
Natural_science
Sciences
Knowledge
Fact
Proof_(truth)
Argument
Logic
Reasoning
Consciousness
Quality_(philosophy)
Property_(philosophy)
Modern_philosophy
Philosophy
25


Tricks I used:


I used the selector p a to only get non-italic links, because all links in the actual article that are not italics are always in paragraph elements in Wikipedia.