Solve AOC day 9

This commit is contained in:
2020-12-10 17:56:34 -07:00
parent 1ae7bca8c2
commit 1b58b38806
5 changed files with 1033 additions and 0 deletions

View File

@@ -89,5 +89,14 @@
(bootCode.setBreakHandler (lambda [:BootCodeFix bootCodeFork] (when (= bootCodeFork.instructionPointer (bootCodeFork.instructionCount)) (print .accumulator (the BootCodeFix bootCodeFork)))))
(bootCode.addBreakPoint (bootCode.instructionCount))
(bootCode.run))
// Day 9
(assert (= 127 (XMAS.firstOffender 5
[35 20 15 25 47 40 62 55 65 95 102 117 150 182 127 219 299 277 309 576])))
(assert (= 133015568 (XMAS.firstOffender 25 (Util.readInts "src/year2020/inputs/day9.txt"))))
(assert (= "[15,25,47,40]" (.toString (SummingTuples.contiguousSumTuple 127
[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)))))
)

View File

@@ -15,3 +15,15 @@
(when pairThatSatisfies
(return [number (nth pairThatSatisfies 0) (nth pairThatSatisfies 1)]))))
null)
(defun contiguousSumTuple [sum :kiss.List<Int> numbers]
(doFor i (range numbers.length)
(deflocal &mut testSum (nth numbers i))
(doFor j (range (+ i 1) numbers.length)
(set testSum (+ testSum (nth numbers j)))
(cond
((= testSum sum)
(return (numbers.slice i (+ j 1))))
((> testSum sum)
(break)))))
null)

View File

@@ -0,0 +1,7 @@
package year2020;
import kiss.Prelude;
import year2020.SummingTuples;
@:build(kiss.Kiss.build("src/year2020/XMAS.kiss"))
class XMAS {}

View File

@@ -0,0 +1,5 @@
(defun firstOffender [preambleLength :kiss.List<Int> input]
(doFor idx (range preambleLength input.length)
(unless (SummingTuples.pairWithSum (nth input idx) (input.slice (- idx preambleLength) idx))
(return (nth input idx))))
(print null))

File diff suppressed because it is too large Load Diff