Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
aoc3
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
;; ;;;;;;;;;; (defun read-file-as-lines (filename) "Read file into a list of lines" (with-open-file (in filename) (loop for line = (read-line in nil nil) while line collect line))) (defun delimiterp (c) (or (char= c #\Space) (char= c #\,))) ;; SO (defun str-split (string &key (delimiterp #'delimiterp)) "Split a string based on delimiterp" (loop :for beg = (position-if-not delimiterp string) :then (position-if-not delimiterp string :start (1+ end)) :for end = (and beg (position-if delimiterp string :start beg)) :when beg :collect (subseq string beg end) :while end)) (defun my-last (xs) (first (reverse xs))) ;; ;;;;;;;;;; (defun dir-tail (dir) (parse-integer (subseq dir 1))) (defun dir-head (dir) (string (char dir 0))) (defun move-up (y pos) (let ((x (first pos))) (loop for i from (+ 1 (my-last pos)) to (+ y (my-last pos)) collect (list x i)))) (defun move-down (y pos) (let ((x (first pos))) (loop for i from (- 1 (my-last pos)) to (- y (my-last pos)) collect (list x (- i))))) (defun move-right (x pos) (let ((y (my-last pos))) (loop for i from (+ 1 (first pos)) to (+ x (first pos)) collect (list i y)))) (defun move-left (x pos) (let ((y (my-last pos))) (loop for i from (- 1 (first pos)) to (- x (first pos)) collect (list (- i) y)))) (defun manhattan-distance (x1 y1 x2 y2) "calculate manhattan dist between (x1 y1) and (x2 y2)" (+ (abs (- x2 x1)) (abs (- y2 y1)))) ;; Load input (defvar input (read-file-as-lines "input/03.txt")) ;; Load input into paths (Uxx Dxx Rxx Lxx ..) (defvar paths (mapcar 'str-split input)) (defun map-dir (dir pos) "Very expensive I do not know how to do variable assignment inside a cond" (let ((op (dir-head dir)) (val (dir-tail dir))) (cond ((equalp "U" op) (values (move-up val pos) (last (move-up val pos)) )) ((equalp "D" op) (values (move-down val pos) (last (move-down val pos)) )) ((equalp "R" op) (values (move-right val pos) (last (move-right val pos)) )) ((equalp "L" op) (values (move-left val pos) (last (move-left val pos)) ))))) ;;; Stuck here !! ;;; I want to be able to take the list path (it looks like this ("U34" "D11" "R32") etc.) ;;; and map the function map-dir over it, however I need to also update the pos input ;;; for each time map-dir is called (defun map-path (path pos) (loop for dir in path collect (let (((mapped npos) (multiple-value-bind ('mapped 'npos) (map-dir dir npos) )) mapped))))
[
+
]
Show input
edit mode
|
history