From da59603764532ead4ba804af1ec94a964172afa0 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 11 Dec 2020 21:38:16 -0700 Subject: [PATCH] Solve arrangement count for AOC Day 10pt2 --- projects/aoc/src/year2020/Adapters.hx | 1 + projects/aoc/src/year2020/Adapters.kiss | 13 ++++++++++++- projects/aoc/src/year2020/Solutions.hx | 1 + projects/aoc/src/year2020/Solutions.kiss | 5 +++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/projects/aoc/src/year2020/Adapters.hx b/projects/aoc/src/year2020/Adapters.hx index c421ebf1..020760ae 100644 --- a/projects/aoc/src/year2020/Adapters.hx +++ b/projects/aoc/src/year2020/Adapters.hx @@ -1,5 +1,6 @@ package year2020; +import haxe.Int64; import kiss.Prelude; import year2020.Util; diff --git a/projects/aoc/src/year2020/Adapters.kiss b/projects/aoc/src/year2020/Adapters.kiss index 7077ff9d..fe3f19ce 100644 --- a/projects/aoc/src/year2020/Adapters.kiss +++ b/projects/aoc/src/year2020/Adapters.kiss @@ -8,4 +8,15 @@ (if (dist.exists num) (+ 1 (dictGet dist num)) 1))) - dist)) \ No newline at end of file + dist)) + +(defun arrangementCount [:kiss.List ratings startingIndex] + (if (= startingIndex (- ratings.length 1)) 1 + (let [&mut :Int64 sum 0 + startingRating (nth ratings startingIndex) + potentialNextAdapters (filter + (for i (range (+ 1 startingIndex) (+ 4 startingIndex)) i) + (lambda [index] (and (< index ratings.length) (<= (nth ratings index) (+ 3 startingRating)))))] + (doFor subCount (map potentialNextAdapters (arrangementCount.bind ratings)) + (set sum (Int64.add sum subCount))) + sum))) diff --git a/projects/aoc/src/year2020/Solutions.hx b/projects/aoc/src/year2020/Solutions.hx index 92cda945..b145d342 100644 --- a/projects/aoc/src/year2020/Solutions.hx +++ b/projects/aoc/src/year2020/Solutions.hx @@ -1,6 +1,7 @@ package year2020; import haxe.ds.Map; +import haxe.Int64; import StringTools; import kiss.Prelude; import kiss.Stream; diff --git a/projects/aoc/src/year2020/Solutions.kiss b/projects/aoc/src/year2020/Solutions.kiss index 39efef93..5c1099a5 100644 --- a/projects/aoc/src/year2020/Solutions.kiss +++ b/projects/aoc/src/year2020/Solutions.kiss @@ -116,8 +116,9 @@ (assert (= 16107959 (+ (apply min tuple) (apply max tuple))))) // Day 10 - (let [adapters (Util.readInts "src/year2020/inputs/day10.txt")] - (.sort adapters Reflect.compare) + (assert (Int64.eq 8 (Adapters.arrangementCount (sort [0 22 16 10 15 5 1 11 7 19 6 12 4]) 0))) + (assert (Int64.eq 19208 (Adapters.arrangementCount (sort [0 52 28 33 18 42 31 14 46 20 48 47 24 23 49 45 19 38 39 11 1 32 25 35 8 17 7 9 4 2 34 10 3]) 0))) + (let [adapters (sort (Util.readInts "src/year2020/inputs/day10.txt"))] (adapters.unshift 0) (adapters.push (+ 3 (last adapters))) (let [diffs (Adapters.differences adapters)