Skip to content

Instantly share code, notes, and snippets.

; Problem 11
;
; What is the greatest product of four adjacent numbers in any direction (up,
; down, left, right, or diagonally) in the 2020 grid?
(def grid20 [[ 8 2 22 97 38 15 0 40 0 75 4 5 7 78 52 12 50 77 91 8]
[49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 4 56 62 0]
[81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 3 49 13 36 65]
[52 70 95 23 4 60 11 42 69 24 68 56 1 32 56 71 37 2 36 91]
[22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80]
user> (time (reduce + (loop [foo 0 bar '()]
(if (= foo 1000000)
bar
(recur (inc foo) (cons foo bar))))))
"Elapsed time: 912.220229 msecs"
499999500000
user> (time (reduce + (loop [foo 0 bar (transient [])]
(if (= foo 1000000)
(persistent! bar)
(recur (inc foo) (conj! bar foo))))))
; Problem 12
;
; What is the value of the first triangle number to have over five hundred divisors?
;
(def natnums (iterate inc 1))
(def trinums (lazy-cat [(first natnums)] (map + trinums (rest natnums))))
(defn divcount [n]
(* 2 (count
; Problem 13
; Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
;
(def answer
(let [numbers
[37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
; Problem 14
;
; The following iterative sequence is defined for the set of positive integers:
;
; n -> n/2 (n is even)
; n -> 3n + 1 (n is odd)
;
; Using the rule above and starting with 13, we generate the following sequence:
;
; 13 40 20 10 5 16 8 4 2 1
; Problem 15
; Starting in the top left corner of a 22 grid, there are 6
; routes (without backtracking) to the bottom right corner.
;
; How many routes are there through a 2020 grid?
;
(def facts (lazy-cat [1] (map * facts (rest natnums))))
(defn solve [gridsize]
; Problem 17
;
; If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there
; are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
;
; If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how
; many letters would be used?
(defn say-number [n]
(cond
; Problem 19
; How many Sundays fell on the first of the month during the twentieth
; century (1 Jan 1901 to 31 Dec 2000)?
(count
(for [year (range 1 101) month (range 0 12)
:let [day (.getDay (doto (java.util.Date.) (.setYear year)
(.setMonth month) (.setDate 1) (.getDay)))]
:when (= day 0)]
[year, month]))
; Problem 18
; 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.
(ns vm-clj
(:import
(com.vmware.vim25.mo ServiceInstance
InventoryNavigator)))
(def *defaults* {:url (java.net.URL. "https://vcenter.example.com/sdk")
:username "joe"
:password "random"})
(defn get-service-instance [url username password]