From 4074582f94e1a0cae4b5f2b81256a1e94d95b98e Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 4 Dec 2020 14:51:31 -0700 Subject: [PATCH] Refactor day 1 stuff to SummingTuples --- .../advent-of-code/src/year2020/Solutions.hx | 1 + .../advent-of-code/src/year2020/Solutions.kiss | 8 ++++---- .../src/year2020/SummingTuples.hx | 6 ++++++ .../src/year2020/SummingTuples.kiss | 17 +++++++++++++++++ projects/advent-of-code/src/year2020/Util.hx | 2 ++ projects/advent-of-code/src/year2020/Util.kiss | 2 +- 6 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 projects/advent-of-code/src/year2020/SummingTuples.hx create mode 100644 projects/advent-of-code/src/year2020/SummingTuples.kiss diff --git a/projects/advent-of-code/src/year2020/Solutions.hx b/projects/advent-of-code/src/year2020/Solutions.hx index d0e572bd..f1a70683 100644 --- a/projects/advent-of-code/src/year2020/Solutions.hx +++ b/projects/advent-of-code/src/year2020/Solutions.hx @@ -4,6 +4,7 @@ import haxe.ds.Map; import StringTools; import kiss.Prelude; import year2020.Util; +import year2020.SummingTuples; import year2020.Passwords; import year2020.Toboggan; diff --git a/projects/advent-of-code/src/year2020/Solutions.kiss b/projects/advent-of-code/src/year2020/Solutions.kiss index ee86db18..b8ee96ef 100644 --- a/projects/advent-of-code/src/year2020/Solutions.kiss +++ b/projects/advent-of-code/src/year2020/Solutions.kiss @@ -1,13 +1,13 @@ (defun main [] // Day 1 - (let [p (pairWithSum 2020 [1721 979 366 299 675 1456])] + (let [p (SummingTuples.pairWithSum 2020 [1721 979 366 299 675 1456])] (assert (and (has p 1721) (has p 299)) "pairWithSum is broken")) - (let [[a b] (pairWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))] + (let [[a b] (SummingTuples.pairWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))] (assert (= 545379 (* a b)) "pairWithSum is broken")) - (let [t (trioWithSum 2020 [1721 979 366 299 675 1456])] + (let [t (SummingTuples.trioWithSum 2020 [1721 979 366 299 675 1456])] (assert (and (has t 675) (has t 366) (has t 979)) "trioWithSum is broken")) - (let [[a b c] (trioWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))] + (let [[a b c] (SummingTuples.trioWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))] (assert (= 257778836 (* a b c)) "trioWithSum is broken")) diff --git a/projects/advent-of-code/src/year2020/SummingTuples.hx b/projects/advent-of-code/src/year2020/SummingTuples.hx new file mode 100644 index 00000000..57e227dd --- /dev/null +++ b/projects/advent-of-code/src/year2020/SummingTuples.hx @@ -0,0 +1,6 @@ +package year2020; + +import kiss.Prelude; + +@:build(kiss.Kiss.build("src/year2020/SummingTuples.kiss")) +class SummingTuples {} diff --git a/projects/advent-of-code/src/year2020/SummingTuples.kiss b/projects/advent-of-code/src/year2020/SummingTuples.kiss new file mode 100644 index 00000000..d982d809 --- /dev/null +++ b/projects/advent-of-code/src/year2020/SummingTuples.kiss @@ -0,0 +1,17 @@ +(defun :kiss.List pairWithSum [sum :kiss.List numbers] + // Put the numbers in a map for random access. This gives an O(n) solution + (deflocal :Map numbersMap (new Map)) + (doFor number numbers + (dict-set numbersMap number (- sum number)) + (let [requiredForPair (dict-get numbersMap number)] + (when (numbersMap.exists requiredForPair) + (return [number requiredForPair])))) + null) + +(defun :kiss.List trioWithSum [sum :kiss.List numbers] + (doFor number numbers + (let [requiredForTrio (- sum number) + pairThatSatisfies (pairWithSum requiredForTrio numbers)] + (when pairThatSatisfies + (return [number (nth pairThatSatisfies 0) (nth pairThatSatisfies 1)])))) + null) \ No newline at end of file diff --git a/projects/advent-of-code/src/year2020/Util.hx b/projects/advent-of-code/src/year2020/Util.hx index 72cc42bb..e91ef0ee 100644 --- a/projects/advent-of-code/src/year2020/Util.hx +++ b/projects/advent-of-code/src/year2020/Util.hx @@ -3,5 +3,7 @@ package year2020; import sys.io.File; import kiss.Prelude; +using StringTools; + @:build(kiss.Kiss.build("src/year2020/Util.kiss")) class Util {} diff --git a/projects/advent-of-code/src/year2020/Util.kiss b/projects/advent-of-code/src/year2020/Util.kiss index 3be4f8d2..872f8c94 100644 --- a/projects/advent-of-code/src/year2020/Util.kiss +++ b/projects/advent-of-code/src/year2020/Util.kiss @@ -2,7 +2,7 @@ (.filter (.map // TODO implement escape sequences in kiss string literals - (.split (File.getContent file) #|"\n"|#) + (.split (.replace (File.getContent file) #|"\r"|# "") #|"\n"|#) StringTools.trim) (lambda [l] (< 0 l.length))))