Skip to content

Instantly share code, notes, and snippets.

@Solaxun
Created December 4, 2021 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Solaxun/a84b956dc885657dc23e15f826495133 to your computer and use it in GitHub Desktop.
Save Solaxun/a84b956dc885657dc23e15f826495133 to your computer and use it in GitHub Desktop.
AoC 2021: Clojure Day 1
(ns aoc-clj-2021.day1
(:require [clojure.string :as str]
[clojure.set :as set]
[clojure.java.io :as io]))
(def data
(->> (io/resource "day1.txt")
slurp
str/split-lines
(map parse-long)))
;; part 1
(->> (partition 2 1 data)
(filter (fn [[x y]] (> y x)))
count)
;; part 2
(reduce (fn [{:keys [count prev] :as state} [x y z]]
(assoc (if (> (+ x y z) prev)
(update state :count inc)
state)
:prev (+ x y z)))
{:count 0 :prev 0}
(partition 3 1 data))
;; count minus 1, as we started with zero for prev instead of first
;; partition - so of course 1st partition sum is > 0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment