Skip to content

Commit

Permalink
Day 07: Solve part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nihas101 committed Dec 7, 2023
1 parent d17639b commit 1ba692f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 12 deletions.
101 changes: 98 additions & 3 deletions src/advent_of_code_2023/day07.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,104 @@
(ns advent-of-code-2023.day07
(:require
[clojure.string :as string]
[advent-of-code-2023.utils.string :as u]))

(defn parse-input [input])
(defn- hand-type [freqs]
(condp = (sort (vals freqs))
[5] :five-of-a-kind
[1 4] :four-of-a-kind
[2 3] :full-house
[1 1 3] :three-of-a-kind
[1 2 2] :two-pair
[1 1 1 2] :one-pair
:high-card))

(defn day07-1 [parsed-input])
(defn parse-input [input]
(transduce
(comp
(remove #(some string/blank? %))
(map (fn [[hand bid]]
(let [freqs (frequencies hand)]
{:hand hand
:bid (Long/parseLong bid)
:frequencies freqs
:type (hand-type freqs)}))))
conj [] (u/split-pairs input)))

(defn day07-2 [parsed-input])
(defonce ^:private hand-type-ranking
{:five-of-a-kind 6
:four-of-a-kind 5
:full-house 4
:three-of-a-kind 3
:two-pair 2
:one-pair 1
:high-card 0})

(defonce ^:private card-ranking
{\A 12
\K 11
\Q 10
\J 9
\T 8
\9 7
\8 6
\7 5
\6 4
\5 3
\4 2
\3 1
\2 0})

(defn- hand-comp [[^long t1 & h1 :as c1] [^long t2 & h2]]
(cond
(nil? c1) 0
(< t1 t2) -1
(> t1 t2) 1
:else (recur h1 h2)))

(defn- day07 [hands sort-fn]
(transduce
(map-indexed (fn [^long rank {:keys [^long bid]}]
(* (inc rank) bid)))
+ (sort-by sort-fn hand-comp hands)))

(defn day07-1 [hands]
(day07 hands
(fn [{:keys [type hand]}]
(conj (map card-ranking hand)
(hand-type-ranking type)))))

(defonce ^:private joker-card-ranking
{\A 12
\K 11
\Q 10
\T 9
\9 8
\8 7
\7 6
\6 5
\5 4
\4 3
\3 2
\2 1
\J 0})

(defonce ^:private joker-hand
{[:four-of-a-kind 4] :five-of-a-kind
[:four-of-a-kind 1] :five-of-a-kind
[:full-house 3] :five-of-a-kind
[:full-house 2] :five-of-a-kind
[:three-of-a-kind 3] :four-of-a-kind
[:three-of-a-kind 1] :four-of-a-kind
[:two-pair 2] :four-of-a-kind
[:two-pair 1] :full-house
[:one-pair 2] :three-of-a-kind
[:one-pair 1] :three-of-a-kind
[:high-card 1] :one-pair})

(defn day07-2 [hands]
(day07 hands
(fn [{:keys [type hand frequencies]}]
(conj (map joker-card-ranking hand)
(hand-type-ranking (get joker-hand [type (frequencies \J)]
type))))))
18 changes: 9 additions & 9 deletions test/advent_of_code_2023/day07_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
[advent-of-code-2023.day07 :refer :all]
[advent-of-code-2023.test-utils :as tu]))

(def ^:private example-input (parse-input ""))
(def ^:private example-input (parse-input "32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483"))

(def ^:private input (parse-input (tu/slurp-input "resources/day07.txt")))

(deftest day07-1-example-test
(testing "day07-1 example"
(is (= nil
(day07-1 example-input)))))
(is (= 6440 (day07-1 example-input)))))

(deftest day07-1-test
(testing "day07-1"
(is (= nil
(day07-1 input)))))
(is (= 246163188 (day07-1 input)))))

(deftest day07-2-example-test
(testing "day07-2 example"
(is (= nil
(day07-2 example-input)))))
(is (= 5905 (day07-2 example-input)))))

(deftest day07-2-test
(testing "day07-2"
(is (= nil
(day07-2 input)))))
(is (= 245794069 (day07-2 input)))))

0 comments on commit 1ba692f

Please sign in to comment.