aoc

ref: master

2020/src/aoc/day01.clj


 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
34
35
36
37
38
39
40
41
42
43
44
45
46
(ns aoc.day01
  (:require [clojure.java.io :as io]))

(defn lines [s]
  (clojure.string/split s #"\n"))

;; ------------

(defn read-input []
  (->> "input01.txt"
       io/resource
       slurp
       lines
       (map read-string)))

(defn produce-matches-1 [input]
  (for [x input]
    (for [y input]
      (when (= 2020 (+ x y))
        [x y]))))

(defn compute-1 [input]
  (->> (produce-matches-1 input)
       flatten
       (remove nil?)
       (take 2)
       (apply *)))

(defn produce-matches-2 [input]
  (for [x input]
    (for [y input]
      (for [z input]
        (when (= 2020 (+ x y z))
          [x y z])))))

(defn compute-2 [input]
  (->> (produce-matches-2 input)
       flatten
       (remove nil?)
       (take 3)
       (apply *)))

(defn main []
  (let [input (read-input)]
    (println (compute-1 input))
    (println (compute-2 input))))