day 1
This commit is contained in:
commit
b15a212927
16
.gitignore
vendored
Normal file
16
.gitignore
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
# ---> Clojure
|
||||
pom.xml
|
||||
pom.xml.asc
|
||||
*.jar
|
||||
*.class
|
||||
/lib/
|
||||
/classes/
|
||||
/target/
|
||||
/checkouts/
|
||||
.lein-deps-sum
|
||||
.lein-repl-history
|
||||
.lein-plugins/
|
||||
.lein-failures
|
||||
.nrepl-port
|
||||
.cpcache/
|
||||
|
||||
2
deps.edn
Normal file
2
deps.edn
Normal file
@ -0,0 +1,2 @@
|
||||
{:paths ["src" "inputs"]
|
||||
:deps {org.clojure/clojure {:mvn/version "1.12.0"}}}
|
||||
1000
inputs/day1
Normal file
1000
inputs/day1
Normal file
File diff suppressed because it is too large
Load Diff
6
inputs/day1.example
Normal file
6
inputs/day1.example
Normal file
@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
28
src/day1.clj
Normal file
28
src/day1.clj
Normal file
@ -0,0 +1,28 @@
|
||||
(ns day1
|
||||
(:require [clojure.edn :as edn]
|
||||
[clojure.string :as s]))
|
||||
|
||||
|
||||
(def input
|
||||
(->> (slurp "inputs/day1")
|
||||
s/split-lines
|
||||
(map #(s/split % #"\ +"))
|
||||
(map #(map edn/read-string %))))
|
||||
|
||||
(def left-list (map first input))
|
||||
(def right-list (map second input))
|
||||
|
||||
(def distances
|
||||
(map (fn [l r] (abs (- r l)))
|
||||
(sort left-list)
|
||||
(sort right-list)))
|
||||
|
||||
(defn count-n-in-right-list [n]
|
||||
(count (filter #(= n %) right-list)))
|
||||
|
||||
(defn similarity-for-n [n]
|
||||
(* n (count-n-in-right-list n)))
|
||||
|
||||
|
||||
(println "Total distance:" (reduce + distances))
|
||||
(println "Similarity score:" (reduce + (map similarity-for-n left-list)))
|
||||
Loading…
x
Reference in New Issue
Block a user