aboutsummaryrefslogtreecommitdiffstats
path: root/day2/part1.clj
blob: 1a4df6b0f3528cb5cf3228dc85c0c436296a2dad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
; Day 2, part 2
; Read a list of instructions from stdin:
;   "down X" increases depth number by X,
;   "up X" decreases depth by X,
;   "forward X" increases xpos by X.
; Print (xpos * depth) after end of data.
;

(require '[clojure.string :as str])

(loop [xpos 0 depth 0]
  (let [input (read-line)]
    (if (empty? input)
      (println (* xpos depth))
      (let [ins (str/split input #" ")
            n (Integer/parseInt (second ins))
            ]
        (recur
          (if (= (first ins) "forward")
            (+ xpos n)
            xpos
            )
          (case (first ins)
            "up" (- depth n)
            "down" (+ depth n)
            depth
            )
          )
        )
      )
    )
  )