Last active
June 1, 2022 07:42
-
-
Save bobbicodes/0ca203bfa38706f19b2d0095c02b87cb to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 1
This file contains hidden or 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
;; https://adventofcode.com/2021/day/1 | |
(ns day01 | |
(:require [clojure.string :as str])) | |
(def input (map #(Integer/parseInt %) (str/split-lines (slurp "day01.txt")))) | |
;; this is like levels of tired->wired etc... | |
;; there was a loop-based solution in https://www.youtube.com/watch?v=q_I3r0JGhR8 by onthecodeagain | |
;; which received some comments from Erik Grundy and Sebastian Anklamm offering a more functional solution: | |
(defn part-1 [coll] | |
(->> coll | |
(partition 2 1) | |
(map (fn [[x y]] (- x y))) | |
(filter #(neg? %)) | |
(count))) | |
(defn part-2 [nums] | |
(->> nums | |
(partition 3 1) | |
(map #(apply + %)) | |
(partition 2 1) | |
(map #(apply < %)) | |
(filter identity) | |
count)) | |
(part-2 input) | |
;;@borkdude's "where's the rest of the code?" solution: | |
(map < input (rest input)) | |
(map + input (next input) (nnext input)) |
Solution using loop
by Daniel from OnTheCodeAgain https://gist.github.com/danownsthisspace/671596eb4010f62c8dda7a7e61a853ee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tonsky's solution is essentially the same as borkdude's, just with more ceremony:
https://github.com/tonsky/advent-of-code/blob/main/src/advent_of_code/year2021/day1.clj
The big takeaway is that mapping on multiple collections is incredibly useful. Use it.