ref: 3703478dcfedd048437f7a0515dbda2ff8a22d8d
src/aoc/day03.clj
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
(ns aoc.day03 (:require [clojure.java.io :as io])) (def example "..##....... #...#...#.. .#....#..#. ..#.#...#.# .#...##..#. ..#.##..... .#.#.#....# .#........# #.##...#... #...##....# .#..#...#.#") (def paths [[1 1] [3 1] [5 1] [7 1] [1 2]]) (defn read-input [] (->> (slurp (io/resource "input03.txt")) clojure.string/split-lines)) (defn all-coordinates [[x y]] (iterate (fn [[xx yy]] [(+ x xx) (+ y yy)]) [0 0])) (defn tree? [s] (= s \#)) (defn count-trees [lines] (fn [acc [x y]] (let [line (nth lines y) spot (nth line x)] (if (tree? spot) (inc acc) acc)))) (defn compute [input coords] (let [infinite-lines (map cycle input) needed-coords (take-while #(< (second %) (count input)) coords)] (reduce (count-trees infinite-lines) 0 needed-coords))) ;; ---------------------------------------------------------------------------- (defn compute-1 [input] (let [infinite-lines (map cycle input) infinite-coords (all-coordinates [3 1]) needed-coords (take-while #(< (second %) (count input)) infinite-coords)] (reduce (count-trees infinite-lines) 0 needed-coords))) (defn compute-2 [input] (let [c (partial compute input) coords (map all-coordinates paths)] (->> coords (map c) (apply *)))) (defn main [] (let [input (read-input)] (println (compute-1 input)) (println (compute-2 input)))) (main) |