day 7
This commit is contained in:
parent
e15e01a9a5
commit
4723e304ab
65
src/day7.clj
Normal file
65
src/day7.clj
Normal file
@ -0,0 +1,65 @@
|
||||
(ns day7
|
||||
(:require [clojure.string :as string]
|
||||
[helpers :refer [get-input]]))
|
||||
|
||||
(def example
|
||||
".......S.......
|
||||
...............
|
||||
.......^.......
|
||||
...............
|
||||
......^.^......
|
||||
...............
|
||||
.....^.^.^.....
|
||||
...............
|
||||
....^.^...^....
|
||||
...............
|
||||
...^.^...^.^...
|
||||
...............
|
||||
..^...^.....^..
|
||||
...............
|
||||
.^.^.^.^.^...^.
|
||||
...............")
|
||||
|
||||
|
||||
;(def input (mapv (partial apply vector) (string/split-lines example)))
|
||||
(def input (mapv (partial apply vector) (string/split-lines (get-input 7))))
|
||||
|
||||
(def line-len (count input))
|
||||
(def column-len (count (first input)))
|
||||
|
||||
(def start [0 (.indexOf (apply str (first input)) "S")])
|
||||
|
||||
(def split (atom 0))
|
||||
|
||||
(defn prop [[l c]]
|
||||
(if (= \^ (get-in input [l c]))
|
||||
(do (swap! split inc)
|
||||
[[l (dec c)] [l (inc c)]])
|
||||
[[l c]]))
|
||||
|
||||
(defn propagate [beams]
|
||||
(->>
|
||||
beams
|
||||
(map (fn [[l c]] [(inc l) c]))
|
||||
(mapcat prop)))
|
||||
|
||||
(loop [beams [start]]
|
||||
(if (< (-> beams first first) column-len)
|
||||
(recur (set (propagate beams)))
|
||||
(println "Beam split" @split "times")))
|
||||
|
||||
(def memo (atom {}))
|
||||
|
||||
(def timelines
|
||||
(memoize
|
||||
(fn [[l c]]
|
||||
(cond
|
||||
(or (neg? l) (neg? c) (>= c column-len) (>= l line-len))
|
||||
1
|
||||
(= \^ (get-in input [l c]))
|
||||
(+ (timelines [(inc l) (inc c)])
|
||||
(timelines [(inc l) (dec c)]))
|
||||
:else
|
||||
(timelines [(inc l) c])))))
|
||||
|
||||
(println "Timelines:" (timelines start))
|
||||
Loading…
x
Reference in New Issue
Block a user