advent of code, days 1-3
and so, i've finally thrown together some advent of code stuff!
https://gist.github.com/Archenoth/4256a377c186b702e046f57130ae8ee9
i started pretty late, but i decided pretty quickly that I'm probably going to bias towards using clojure for these challenges, and so far everything has been solvable pretty easily with loop and recur!
re: advent of code, day 2
https://gist.github.com/Archenoth/4256a377c186b702e046f57130ae8ee9#-day-2-dive-
part one was literally just a case in a loop, not a lot interesting to say about that!
(loop [[[dir dist] & rem] input
horiz 0
depth 0]
(if-not dir
(* horiz depth)
(case dir
"forward" (recur rem (+ horiz dist) depth)
"down" (recur rem horiz (+ depth dist))
"up" (recur rem horiz (- depth dist)))))
part two was just a slightly different recur, so there's not a lot to say about that either
(loop [[[dir dist] & rem] input
aim 0
horiz 0
depth 0]
(if-not dir
(* horiz depth)
(case dir
"forward" (recur rem aim (+ horiz dist) (+ depth (* aim dist)))
"down" (recur rem (+ aim dist) horiz depth)
"up" (recur rem (- aim dist) horiz depth))))
also, this was when i decided to use clojure to read in the data itself too, since it seems like passing larger datasets in my version of ob-clojure seems to blow the class size limit since it just plops the entire data structure into a let
i fixed how ob-clojure handles passed-in lists, but i haven't yet fixed how it loads larger datasets (though i still need to sign the fsf papers if i want to contrib it back upstream anyway, which is kind of a hassle :s)
re: advent of code, day 3
https://gist.github.com/Archenoth/4256a377c186b702e046f57130ae8ee9#-day-3-binary-diagnostic-
day 3 part 1 was easy peasy, i could just use the clojure frequencies function on each column, and sort them by that to make my destructuring consistent!
(loop [[more? & rem] (first input)
col 0
gamma ""
epsilon ""]
(if-not more?
(* (Integer/parseInt gamma 2) (Integer/parseInt epsilon 2))
(let [[[e] [g]] (sort-by second (frequencies (map #(nth % col) input)))]
(recur rem (+ 1 col) (str gamma g) (str epsilon e)))))
in part 2 was harder to understand what to do, and i had to break the column generalization into a separate function that i didn't know what to call (so i just called it "magic", hee)
the idea here is that the function will extract the most and least common bit, and their counts, then based on the default, do the right thing for the oxygen and carbon values
i could do both at the same time since the function should never reduce the values below one row
(defn magic [col values default]
(let [colvals (map #(nth % col) values)
[[lc lcc] [mc mcc]] (sort-by second (frequencies colvals))
look-for (if (= lcc mcc) default (if (= default 1) mc lc))]
(filter #(= look-for (nth % col)) values)))
(loop [col 0
omatches input
cmatches input]
(if (and (= 1 (count omatches)) (= 1 (count cmatches)))
(let [oxygen (apply str (first omatches))
coscrub (apply str (first cmatches))]
(* (Integer/parseInt oxygen 2) (Integer/parseInt coscrub 2)))
(recur (+ 1 col) (magic col omatches 1) (magic col cmatches 0))))
re: advent of code, day 1
https://gist.github.com/Archenoth/4256a377c186b702e046f57130ae8ee9#-day-1-sonar-sweep-
there wasn't anything particularly tricky about this day; i just basically added to the accumulator whenever the two numbers differed in the right way
part 2 was a little more interesting because i broke out the ##Inf constant to avoid the addition from happening in the first run. also destructuring made it a cakewalk to grab any number of numbers for the window~