Skip to content

Instantly share code, notes, and snippets.

@agentbellnorm
Last active December 14, 2020 17:47
Show Gist options
  • Save agentbellnorm/a30cebaa3933f3ad8e157b81a14d80e0 to your computer and use it in GitHub Desktop.
Save agentbellnorm/a30cebaa3933f3ad8e157b81a14d80e0 to your computer and use it in GitHub Desktop.
(ns day-eleven
(:require
[ysera.test :refer [is is-not is= deftest]]
[ysera.debug :refer [printreturn printlet]]))
(defn get-seat
{:test (fn []
(is= (get-seat [".#"
"L."] [1 0]) "#")
(is= (get-seat [".#"
"L."] [0 1]) "L")
(is= (get-seat [".#"
"L."] [-1 -1]) ".")
(is= (get-seat [".#"
"L."] [3 3]) "."))}
[seats [x y]]
(let [row (get seats y)]
(if row
(str (get row x))
".")))
(defn get-adjacent
[[x y]]
[[(dec x) (dec y)]
[x (dec y)]
[(inc x) (dec y)]
[(dec x) y]
[(inc x) y]
[(dec x) (inc y)]
[x (inc y)]
[(inc x) (inc y)]])
(defn is-taken
{:test (fn []
(is (is-taken [".#"
"L."] [1 0]))
(is-not (is-taken [".#"
"L."] [0 1])))}
[seats coords]
(= (get-seat seats coords) "#"))
(defn number-of-taken-adjacent
{:test (fn []
(is= (number-of-taken-adjacent ["L##"
"###"
"L#."] [0 0]) 3)
(is= (number-of-taken-adjacent ["LL#"
"LL#"
"###"] [0 0]) 0))}
[seats [x y]]
(->> (get-adjacent [x y])
(map (fn [coordinates] (is-taken seats coordinates)))
(filter identity)
(count)))
(defn next-seat-map
{:test (fn []
(is= (next-seat-map
["#.##.##.##"
"#######.##"
"#.#.#..#.."
"####.##.##"
"#.##.##.##"
"#.#####.##"
"..#.#....."
"##########"
"#.######.#"
"#.#####.##"])
["#.LL.L#.##"
"#LLLLLL.L#"
"L.L.L..L.."
"#LLL.LL.L#"
"#.LL.LL.LL"
"#.LLLL#.##"
"..L.L....."
"#LLLLLLLL#"
"#.LLLLLL.L"
"#.#LLLL.##"]))}
[seats]
(reduce (fn [new-seats y]
(conj new-seats
(reduce
(fn [new-row x]
(cond
(and
(= "L" (get-seat seats [x y]))
(= 0 (number-of-taken-adjacent seats [y])))
(str new-row "#")
(and
(= "#" (get-seat seats [x y]))
(>= (number-of-taken-adjacent seats [x y]) 4))
(str new-row "L")
:else (str new-row (get-seat seats [x y]))))
""
(range (count (first seats))))))
[]
(range (count seats))))
(defn stabilize
{:test (fn []
(is= (stabilize
["L.LL.LL.LL"
"LLLLLLL.LL"
"L.L.L..L.."
"LLLL.LL.LL"
"L.LL.LL.LL"
"L.LLLLL.LL"
"..L.L....."
"LLLLLLLLLL"
"L.LLLLLL.L"
"L.LLLLL.LL"])
["#.#L.L#.##"
"#LLL#LL.L#"
"L.#.L..#.."
"#L##.##.L#"
"#.#L.LL.LL"
"#.#L#L#.##"
"..L.L....."
"#L#L##L#L#"
"#.LLLLLL.L"
"#.#L#L#.##"]))}
[seats]
(loop [current-seats seats]
(let [next-seats (next-seat-map current-seats)]
(if (= next-seats current-seats)
current-seats
(recur next-seats)))))
(->> ["L.LL.LL.LL"
"LLLLLLL.LL"
"L.L.L..L.."
"LLLL.LL.LL"
"L.LL.LL.LL"
"L.LLLLL.LL"
"..L.L....."
"LLLLLLLLLL"
"L.LLLLLL.L"
"L.LLLLL.LL"]
(stabilize)
(clojure.string/join)
(re-seq #"#")
(count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment