From acd1289fe42933633f2edf46c238ea179df3f7f7 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 10 Dec 2020 18:57:54 -0700 Subject: [PATCH] Solve AOC Day 10 pt 1 --- projects/aoc/src/year2020/Adapters.hx | 9 ++ projects/aoc/src/year2020/Adapters.kiss | 12 +++ projects/aoc/src/year2020/Solutions.hx | 1 + projects/aoc/src/year2020/Solutions.kiss | 9 ++ projects/aoc/src/year2020/Util.kiss | 3 +- projects/aoc/src/year2020/inputs/day10.txt | 100 +++++++++++++++++++++ 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 projects/aoc/src/year2020/Adapters.hx create mode 100644 projects/aoc/src/year2020/Adapters.kiss create mode 100644 projects/aoc/src/year2020/inputs/day10.txt diff --git a/projects/aoc/src/year2020/Adapters.hx b/projects/aoc/src/year2020/Adapters.hx new file mode 100644 index 00000000..c421ebf1 --- /dev/null +++ b/projects/aoc/src/year2020/Adapters.hx @@ -0,0 +1,9 @@ +package year2020; + +import kiss.Prelude; +import year2020.Util; + +using StringTools; + +@:build(kiss.Kiss.build("src/year2020/Adapters.kiss")) +class Adapters {} diff --git a/projects/aoc/src/year2020/Adapters.kiss b/projects/aoc/src/year2020/Adapters.kiss new file mode 100644 index 00000000..7851efd5 --- /dev/null +++ b/projects/aoc/src/year2020/Adapters.kiss @@ -0,0 +1,12 @@ +(defun differences [:kiss.List ratings] + (for idx (range (- ratings.length 1)) + (- (nth ratings (+ idx 1)) (nth ratings idx)))) + +(defun distribution [:kiss.List numbers] + (let [:Map dist (new Map)] + (doFor num numbers + (dictSet dist num + (if (dist.exists num) + (+ 1 (dictGet dist num)) + 1))) + dist)) \ No newline at end of file diff --git a/projects/aoc/src/year2020/Solutions.hx b/projects/aoc/src/year2020/Solutions.hx index d6d46400..92cda945 100644 --- a/projects/aoc/src/year2020/Solutions.hx +++ b/projects/aoc/src/year2020/Solutions.hx @@ -13,6 +13,7 @@ import year2020.Seating; import year2020.Customs; import year2020.Bags; import year2020.BootCode; +import year2020.Adapters; @:build(kiss.Kiss.build("src/year2020/Solutions.kiss")) class Solutions {} diff --git a/projects/aoc/src/year2020/Solutions.kiss b/projects/aoc/src/year2020/Solutions.kiss index 5858c2b6..2c1e40cb 100644 --- a/projects/aoc/src/year2020/Solutions.kiss +++ b/projects/aoc/src/year2020/Solutions.kiss @@ -98,5 +98,14 @@ [35 20 15 25 47 40 62 55 65 95 102 117 150 182 127 219 299 277 309 576])))) (let [tuple (SummingTuples.contiguousSumTuple 133015568 (Util.readInts "src/year2020/inputs/day9.txt"))] (assert (= 16107959 (+ (apply min tuple) (apply max tuple))))) + + // Day 10 + (let [adapters (Util.readInts "src/year2020/inputs/day10.txt")] + (.sort adapters Reflect.compare) + (adapters.unshift 0) + (adapters.push (+ 3 (last adapters))) + (let [diffs (Adapters.differences adapters) + dist (Adapters.distribution diffs)] + (assert (= 1998 (* (dictGet dist 1) (dictGet dist 3)))))) ) diff --git a/projects/aoc/src/year2020/Util.kiss b/projects/aoc/src/year2020/Util.kiss index 4a2e7059..dd16a5ba 100644 --- a/projects/aoc/src/year2020/Util.kiss +++ b/projects/aoc/src/year2020/Util.kiss @@ -17,7 +17,8 @@ (lambda [line] (< 0 line.length)))) (lambda [lines] (< 0 lines.length)))) -(defun readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt))) +// TODO won't need to specify type here if last is not a quickNth +(defun :kiss.List readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt))) (defun countChar [char str] (count (str.split "") (lambda [c] ?(= c char)))) \ No newline at end of file diff --git a/projects/aoc/src/year2020/inputs/day10.txt b/projects/aoc/src/year2020/inputs/day10.txt new file mode 100644 index 00000000..20133c95 --- /dev/null +++ b/projects/aoc/src/year2020/inputs/day10.txt @@ -0,0 +1,100 @@ +46 +63 +21 +115 +125 +35 +89 +17 +116 +90 +51 +66 +111 +142 +148 +60 +2 +50 +82 +20 +47 +24 +80 +101 +103 +16 +34 +72 +145 +141 +124 +14 +123 +27 +62 +61 +95 +138 +29 +7 +149 +147 +104 +152 +22 +81 +11 +96 +97 +30 +41 +98 +59 +45 +88 +37 +10 +114 +110 +4 +56 +122 +139 +117 +108 +91 +36 +146 +131 +109 +31 +75 +70 +140 +38 +121 +3 +28 +118 +54 +107 +84 +15 +76 +71 +102 +130 +132 +87 +55 +129 +83 +23 +42 +69 +1 +77 +135 +128 +94