about summary refs log tree commit diff
path: root/day08.clj
blob: 18091d800fe2c6b143f57a7758ba403c3690e4be (plain) (blame)
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
(ns org.vuxu.aoc2021.day08
  (:require [clojure.string :as str]
            [clojure.set :as set]))

(def data
  (->> (slurp "day08")
       str/split-lines
       (map #(str/split % #" \| " 2))
       (map (partial map #(str/split % #" ")))))
  
(def part1
  (->> data
       (map second)
       flatten
       (map count)
       (filter #{2 4 3 7})   ; LCD 1 4 7 8
       count))
;; => 342

(def part2
  (let [canonical-pattern ["abcefg" "cf" "acdeg" "acdfg" "bdcf"
                           "abdfg" "abdefg" "acf" "abcdefg" "abcdfg"]
        freq (frequencies (apply str canonical-pattern))
        dict (zipmap (map #(sort (map freq %)) canonical-pattern)
                     (range 10))]
    (apply + (for [[digits output] data]
               (let [line-freq (frequencies (apply str digits))]
                 (->> output
                      (map #(sort (map line-freq %)))
                      (map dict)
                      (apply str)
                      parse-long))))))
;; => 1068933