Skip to content

Commit c86db54

Browse files
committed
Updates from doing AoC 2016
Integrate all updates made to the core files while working the 2016 AoC puzzles. Notable changes are: bumping Clojure to 1.12.0, adding a config file for clj-kondo, and six new utils functions.
1 parent e9abe6b commit c86db54

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

.clj-kondo/config.edn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{:linters {:clojure-lsp/unused-public-var {:level :warning
2+
:exclude #{advent-of-code.utils
3+
part-1 part-2}
4+
:ignore-test-references? true}}}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ pom.xml.asc
1111
.hgignore
1212
.hg/
1313
.tool-versions
14-
.clj-kondo
14+
/.clj-kondo/.cache
1515
.lsp

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
:url "https://github.com/rjray/advent-20XX-clojure"
44
:license {:name "MIT"
55
:url "https://opensource.org/license/mit/"}
6-
:dependencies [[org.clojure/clojure "1.11.0"]
6+
:dependencies [[org.clojure/clojure "1.12.0"]
77
[org.clojure/tools.cli "1.0.219"]
88
[org.clojure/math.numeric-tower "0.0.4"]
99
[org.clojure/math.combinatorics "0.1.6"]

src/advent_of_code/utils.clj

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(ns advent-of-code.utils
22
(:require [clojure.string :as str]
3-
[clojure.java.io :as io]))
3+
[clojure.java.io :as io])
4+
(:import (java.security MessageDigest)
5+
(java.math BigInteger)))
46

57
(defn read-input
68
"Read in the content of the given day-file and return as a blob"
@@ -29,6 +31,28 @@
2931
[line]
3032
(map parse-long (re-seq #"[-+]?\d+" line)))
3133

34+
(defn parse-ranges
35+
"Parse each string of input as a range in the form 'M-N'"
36+
[ranges]
37+
(->> ranges
38+
(map #(str/replace % "-" " "))
39+
(map parse-out-longs)))
40+
41+
(defn manhattan-dist
42+
"Calculate the Manhattan Distance between two points."
43+
[p1 p2]
44+
(+ (abs (- (first p1) (first p2)))
45+
(abs (- (last p1) (last p2)))))
46+
47+
(defn first-duplicate
48+
"Find first element of collection that is a duplicate"
49+
[coll]
50+
(reduce (fn [acc elt]
51+
(if (get acc elt)
52+
(reduced elt)
53+
(assoc acc elt true)))
54+
{} coll))
55+
3256
;; Like the core time macro, but rather than printing the elapsed time it
3357
;; returns a list of (result, time). Returned value is in milliseconds.
3458
(defmacro time-it [expr]
@@ -73,7 +97,7 @@
7397
(defn factorize
7498
"Determine all prime factors of n"
7599
[n]
76-
(loop [x n [p & ps] primes factors []]
100+
(loop [x n, [p & ps] primes, factors []]
77101
(cond
78102
(= 1 x) factors
79103
(zero? (mod x p)) (recur (/ x p) primes (conj factors p))
@@ -94,3 +118,23 @@
94118
([] 1)
95119
([x] x)
96120
([a b] (/ (* a b) (gcd a b))))
121+
122+
;; Taken from https://gist.github.com/jizhang/4325757?permalink_comment_id=2196746#gistcomment-2196746
123+
(defn md5 [^String s]
124+
(let [algorithm (MessageDigest/getInstance "MD5")
125+
raw (.digest algorithm (.getBytes s))]
126+
(format "%032x" (BigInteger. 1 raw))))
127+
128+
(defn create-field
129+
"Create a NxM field as a matrix (vector of vectors). Fill with `with` or nil"
130+
[N M & [with]]
131+
(if (or (seq? with) (vector? with))
132+
;; Ignore N/M and treat each element of `with` as a row in the field
133+
(mapv vec with)
134+
;; Otherwise, use the value of `with` itself (which may be nil)
135+
(vec (repeat M (vec (repeat N with))))))
136+
137+
(defn display
138+
"Display a matrix of characters, as if on a terminal or similar"
139+
[lines]
140+
(println (str/join "\n" (map #(str/join %) lines))))

0 commit comments

Comments
 (0)