Skip to content

Instantly share code, notes, and snippets.

@hroi
hroi / dijkstra.clj
Created April 18, 2011 10:40
dijkstra algo (exercise)
(def topo {:R1 {:R2 10, :R9 30}
:R2 {:R1 10, :R3 10, :R4 100, :R9 30}
:R3 {:R2 10, :R4 10, }
:R4 {:R2 100, :R3 10, :R5 10, :R9 30}
:R5 {:R4 10, :R9 30}
:R9 {:R1 30 :R2 30 :R4 30 :R5 30}})
(defn spf [topo node]
(let [infinity (Integer/MAX_VALUE)
nodes (set (keys topo))]
(defmacro tx "Wrap expr in a DB transaction" [expr]
`(with-connection db
(transaction
~expr)))
; Use
(tx
(doseq [x (range 100)]
(insert-values :mytable [:xid] [x])))
; function that takes {"foo" (:x :y :z),
; "bar" (:z :a :b)}
; and returns {:x ("foo"),
; :y ("foo"),
; :z ("foo" "bar"),
; :a ("bar"),
; :b ("bar")}
(defn inverse-map [m]
(apply merge
(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]
; 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.
; 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 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 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 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 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