From cc51fb6ce794cee7b1af096c4ad1d49d84a9f88b Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 16 Dec 2019 00:43:43 +0000 Subject: feat(aoc2019): Add solution for day3/2 --- tools/aoc2019/solution-day3.el | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'tools') diff --git a/tools/aoc2019/solution-day3.el b/tools/aoc2019/solution-day3.el index c0d2eb5ee6..b7dfdd245f 100644 --- a/tools/aoc2019/solution-day3.el +++ b/tools/aoc2019/solution-day3.el @@ -1,8 +1,7 @@ ;; -*- lexical-binding: t; -*- ;; Advent of Code 2019 - Day 3 -;; -;; Note: Input was pre-processed with some Emacs shortcuts. -(require 'cl) + +(require 'cl-lib) (require 'dash) (require 'ht) (require 's) @@ -37,22 +36,29 @@ (next (day3/move x y point))) (-concat next acc))) '((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)) points)) -(defun day3/closest-intersection (wire1 wire2) - (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 #'< - (-map (-lambda ((x . y)) - (+ (abs x) (abs y))) - crossed-points))))) +(defun day3/closest-intersection (crossed-points) + (car (-sort #'< + (-map (-lambda ((x . y)) + (+ (abs x) (abs y))) + crossed-points)))) -(message "Solution form day3/1: %d" - (day3/closest-intersection day3/input/wire1 - day3/input/wire2)) +(defun day3/minimum-steps (wire1 wire2 crossed) + (car (-sort #'< + (-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))) -- cgit 1.4.1