Skip to content

Instantly share code, notes, and snippets.

@edbond
Created November 27, 2010 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edbond/718088 to your computer and use it in GitHub Desktop.
Save edbond/718088 to your computer and use it in GitHub Desktop.
solution to euler 18 problem
problem18> (solve)
14 [4 62 98 27 23 9 70 98 73 93 38 53 60 4 23] [63 66 4 68 89 53 67 30 73 16 69 87 40 31]
13 (125 164 102 95 112 123 165 128 166 109 122 147 100 54) [91 71 52 38 17 14 91 43 58 50 27 29 48]
12 (255 235 154 150 140 179 256 209 224 172 174 176 148) [70 11 33 28 77 73 17 78 39 68 17 57]
11 (325 246 187 178 256 329 273 302 263 242 193 233) [53 71 44 65 25 43 91 52 97 51 14]
10 (378 317 231 321 354 372 393 354 360 293 247) [41 48 72 33 47 32 37 16 94 29]
9 (419 365 393 387 419 425 430 376 454 322) [41 41 26 56 83 40 80 70 33]
8 (460 434 419 475 508 470 510 524 487) [99 65 4 28 6 16 70 92]
7 (559 499 479 536 514 526 594 616) [88 2 77 73 7 63 67]
6 (647 501 613 609 533 657 683) [19 1 23 75 3 34]
5 (666 614 636 684 660 717) [20 4 82 47 65]
4 (686 640 766 731 782) [18 35 87 10]
3 (704 801 853 792) [17 47 82]
2 (818 900 935) [95 64]
1 (995 999) [75]
0 (1074) nil
1074
;; By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
;; 3
;; 7 4
;; 2 4 6
;; 8 5 9 3
;; That is, 3 + 7 + 4 + 9 = 23.
;; Find the maximum total from top to bottom of the triangle below:
;; 75
;; 95 64
;; 17 47 82
;; 18 35 87 10
;; 20 04 82 47 65
;; 19 01 23 75 03 34
;; 88 02 77 73 07 63 67
;; 99 65 04 28 06 16 70 92
;; 41 41 26 56 83 40 80 70 33
;; 41 48 72 33 47 32 37 16 94 29
;; 53 71 44 65 25 43 91 52 97 51 14
;; 70 11 33 28 77 73 17 78 39 68 17 57
;; 91 71 52 38 17 14 91 43 58 50 27 29 48
;; 63 66 04 68 89 53 67 30 73 16 69 87 40 31
;; 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
;; NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
(ns problem18
(:require [clojure.zip :as z]))
;; 3
;; 7 4
;; 2 4 6
;; 8 5 9 3
;; (def test-tree
;; [[3]
;; [7 4]
;; [2 4 6]
;; [8 5 9 3]])
;; (def test-tree
;; [[5]
;; [9 6]
;; [4 6 9]
;; [0 7 1 9]])
(def test-tree
[[75]
[95 64]
[ 17 47 82]
[ 18 35 87 10]
[ 20 4 82 47 65]
[ 19 1 23 75 03 34]
[ 88 2 77 73 07 63 67]
[ 99 65 04 28 06 16 70 92]
[41 41 26 56 83 40 80 70 33]
[41 48 72 33 47 32 37 16 94 29]
[53 71 44 65 25 43 91 52 97 51 14]
[70 11 33 28 77 73 17 78 39 68 17 57]
[91 71 52 38 17 14 91 43 58 50 27 29 48]
[63 66 04 68 89 53 67 30 73 16 69 87 40 31]
[04 62 98 27 23 9 70 98 73 93 38 53 60 04 23]])
(defn row
[n]
(get test-tree n))
(defn reduce-pair
[[a b]]
(let [a-sum (apply + a)
b-sum (apply + b)]
(if (> a-sum b-sum) a-sum b-sum)))
(defn solve
[]
(loop [n (dec (count test-tree))
current-row (row n)
above-row (row (dec n))]
(prn n current-row above-row)
(if (zero? n)
(first current-row)
(recur
(dec n)
(->> (interleave current-row (conj above-row 0))
(partition 2 1)
(partition 2)
(map reduce-pair))
(row (- n 2))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment