Last active
December 1, 2020 17:52
-
-
Save g7s/d96f8a30df007ae126906b16f714b612 to your computer and use it in GitHub Desktop.
Advent of Code 2020 - Day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn sum-pair | |
[target nums] | |
(when (pos? (count nums)) | |
(let [min-el (get nums 0) | |
max-el (peek nums)] | |
(case (compare (+ min-el max-el) target) | |
1 (recur target (pop nums)) | |
-1 (recur target (subvec nums 1)) | |
0 [min-el max-el])))) | |
(defn nums | |
[rdr] | |
(->> (line-seq rdr) | |
(map #(Integer/parseInt %)) | |
sort | |
vec)) | |
(defn aoc-1 | |
"https://adventofcode.com/2020/day/1" | |
[input-file-path] | |
(with-open [rdr (clojure.java.io/reader input-file-path)] | |
(some->> (sum-pair 2020 (nums rdr)) | |
(apply *)))) | |
(defn aoc-12 | |
"https://adventofcode.com/2020/day/1#part2" | |
[input-file-path] | |
(with-open [rdr (clojure.java.io/reader input-file-path)] | |
(let [vnums (nums rdr) | |
coll (->> (subvec vnums 1) | |
(iterate #(subvec % 1)) | |
(interleave vnums) | |
(partition 2))] | |
(some->> (keep (fn [[n v]] | |
(some-> (sum-pair (- 2020 n) v) | |
(conj n))) | |
coll) | |
first | |
(apply *))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment