-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
92 lines (79 loc) · 2.28 KB
/
core.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(ns _2020.nine.core
(:require
[util :refer [input]]
[clojure.math.combinatorics :as combo]
))
(defn unique-ns-that-sum [sum n xs]
(let [combs (combo/combinations xs n)]
(->> combs
(filter (fn [comb]
(-> (apply + comb)
(= sum))))
first)))
(comment
(peek '(1 2 3))
(pop '(1 2 3))
(cons 0 '(1 2 3))
(-> '(1 2 3) rest (concat '(4)))
(conj '(1 2 3) 0)
(unique-ns-that-sum 40 2 [35 20 15 25 47])
(->> (input "example.txt")
(map read-string)
(take 5)
reverse
butlast
(cons 40)
)
(println "\n\nbreak\n\n"))
(defn first-invalid [{:keys [group-size xs]}]
(loop [group (->> xs (take group-size) reverse)
next-xs (->> xs (drop group-size))]
(if-let [next (first next-xs)]
(let [ns (unique-ns-that-sum next 2 group)]
(if (seq ns)
(recur (->> group butlast (cons next))
(rest next-xs))
next))
:none-invalid)))
(comment
(first-invalid {:group-size 5
:xs (->> (input "example.txt")
(map read-string))})
(->> (input "input.txt")
(map read-string)
(filter #{507622668}))
(first-invalid {:group-size 25
:xs (->> (input "input.txt")
(map read-string))})
;; 507622668
)
(defn contiguous-ns-that-sum [sum xs]
(loop [ns (take 1 xs)
xxs (rest xs)]
(if-let [next (some-> xxs first)]
(let [current-sum (->> ns (apply +))]
(cond
(= current-sum sum)
ns
(> current-sum sum)
(recur (->> ns butlast) xxs)
(< current-sum sum)
(recur (cons next ns) (rest xxs))))
:no-group-found)))
(comment
(let [res
(contiguous-ns-that-sum 127
(->> (input "example.txt")
(map read-string))
)]
(+ (apply max res) (apply min res))
)
(let [res
(contiguous-ns-that-sum 507622668
(->> (input "input.txt")
(map read-string))
)]
(+ (apply max res) (apply min res))
)
;; 76688505
)