feat(aoc2019): Add solution for day3/2

This commit is contained in:
Vincent Ambo 2019-12-16 00:43:43 +00:00
parent e8b184adcc
commit cc51fb6ce7

View file

@ -1,8 +1,7 @@
;; -*- lexical-binding: t; -*- ;; -*- lexical-binding: t; -*-
;; Advent of Code 2019 - Day 3 ;; Advent of Code 2019 - Day 3
;;
;; Note: Input was pre-processed with some Emacs shortcuts. (require 'cl-lib)
(require 'cl)
(require 'dash) (require 'dash)
(require 'ht) (require 'ht)
(require 's) (require 's)
@ -37,22 +36,29 @@
(next (day3/move x y point))) (next (day3/move x y point)))
(-concat next acc))) (-concat next acc)))
'((0 . 0)) wire))) '((0 . 0)) wire)))
(-map (lambda (p) (ht-set! points p t)) point-list) (-map (-lambda ((s . p)) (ht-set! points p s))
(-zip (reverse (number-sequence 0 (- (length point-list) 1))) point-list))
(ht-remove! points '(0 . 0)) (ht-remove! points '(0 . 0))
points)) points))
(defun day3/closest-intersection (wire1 wire2) (defun day3/closest-intersection (crossed-points)
(let* ((wire1-points (day3/wire-points (wire-from wire1)))
(wire2-points (day3/wire-points (wire-from wire2)))
(crossed-points (-filter (lambda (p) (ht-contains? wire1-points p))
(ht-keys wire2-points))))
(car (-sort #'< (car (-sort #'<
(-map (-lambda ((x . y)) (-map (-lambda ((x . y))
(+ (abs x) (abs y))) (+ (abs x) (abs y)))
crossed-points))))) crossed-points))))
(message "Solution form day3/1: %d" (defun day3/minimum-steps (wire1 wire2 crossed)
(day3/closest-intersection day3/input/wire1 (car (-sort #'<
day3/input/wire2)) (-map (-lambda (p)
(+ (ht-get wire1 p) (ht-get wire2 p)))
crossed))))
;; Example:
(let* ((wire1-points (day3/wire-points (wire-from day3/input/wire1)))
(wire2-points (day3/wire-points (wire-from day3/input/wire2)))
(crossed-points (-filter (lambda (p) (ht-contains? wire1-points p))
(ht-keys wire2-points))))
(message "Solution for day3/1: %d" (day3/closest-intersection crossed-points))
(message "Solution for day3/2: %d" (day3/minimum-steps wire1-points
wire2-points
crossed-points)))