diff --git a/advent-of-code.org b/advent-of-code.org index c3b43e0..4586bed 100644 --- a/advent-of-code.org +++ b/advent-of-code.org @@ -1247,3 +1247,101 @@ How many total feet of ribbon should they order? #+RESULTS[c13958a4756029347386f7b48c44959c7b8e3dea]: : 3842356 +* Day 3: Perfectly Spherical Houses in a Vacuum + +Santa is delivering presents to an infinite two-dimensional grid of +houses. + +He begins by delivering a present to the house at his starting +location, and then an elf at the North Pole calls him via radio and +tells him where to move next. Moves are always exactly one house to +the north (=^=), south (=v=), east (=>=), or west (=<=). After each +move, he delivers another present to the house at his new location. + +However, the elf back at the north pole has had a little too much +eggnog, and so his directions are a little off, and Santa ends up +visiting some houses more than once. How many houses receive *at least +one present*? + +For example: + +- =>= delivers presents to =2= houses: one at the starting location, + and one to the east. +- =^>v<= delivers presents to =4= houses in a square, including twice + to the house at his starting/ending location. +- =^v^v^v^v^v= delivers a bunch of presents to some very lucky + children at only =2= houses. + +---------------------------------------------------------------------- + +:HIDDEN: +#+name: 3-input +#+BEGIN_EXAMPLE + >^^v^<>v<<>v^^^^^><^<<^vv>>>^<<^>><>>><>v<><>^^<^^^<><>>vv>vv>v<<^>v<>^>vv>><>^v<<<v^>>>vv>v^^^<^^<>>v<^^v<>^<<>^>><^<>>><><>v<<<><><>v><<>^^^^v>>^>^^^v^^^^^v<><^v><<><^v^>v<<>^<>^^v^<>vv>^^<^<>^v<><^><><><<<<>^vv^>^vvvvv><><^v<<^<^^v^<>^>^^^v^>v<><^vv<<^<>v<>^^>^^>v^>^<<<v><^v>^>>v>>>>^v^^>v^>^vv^>vv^^v<<^<^^<>v>vv^v>><>>>v^>^>^^v<>^^vv>v^<v<<>^vvvv><<^<>>^v^>>^v<^<>>v^<>>v<>>v^^^><^>>vvvv>^v<^><<>>^<>^>vv>>^^>v^^^><^<<^^v>v<^<<>v>^^vvv^v^>v^<>^^<>v^v>v>vvv>^^v<>v>>^<>><>v>v^<^v<>>^>><>^vvv^>>vvv<>v>v>^>>v<<>^<>^<>>>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><^<^^vv^^>v^<>^^^>>><^^v>v>^<<>^vvv^>^^^>>v>^v><<><<>v<^<<>^><>^>vv>^^^v<<^v^vvv^^>^vv^<^>^>^^v>v^>^<<><<^>v>>vv^vv>>^<<^<^^<^^>v^^^<^<>^<>>^v<^vvv^^v^<><^>>>>>v><><<<>vv<^v>><<>vvv<><v^^>>^>^v>><><^^v<>><>>v^>^<<<>><><^^<>>v<><^vv<^v>^<<<>^<><^>><<>^>v>^^^v>>^<^^v>^><<><>>^>>^<^v<>^>^>vv>^vvv<^>^<<^^<>^^^^vvv<>^vv^^<^>>><>v^<><^<<^>v^^v<>>^vv<>v^^<>>v^vvvvv<>>><^>vv>v^v^^^><^>^^^^v<><^v<<>v^>v>>vv<<>^vvv>^^vv^><>>^>>^>v><>>^^v>^>^>>>^>v<^v>v>^<^^^^^>>v>v<<^>^^^>><<^><>v<>^^^vv<>^^>><<^^>v>vv>vv>v^>^v>v^^<>>><>v><>vvv^^v>^^>^vvvv^>^<>^vvvv>>><>^<^vv<>^v<^v<>^vvv<<>>>^><^^^<^^v^>v<>v^v><>>>^vvv><^vv>v^<^<^v>>v^^>^vvv^v<^<>>^<>>>^^<><^^vv<>^vv^<>>>>^^<<^^<>vv^^><>^^^^v<><><>vvv>^v^>>vv<<^v<<>>^><^>>>^<^<^^>vv^<<^<>>^^><><^^>v<^v^vv>><^^<<^>>v>v<^^^<^><^^vv>^vv<^v><^<><^^^>>^<><^>>>v^>>>>v<><^^>v<^<^>>^>vv>^^v^v^<<<<^v^><<^<><<<><<<>v>>vv><<^<^<>^^^^<>v<<<vv<>vv^^^>><>vv^><>>^vv<<><^^vv<>v^>>^<<>^v< +#+END_EXAMPLE +:END: + +#+BEGIN_SRC emacs-lisp :var input=3-input :exports both + (defun day3/make-coord (x y) + (cons x y)) + + (defun day3/coord-x (coord) + (car coord)) + + (defun day3/coord-y (coord) + (cdr coord)) + + (defun day3/add-coords (a b) + (day3/make-coord + (+ (day3/coord-x a) + (day3/coord-x b)) + (+ (day3/coord-y a) + (day3/coord-y b)))) + + (defun day3/arrow-to-coord (arrow) + (cond ((eq arrow ?>) (day3/make-coord 1 0)) + ((eq arrow ?<) (day3/make-coord -1 0)) + ((eq arrow ?^) (day3/make-coord 0 1)) + ((eq arrow ?v) (day3/make-coord 0 -1)) + (t (day3/make-coord 0 0)))) + + (defun day3/make-map () + (make-hash-table :test #'equal)) + + (defun day3/map-visit (map coord) + (puthash coord + (+ 1 (gethash coord map 0)) + map)) + + (defun day3/deliver (directions) + (let ((map (day3/make-map))) + (day3/map-visit map (day3/make-coord 0 0)) + (cdr (seq-reduce + (lambda (acc next) + (let* ((loc (car acc)) + (map (cdr acc)) + (next-loc (day3/add-coords loc + (day3/arrow-to-coord next)))) + (day3/map-visit map next-loc) + (cons next-loc map))) + directions + (cons (day3/make-coord 0 0) + map))))) + + (defun day3/map-count-visited (map) + (hash-table-count map)) + + (ert-deftest day3/map-count-visited () + (should (eq 2 (day3/map-count-visited + (day3/deliver ">")))) + (should (eq 4 (day3/map-count-visited + (day3/deliver "^>v<")))) + (should (eq 2 (day3/map-count-visited + (day3/deliver "^v^v^v^v^v"))))) + + (day3/map-count-visited + (day3/deliver input)) +#+END_SRC + +#+RESULTS[de0154066fa58bb3f337d9fc6135a1e7620998c1]: +: 2592