Skip to content

Instantly share code, notes, and snippets.

@stuartstein777
Created December 5, 2021 14:25
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 stuartstein777/73f42275324d7c06b6d3510fd70e4229 to your computer and use it in GitHub Desktop.
Save stuartstein777/73f42275324d7c06b6d3510fd70e4229 to your computer and use it in GitHub Desktop.
advent of code 2021 day 5
(ns stuartstein777.2021.day5
(:require [clojure.string :as str]
[clojure.set :as set]
[stuartstein777.file :as f]
[stuartstein777.utils :as u]))
(defn parser [line]
(let [[x1 y1 x2 y2] (->> line
(re-seq #"(\d+),(\d+) -> (\d+),(\d+)")
(u/frest)
(map #(Integer/parseInt %)))]
[[x1 y1] [x2 y2]]))
(defn horizontal-or-vertical? [[[x1 y1] [x2 y2]]]
(or (= x1 x2) (= y1 y2)))
(defn get-horizontal-or-vertical-points [[[x1 y1] [x2 y2]]]
(for [xs (range (min x1 x2) (inc (max x1 x2)))
ys (range (min y1 y2) (inc (max y1 y2)))]
[xs ys]))
(defn get-diagonal-points [[[x1 y1] [x2 y2]]]
(cond (and (>= x2 x1) (>= y2 y1)) (map vector (range x1 (inc x2)) (range y1 (inc y2)))
(and (<= x2 x1) (<= y2 y1)) (map vector (range x2 (inc x1)) (range y2 (inc y1)))
(and (<= x1 x2) (>= y1 y2)) (map vector (range x1 (inc x2)) (reverse (range y2 (inc y1))))
(and (>= x1 x2) (<= y1 y2)) (map vector (reverse (range x2 (inc x1))) (range y1 (inc y2)))))
(defn get-points-on-line [coords]
(if (horizontal-or-vertical? coords)
(get-horizontal-or-vertical-points coords)
(get-diagonal-points coords)))
(->> (f/read-all-lines-and-parse "puzzle-inputs/2021/day5" parser)
#_(filter horizontal-or-vertical?) ; uncomment to solve part 1
(mapcat get-points-on-line)
(frequencies)
(filter (fn [[_ v]] (> v 1)))
(count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment