day 4 WIP

This commit is contained in:
Aleh Suprunovich 2024-12-04 14:13:10 +03:00
parent eb82538b78
commit df80da0eec

89
src/day4.clj Normal file
View File

@ -0,0 +1,89 @@
(ns day4
(:require [clojure.string :as string]
[helpers :refer [get-input]]))
(def input-example
"MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX")
(def input-example
"..X...
.SAMX.
.A..A.
XMAS.S
.X....")
(def input (string/split-lines input-example))
;(def input (string/split-lines (get-input 4)))
(def column-len (count input))
(def line-len (count (first input)))
(defn get-string [coords]
(->>
coords
(map #(get-in input %))
(apply str)))
(defn diagonals-right-down [line]
(map (fn [l c] [l c])
(range line line-len)
(range column-len)))
(defn diagonals-down-right [column]
(map (fn [l c] [l c])
(range line-len)
(range column column-len )))
(defn diagonals-up-right [line]
(map (fn [l c] [l c])
(range line -1 -1)
(range column-len)))
(defn diagonals-right-up [column]
(map (fn [l c] [l c])
(range line-len -1 -1)
(range column column-len)))
(def diagonals
(->>
(concat
(map diagonals-right-down (range line-len))
(map diagonals-down-right (range 1 column-len))
(map diagonals-up-right (range line-len))
(map diagonals-right-up (range 1 column-len)))
(map get-string)))
(defn input-lines [input]
(let [lines input
columns (->> (map seq lines)
(apply interleave)
(partition column-len)
(map (partial apply str)))
rev-fn (fn [i] (map #(apply str (reverse %)) i))]
(concat
lines (rev-fn lines)
columns (rev-fn columns)
diagonals (rev-fn diagonals))))
(defn check-line [l]
(let [r (re-seq #"X.*M.*A.*S" l)]
(println "Checking line:" l "\nResult:" r)
r))
(def result
(->>
(input-lines input)
(map check-line)
(apply concat)
))
(println "XMAS appears" (count result) "times.")