{"id":1107,"date":"2011-12-27T22:22:41","date_gmt":"2011-12-28T05:22:41","guid":{"rendered":"http:\/\/thesmithfam.org\/blog\/?p=1107"},"modified":"2019-08-12T07:15:07","modified_gmt":"2019-08-12T13:15:07","slug":"fun-python-solution-to-euler-problem-79","status":"publish","type":"post","link":"https:\/\/thesmithfam.org\/blog\/2011\/12\/27\/fun-python-solution-to-euler-problem-79\/","title":{"rendered":"Fun Python Solution to Euler Problem 79"},"content":{"rendered":"<p>Nothing says Merry Christmas like Project Euler.<\/p>\n<p>Here&#8217;s a nifty solution to <a href=\"http:\/\/projecteuler.net\/problem=79\">Problem 79<\/a> that uses Python and Graphviz.<\/p>\n<p>The problem is to identify a user&#8217;s password given a bunch of successful logins (taken from some kind of nefarious keylogger&#8211;those crafty devils). The \u00c3\u00a7atch is that each login is actually a <b>subset<\/b> of the actual password. Of course, they give you <a href=\"http:\/\/projecteuler.net\/project\/keylog.txt\">a sample of 50 successful logins<\/a>.<\/p>\n<p>So I decided to whip up some Python code to ingest each login and store a list of digit precedence. Then, the code prints out a <a href=\"http:\/\/www.graphviz.org\/Gallery.php\">Dot file<\/a> which we turn into an image using the &#8220;dot&#8221; command. I ran this on Mac OS X, but it should work just fine on any Linux box (hint: install the &#8220;graphviz&#8221; package).<\/p>\n<p>Here&#8217;s the Python code:<\/p>\n<p><b>problem79.py:<\/b><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndigit_precendence = dict()\r\n\r\nfor line in &#x5B;line.strip() for line in open(&quot;.\/keylog.txt&quot;)]:\r\n    for index, char in enumerate(line):\r\n        digits = digit_precendence.setdefault(int(char), set())\r\n        if index &lt; len(line)-1:\r\n            digits.add(int(line&#x5B;index+1]))\r\n\r\nprint &quot;digraph problem79 {&quot;\r\nfor (digit, subsequent_digits) in digit_precendence.iteritems():\r\n    for subsequent_digit in subsequent_digits:\r\n        print &quot;   %d -&gt; %d;&quot; % (digit, subsequent_digit)\r\nprint &quot;}&quot;\r\n<\/pre>\n<p>Save that as <b>problem79.py<\/b>, download <b>keylog.txt<\/b> into the same folder, and run the program like this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">python problem79.py | dot -Tpng &gt; problem79.png<\/pre>\n<p>Then view <b>problem79.png<\/b>, and voila! Graphviz just put the answer right in front of your nose: <b>73162890<\/b>.<\/p>\n<p><img decoding=\"async\" src=\"\/images\/problem79.png\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nothing says Merry Christmas like Project Euler. Here&#8217;s a nifty solution to Problem 79 that uses Python and Graphviz. The problem is to identify a user&#8217;s password given a bunch of successful logins (taken from some kind of nefarious keylogger&#8211;those crafty devils). The \u00c3\u00a7atch is that each login is actually a subset of the actual [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1107","post","type-post","status-publish","format-standard","hentry","category-code-and-cruft"],"_links":{"self":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/1107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/comments?post=1107"}],"version-history":[{"count":21,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/1107\/revisions"}],"predecessor-version":[{"id":1508,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/1107\/revisions\/1508"}],"wp:attachment":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/media?parent=1107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/categories?post=1107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/tags?post=1107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}