mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:23:17 -09:00
do day 7
This commit is contained in:
parent
e922e14436
commit
4018ae20dd
60
2023/src/day07.clj
Normal file
60
2023/src/day07.clj
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
(ns ^{:doc "Day 7"
|
||||||
|
:author "Adam Jeniski"}
|
||||||
|
day07 (:require [clojure.string :as string]
|
||||||
|
[core :refer [get-puzzle-input]]))
|
||||||
|
|
||||||
|
(def input (get-puzzle-input 7))
|
||||||
|
|
||||||
|
;; part 1
|
||||||
|
(->> input
|
||||||
|
(map #(string/split % #" "))
|
||||||
|
(map #(update % 1 (fn [x] (Integer/parseInt x))))
|
||||||
|
(sort-by
|
||||||
|
(juxt (comp (fn hand-type [hand]
|
||||||
|
(let [v (->> hand
|
||||||
|
frequencies
|
||||||
|
vals)]
|
||||||
|
(cond (and (some #(= 3 %) v)
|
||||||
|
(some #(= 2 %) v))
|
||||||
|
7,
|
||||||
|
(= 2 (count (filter #(= 2 %) v)))
|
||||||
|
5
|
||||||
|
:else (* 2 (apply max v))))) first)
|
||||||
|
(comp (partial into [])
|
||||||
|
(partial map (->> "23456789TJQKA"
|
||||||
|
(map-indexed #(vector %2 (inc %1)))
|
||||||
|
(into {}))) first)))
|
||||||
|
(map-indexed (fn [idx [_ v]]
|
||||||
|
(* v (inc idx))))
|
||||||
|
(reduce +))
|
||||||
|
|
||||||
|
;; part 2
|
||||||
|
(->> input
|
||||||
|
(map #(string/split % #" "))
|
||||||
|
(map #(update % 1 (fn [x] (Integer/parseInt x))))
|
||||||
|
(sort-by
|
||||||
|
(juxt (comp (fn hand-type [hand]
|
||||||
|
(let [w (+ (count (filter #(= % \J) hand)))
|
||||||
|
v (->> hand
|
||||||
|
(filter #(not= % \J))
|
||||||
|
frequencies
|
||||||
|
vals
|
||||||
|
(sort >)
|
||||||
|
(into [])
|
||||||
|
(#(if (empty? %)
|
||||||
|
(list 5)
|
||||||
|
(update % 0 + w))))]
|
||||||
|
(cond (and (some #(= 3 %) v)
|
||||||
|
(some #(= 2 %) v))
|
||||||
|
7,
|
||||||
|
(= 2 (count (filter #(= 2 %) v)))
|
||||||
|
5
|
||||||
|
:else (* 2 (apply max v))))) first)
|
||||||
|
(comp (partial into [])
|
||||||
|
(partial map (->> "J23456789TQKA"
|
||||||
|
(map-indexed #(vector %2 (inc %1)))
|
||||||
|
(into {}))) first)))
|
||||||
|
(map-indexed (fn [idx [_ v]]
|
||||||
|
(* v (inc idx))))
|
||||||
|
(reduce +))
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user