August 13, 2018

Advent of Code 2016 Day 1 - No Time for a Taxicab

Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further.

The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection.

There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination?

For example:

Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away. R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away. R5, L5, R5, R3 leaves you 12 blocks away.

How many blocks away is Easter Bunny HQ?

(ns day01
  (:require [cljs.test :refer-macros [deftest is run-tests]]
            [clojure.string  :as str]))

(defn manhattan-distance [u v]
  (reduce +
    (map (fn [[a b]] (Math/abs (- a b)))
         (map vector u v))))

(deftest day01-test
  (is (= 5 (day01 "R2, L3")))
  (is (= 2 (day01 "R2, R2, R2")))
  (is (= 12 (day01 "R5, L5, R5, R3"))))

(defn parse [s]
  (map rest (map #(re-matches #"([LR])(\d+)" %)
     (str/split s #", "))))

(defn go-north [l n]
  (vector (first l)
          (+ n (last l))))
(defn go-east [l n]
  (vector (+ n (first l))
          (last l)))
(defn go-south [l n]
  (vector (first l)
          (- (last l) n)))
(defn go-west [l n]
  (vector (- (first l) n)
          (last l)))

(defn step [directions]
  (loop [direction 1
         location [0 0]
         steps (parse directions)]
    (if (empty? steps)
        (if (= "R" (first (first steps)))
               (mod (inc direction) 4)
               (mod (dec direction) 4))
        (cond (= direction 1) (go-north location (js/parseInt (last (first steps))))
              (= direction 2) (go-east location (js/parseInt (last (first steps))))
              (= direction 3) (go-south location (js/parseInt (last (first steps))))
              (= direction 4) (go-west location (js/parseInt (last (first steps)))))
        (rest steps)))))

(step "R5, L5, R5, R3")

Part 2

Then, you notice the instructions continue on the back of the Recruiting Document. Easter Bunny HQ is actually at the first location you visit twice.

For example, if your instructions are R8, R4, R4, R8, the first location you visit twice is 4 blocks away, due East.

How many blocks away is the first location you visit twice?

Tags: coding exercises KLIPSE Clojure Advent of Code parsing