From 58ee9f48c4c90352bccdb1cc88d7b9ef923f9216 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 10 Jun 2024 12:26:20 -0600 Subject: AoC 2015.3 p1+2 --- perfectly-spherical-houses-in-a-vacuum/README.org | 41 +++++++++++ perfectly-spherical-houses-in-a-vacuum/input.txt | 1 + perfectly-spherical-houses-in-a-vacuum/main.scm | 16 +++++ .../pshiav-test.scm | 83 ++++++++++++++++++++++ perfectly-spherical-houses-in-a-vacuum/pshiav.scm | 54 ++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 perfectly-spherical-houses-in-a-vacuum/README.org create mode 100644 perfectly-spherical-houses-in-a-vacuum/input.txt create mode 100644 perfectly-spherical-houses-in-a-vacuum/main.scm create mode 100644 perfectly-spherical-houses-in-a-vacuum/pshiav-test.scm create mode 100644 perfectly-spherical-houses-in-a-vacuum/pshiav.scm diff --git a/perfectly-spherical-houses-in-a-vacuum/README.org b/perfectly-spherical-houses-in-a-vacuum/README.org new file mode 100644 index 0000000..39fd3d6 --- /dev/null +++ b/perfectly-spherical-houses-in-a-vacuum/README.org @@ -0,0 +1,41 @@ +See: https://adventofcode.com/2015/day/3 +** Part 1 +*** Purpose +Given a list of directional arrows: + +#+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>>^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<^^><^v^^v<>^^>^><^>v^^^^v<^<vv^^>v^vv>v><>>vv>^<^>v^v^^v>^>vv^>v^v>^^v<<^>^^<<>^><^v>>>vv^>^^>vvvv>>v<^^>>>v^<><^<^^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< \ No newline at end of file diff --git a/perfectly-spherical-houses-in-a-vacuum/main.scm b/perfectly-spherical-houses-in-a-vacuum/main.scm new file mode 100644 index 0000000..9ce9361 --- /dev/null +++ b/perfectly-spherical-houses-in-a-vacuum/main.scm @@ -0,0 +1,16 @@ +;; -*- compile-command: "guile -L . -e main -s main.scm < input.txt"; -*- +(use-modules (pshiav) + ((ice-9 rdelim)) + (ice-9 binary-ports)) + + +(define (stdin-to-str) + (let loop ((result "")) + (let ((line (read-line))) + (if (eof-object? line) + result + (loop (string-append result line "\n")))))) + +(define (main args) + (display (pshiav (stdin-to-str) (string->number (cadr args)))) + (newline)) diff --git a/perfectly-spherical-houses-in-a-vacuum/pshiav-test.scm b/perfectly-spherical-houses-in-a-vacuum/pshiav-test.scm new file mode 100644 index 0000000..a27dec4 --- /dev/null +++ b/perfectly-spherical-houses-in-a-vacuum/pshiav-test.scm @@ -0,0 +1,83 @@ +;; -*- compile-command: "guile -L . pshiav-test.scm"; -*- +(use-modules (srfi srfi-64) + (pshiav)) + +(test-begin "harness") + + +(define coordinate-accumulator + (add-coordinates)) + +(test-equal "don't move" + '((0 . 0) (0 . 0)) + (coordinate-accumulator '(0 . 0))) + +(set! coordinate-accumulator + (add-coordinates)) + +(test-equal "move up" + '((0 . 1) (0 . 0)) + (coordinate-accumulator '(0 . 1))) + +(set! coordinate-accumulator + (add-coordinates)) + +(test-equal "move up twice" + '((0 . 2) (0 . 1) (0 . 0)) + (begin (coordinate-accumulator '(0 . 1)) + (coordinate-accumulator '(0 . 1)))) + +(test-equal "up maps to +y" + '(0 . 1) + (direction->pair #\^)) + +(test-equal "right maps to +x" + '(1 . 0) + (direction->pair #\>)) + +(test-equal "down maps to -y" + '(0 . -1) + (direction->pair #\v)) + +(test-equal "left maps to -x" + '(-1 . 0) + (direction->pair #\<)) + +(test-equal "? maps to no change" + '(0 . 0) + (direction->pair #\?)) + +(test-equal "empty input visits one place" + 1 + (pshiav "" 1)) + +(test-equal "down once visits two places" + 2 + (pshiav "v" 1)) + +(test-equal "down twice visits three places" + 3 + (pshiav "vv" 1)) + +(test-equal "down twice, up once visits three places" + 3 + (pshiav "vv^" 1)) + +(test-equal "up down visits three with two visitors" + 3 + (pshiav "^v" 2)) + +(test-equal "up right down left visits three with two visitors" + 3 + (pshiav "^>v<" 2)) + +(test-equal "opposite instructions maximizes visits with two" + 11 + (pshiav "^v^v^v^v^v" 2)) + +(test-equal "opposite instructions maximizes visits with three" + 16 + (pshiav "^v>^v>^v>^v>^v>" 3)) + + +(test-end "harness") diff --git a/perfectly-spherical-houses-in-a-vacuum/pshiav.scm b/perfectly-spherical-houses-in-a-vacuum/pshiav.scm new file mode 100644 index 0000000..d117cce --- /dev/null +++ b/perfectly-spherical-houses-in-a-vacuum/pshiav.scm @@ -0,0 +1,54 @@ +(define-module (pshiav) + #:use-module (srfi srfi-1) + #:export (pshiav + add-coordinates + direction->pair)) + +(define (pshiav str num) + "Given string of cardinal directions STR, +calculates the number of visited places on +a grid. + +NUM specifies the number of visitors. Will +always be at least one." + (set! num (max num 1)) + (let ((visitors (map (lambda (x) + (add-coordinates)) + (iota num)))) + (let loop ((i 0) (lst (map direction->pair + (string->list str)))) + (if (null? lst) + (length (delete-duplicates + (apply append (map (lambda (visitor) + (visitor '(0 . 0))) + visitors)))) + (begin + ((list-ref visitors i) (car lst)) + (loop (modulo (1+ i) (length visitors)) + (cdr lst))))))) + +(define (add-coordinates) + (let ((hist '((0 . 0)))) + (lambda dir + "Given cons pair DIR, increments the previous +position, and returns HIST, a history of visited +positions." + (set! hist (cons + (cons (+ (caar dir) + (caar hist)) + (+ (cdar dir) + (cdar hist))) + hist)) + hist))) + + +(define (direction->pair char) + "Given a character representing one of the four +cardinal directions, return a pair representing +a change in coordinates." + (cond + ((char=? char #\^) '(0 . 1)) + ((char=? char #\>) '(1 . 0)) + ((char=? char #\v) '(0 . -1)) + ((char=? char #\<) '(-1 . 0)) + (#t '(0 . 0)))) -- cgit v1.2.3