aoc 2018 day 1

This commit is contained in:
Angad Singh
2021-07-12 21:32:22 +05:30
parent 2d8f4cfee7
commit befaadf2d5
11 changed files with 2150 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package year2018;
import haxe.ds.Map;
import haxe.Int64;
import StringTools;
import kiss.Prelude;
import kiss.Stream;
import Util;
@:build(kiss.Kiss.build())
class Solutions {}

View File

@@ -0,0 +1,165 @@
(defmacro day [num &rest body]
`(when (<= 0 (days.indexOf ,num))
(print (+ "day " (Std.string ,num)))
,@body))
(defmacro dayTodo [num]
`(day ,num (print "TODO")))
(defun run [:kiss.List<Int> days]
(day 1
(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] (SummingTuples.pairWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))]
(assert (= 545379 (* a b)) "pairWithSum is broken"))
(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] (SummingTuples.trioWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))]
(assert (= 257778836 (* a b c))
"trioWithSum is broken")))
(day 2
(assert (Passwords.validateInputLine "1-3 a: abcde" Passwords.parsePasswordCheck1))
(assert !(Passwords.validateInputLine "1-3 b: cdefg" Passwords.parsePasswordCheck1))
(assert (Passwords.validateInputLine "2-9 c: ccccccccc" Passwords.parsePasswordCheck1))
(assert (= 655 (count (map (Util.readLines "src/year2020/inputs/day2-1.txt") (.bind Passwords.validateInputLine _ Passwords.parsePasswordCheck1)) (lambda [v] v))))
(assert (Passwords.validateInputLine "1-3 a: abcde" Passwords.parsePasswordCheck2))
(assert !(Passwords.validateInputLine "1-3 b: cdefg" Passwords.parsePasswordCheck2))
(assert !(Passwords.validateInputLine "2-9 c: ccccccccc" Passwords.parsePasswordCheck2))
(assert (= 673 (count (map (Util.readLines "src/year2020/inputs/day2-1.txt") (.bind Passwords.validateInputLine _ Passwords.parsePasswordCheck2)) (lambda [v] v)))))
(day 3
(deflocal exampleHillTile [
"..##......."
"#...#...#.."
".#....#..#."
"..#.#...#.#"
".#...##..#."
"..#.##....."
".#.#.#....#"
".#........#"
"#.##...#..."
"#...##....#"
".#..#...#.#"])
(assert (= "..#.##.####" (Toboggan.pathString exampleHillTile 0 0 3 1)))
(assert (= 2 (Toboggan.pathTrees exampleHillTile 0 0 1 1)))
(assert (= 3 (Toboggan.pathTrees exampleHillTile 0 0 5 1)))
(assert (= 4 (Toboggan.pathTrees exampleHillTile 0 0 7 1)))
(assert (= 2 (Toboggan.pathTrees exampleHillTile 0 0 1 2)))
(assert (= 289 (Util.countChar "#" (Toboggan.pathString (Util.readLines "src/year2020/inputs/day3-1.txt") 0 0 3 1))))
(assert (= 5522401584 (let [hillTile (Util.readLines "src/year2020/inputs/day3-1.txt")]
(apply * (for args [[0 0 1 1] [0 0 3 1] [0 0 5 1] [0 0 7 1] [0 0 1 2]] (apply (Toboggan.pathTrees.bind hillTile) args)))))))
(day 4
(assert (= 2 (Passports.countValidPassports (Stream.fromFile "src/year2020/inputs/day4-example.txt"))))
(assert (= 250 (Passports.countValidPassports (Stream.fromFile "src/year2020/inputs/day4-1.txt"))))
(assert (= 158 (Passports.countValidPassports (Stream.fromFile "src/year2020/inputs/day4-1.txt") "strict"))))
(day 5
(assert (= 567 (Seating.seatId "BFFFBBFRRR")))
(let [:kiss.List<Int> seatIds (map (Util.readLines "src/year2020/inputs/day5-1.txt") Seating.seatId)]
(seatIds.sort (lambda [a b] (- a b)))
(assert (= 947 (nth seatIds -1)))
(deflocal &mut lastId -1)
(deflocal &mut myId -1)
(doFor id seatIds
(when (and (<= 0 lastId) !(= lastId (- id 1)))
(set myId (- id 1))
(break))
(set lastId id))
(assert (= 636 myId))))
(day 6
(assert (= 6 (Customs.countAnyYes ["abcx" "abcy" "abcz"])))
(assert (= 6683 (apply + (map (Util.readParagraphLines "src/year2020/inputs/day6-1.txt") Customs.countAnyYes))))
(assert (= 6 (apply + (map (Util.readParagraphLines "src/year2020/inputs/day6-example2.txt") Customs.countAllYes))))
(assert (= 3122 (apply + (map (Util.readParagraphLines "src/year2020/inputs/day6-1.txt") Customs.countAllYes)))))
(day 7
(deflocal parentMap (new ParentMap))
(deflocal childMap (new ChildMap))
(doFor line (Util.readLines "src/year2020/inputs/day7.txt")
(Bags.parseRule line parentMap childMap))
(deflocal :Map<String,Bool> shinyGoldParents (new Map))
(Bags.findIndirectContainers "shiny gold" childMap shinyGoldParents)
(assert (= 172 (count shinyGoldParents)))
(assert (= 39645 (Bags.totalChildBags "shiny gold" parentMap))))
(day 8
(let [example (new BootCodeExample)]
(example.setBreakHandler (lambda [example] (assert (= 5 .accumulator example))))
(example.run))
(let [bootCode (new BootCodeReal)]
(bootCode.setBreakHandler (lambda [bootCode] (assert (= 2058 bootCode.accumulator))))
(bootCode.run))
/* (print "BootCodeFixExample")
(let [bootCode (new BootCodeFixExample)]
(bootCode.setBreakHandler
(lambda [bootCodeFork]
(if (= bootCodeFork.instructionPointer (bootCodeFork.instructionCount))
(print (+ "answer could be " (Std.string bootCodeFork.accumulator) " forked at " (Std.string bootCodeFork.forkedAt)))
(print "hit an infinite loop"))))
(bootCode.addBreakPoint (bootCode.instructionCount))
(bootCode.run))
(print "BootCodeFixReal")
(let [bootCode (new BootCodeFix)]
(bootCode.setBreakHandler
(lambda [bootCodeFork]
(if (= bootCodeFork.instructionPointer (bootCodeFork.instructionCount))
(print (+ "answer could be " (Std.string bootCodeFork.accumulator) " forked at " (Std.string (+ 1 bootCodeFork.forkedAt))))
(print "hit an infinite loop"))))
(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))))))
(day 10
(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)
dist (Adapters.distribution diffs)]
(assert (= 1998 (* (dictGet dist 1) (dictGet dist 3)))))
(let [memoized (memoize Adapters.arrangementCount)]
(set Adapters.arrangementCount #|cast memoized|#))
(assert (= "347250213298688" (Int64.toStr (Adapters.arrangementCount adapters 0))))))
(day 11
(let [example (new FerrySimExample)
real (new FerrySimReal)]
(example.run)
(example.simulate)
(assert (= 71 (example.countFullSeats)))
(real.run)
(real.fullSimulate)
(assert (= 2329 (real.countFullSeats)))))
(day 12
(let [script (new EvasionScript)]
(script.run)
(assert (= 441 (+ (Math.abs script.x) (Math.abs script.y))))))
(dayTodo 13)
(dayTodo 14)
(dayTodo 15)
(dayTodo 16)
(dayTodo 17)
(dayTodo 18)
(dayTodo 19)
(dayTodo 20)
(dayTodo 21)
(dayTodo 22)
(dayTodo 23)
(dayTodo 24)
(dayTodo 25))

View File

@@ -0,0 +1,951 @@
-10
-12
+1
+14
+11
-19
-4
+10
+10
-12
-13
-10
-8
+11
+3
-10
+8
+5
-14
+7
+12
+12
+14
-1
+17
-5
+9
-15
+8
-16
+9
+6
+17
-11
+19
+11
-9
-1
-8
-16
-5
+6
+2
+6
+12
+15
+16
-6
-8
-5
+11
-9
+19
+19
-5
-12
-17
-20
+9
-6
-2
+20
+15
-8
+15
+19
-5
-17
-9
+1
-9
-11
-19
-1
-15
+18
-4
+19
+3
+21
+5
-1
+8
+9
+9
-16
+17
-15
+18
+14
+8
-13
-2
-15
+13
+19
-13
+5
+16
+16
+7
+8
-19
+3
-12
+18
-16
-19
+8
-16
+20
+15
-5
-17
+15
+14
-2
+18
-6
+5
+16
+13
-8
-9
-1
+13
+18
-2
+16
-4
+19
+6
+14
-6
-16
-10
-5
-15
+11
+10
+18
+8
-2
+15
-9
+7
+10
+9
-6
+1
-17
-12
+19
+14
-9
-18
+20
+12
+10
+14
+6
+9
-18
+4
+15
+15
+3
+9
-4
-19
+8
+16
+19
-12
-1
+16
+11
+13
-8
+4
+18
+10
-11
+18
-8
+19
-4
-5
-12
-3
-10
-11
-18
-7
+3
+14
+18
+2
-7
-12
+14
+12
+13
-1
+13
-1
+15
+13
-1
-17
-4
+17
-3
+13
+5
-19
+17
+10
+6
-15
+11
+16
+11
+20
+19
+5
-11
-2
-12
-6
-11
-14
+5
+10
-23
+14
-11
+22
-1
-14
-35
-23
+11
+4
+10
-6
+19
-5
+4
-13
-7
+11
-24
-17
+9
-17
+11
+2
-4
-10
-24
+14
+15
+19
+7
-2
+11
-13
-8
-8
-14
-17
+6
+8
-16
-18
+1
-21
-14
+8
-2
+20
-19
+9
-15
-6
-7
+4
-3
-8
+16
+15
-26
+18
-22
-16
-3
-17
-18
+4
+5
+3
-1
+3
+2
+3
+7
-5
+20
-11
+15
-6
-19
-24
-6
-20
+13
-4
-18
+15
+9
+16
+12
-5
+16
+9
-4
-14
-9
-19
-5
-16
-16
+5
-4
+12
-15
-15
-14
+4
-18
-12
-4
+6
+9
-12
+14
+9
-6
+3
+8
+4
+7
-14
-15
-18
+12
-19
-14
-15
-16
+15
-8
+14
-4
-21
+7
-12
-2
-10
-6
+14
+14
-9
-14
-16
+2
+7
+5
-15
-15
+19
-16
-5
-16
-19
+9
-27
-8
+6
+1
-14
-9
-50
-9
-21
+5
-20
-19
+6
-11
+6
+1
-17
-14
-2
+8
-4
+16
+1
-3
-9
+20
+15
-22
-3
-13
-12
-25
+5
-13
-2
-8
-11
+17
+3
-7
+13
+1
+18
-13
+1
+7
-15
-43
-6
-10
-9
-20
-7
-8
-15
-9
+13
-9
+24
+13
+5
-8
-15
-4
-4
-4
-14
-21
+16
+15
-14
-20
-20
-15
-19
-41
+16
+3
+8
-22
-30
+8
+40
+15
+24
-7
+22
+10
+15
+9
+28
+14
+2
+14
+6
+17
+1
+30
+5
-2
+46
+24
+46
-3
-7
+35
+16
+13
-21
+28
+13
-25
+180
+25
+5
-9
+23
-5
-2
+13
-12
+2
-25
-5
+18
-24
+45
+17
-7
+22
-152
-11
+4
+50
+129
-7
+22
+109
-56
+80
+29
-75060
-2
-14
+7
-17
-5
+6
-3
+18
+19
-14
-12
-6
+9
-5
-13
-6
-3
-15
+13
+3
-18
-11
+16
-2
+10
-14
-7
+19
-20
+15
+10
+12
+10
+16
-11
+14
-5
+14
+11
-5
+1
-13
+1
+18
-10
+25
+13
-17
+15
-18
+1
-6
-12
+5
-7
+18
-6
-18
-17
-5
-15
-1
-15
+1
-12
+9
+16
+8
+5
+1
+9
+18
-13
-30
-27
-19
+2
-16
-19
-10
-18
+21
+18
-5
+14
+9
-6
+2
-16
-11
+1
+18
-4
+19
+7
+6
-8
+25
-50
+6
-21
+6
-2
+14
-11
+10
-9
+16
+25
-61
-17
-14
+13
+3
-14
-4
-1
+9
-1
-4
-5
+7
-15
+7
+16
+6
-2
-11
-11
-13
+11
-12
-13
-8
+3
-13
+16
-1
-14
+19
+4
+10
+12
+26
+6
+4
+24
-31
+9
-10
-23
-13
+3
-18
-9
-6
+1
-2
+15
-10
-17
-17
+15
+9
+4
+19
-4
+12
+11
-25
+16
+4
-2
+39
+49
+9
+96
+4
+19
+3
+11
+25
+14
+11
-5
-26
-8
-16
+11
+2
+2
-1
-10
-45
-160
-31
-3
-43
-18
+19
+3
+4
+11
-16
-19
-22
+6
+2
+10
-11
-17
+7
+1
+15
+17
-19
+6
-26
-7
-9
+19
+2
+6
-22
+2
-8
+5
+7
+9
+8
-16
+2
-19
-19
-3
+16
+5
+4
-18
-11
-19
+16
-15
+7
-1
-9
-19
+11
-17
-5
+7
-11
+16
-19
+5
+21
-2
+12
+14
-7
+18
-8
+10
-11
-17
+3
-13
+19
-3
+10
+17
+17
-18
+7
-2
-2
+12
-6
+19
-20
-15
-11
-27
+8
+18
+9
-1
-2
+19
+7
+12
+32
+14
+13
-9
-14
+20
+18
-4
-49
-6
-10
-7
-4
-9
-6
-37
-21
-2
+5
+12
+4
-33
-4
-13
-4
+13
-19
-7
-16
-1
-1
+8
-14
-15
+11
-10
-14
-22
-11
+10
-3
+7
+9
-15
+16
-7
-1
+2
+20
+11
-1
+6
-2
+3
+3
+2
-20
-10
+11
-10
+13
-1
+20
+7
+7
+5
+1
+4
-13
+15
-3
+2
-4
-3
-1
+13
-15
-14
-2
-6
+3
+13
-19
-11
+8
-21
-2
-23
-18
+20
+7
+6
-28
+7
-33
+13
-16
-18
-10
-18
+13
+8
-14
-13
-2
+4
-14
-7
-18
-20
-4
-5
+8
-4
+16
+21
+20
-8
-14
-2
-14
-22
+14
+36
+32
-28
+1
-21
+8
+14
+75784