LOADING: An error occurred. Update Chrome, try Firefox, or visit this post for more details.

⚠️Reddit changed how removals work, which breaks Reveddit's website. Install the extension to track removed content:Add to chromeAdd to firefoxWhat changed?
✖︎
about reveddit
⚙F.A.Q.add-ons
r/
status
copy sharelink
[+] show filters
112
SOLUTION MEGATHREAD-🎄- 2021 Day 2 Solutions -🎄-(self.adventofcode)
submitted 4 years, 1 month ago* (edited 4 minutes after) by daggerdragon to /r/adventofcode (134.8k)
1555 commentsredditother-discussionssubreddit-indexmessage modsop-focus

--- Day 2: Dive! ---


Post your code solution in this megathread.

  • Include what language(s)...

... view full text

since 4 years, 1 month ago
5 of 5

Tip Reveddit Real-Time can notify you when your content is removed.

your account history
(check your username's removed content. why?)
Tip Check if your account has any removed comments.
view my removed comments
you are viewing a single comment's thread.
view all comments
[–]rabuf5 points4 years, 1 month ago

Common Lisp

This is slightly cleaned up from my initial version, in particular reading the input was simplified to just use Common Lisp's read function. I was reading each line, initially, splitting them, and then had more complex conditional logic. read mostly does what you want, most alphanumeric (+ some symbols) strings become symbols, numbers become numbers.

(defun follow-directions (directions)
  (loop
     with position = #C(0 0)
     for (dir dist) in directions
     finally (return (* (realpart position) (imagpart position)))
     do (case dir
          (up (decf position dist))
          (down (incf position dist))
          (forward (incf position (complex 0 dist))))))

(defun follow-directions-aim (directions)
  (loop
     with position = #C(0 0)
     with aim = 0
     for (dir dist) in directions
     finally (return (* (realpart position) (imagpart position)))
     do (case dir
          (up (decf aim dist))
          (down (incf aim dist))
          (forward (incf position (complex (* aim dist) dist))))))
permalinkhide replies (1)author-focusas-ofpreserve
[–][deleted]1 point4 years, 1 month ago
[deleted] by user
(check your username's removed content. why?)
parenthide replies (1)as-of
[–]rabuf2 points4 years, 1 month ago* (edited 4 minutes after)

loop does a destructuring bind when you use in. If you just want a single element then you'll do (with some typed in values, expect typos, not actually executed in a REPL):

> (loop for i in '(1 2 3)) do (print i))
1
2
3

For a more complex structure (in this case I'll imitate my directions list):

> (loop for i in '((up 1) (down 2) (forward 3))
        do (print i))
(up 1)
(down 2)
(forward 3)

Instead of getting those values and having to break them out later, I just used destructuring bind to get both parts:

> (loop for (dir dist) in '((up 1) (down 2) (forward 3))
        do (format t "~A by ~A~%" dir dist))
UP by 1
DOWN by 2
FORWARD by 3

The destructuring bind is the same (or at least equivalent) as with the actual special form destructuring-bind:

(destructuring-bind (dir dist) '(up 1)
    ...)

But it's wrapped up in the loop structure itself so I don't have to either do it myself later or use accessors (like first/car or second/cadr) which would make the rest of it uglier (in my opinion, especially how often dist is used).

permalinkparentcontexthide replies (1)author-focusas-ofpreserve
[–][deleted]1 point4 years, 1 month ago
[deleted] by user
(check your username's removed content. why?)
parenthide replies (1)as-of
[–]rabuf2 points4 years, 1 month ago* (edited 19 minutes after)

read will parse strings that are alphanumeric (and some other symbols, whatever is acceptable for an actual Lisp symbol like foo-bar or *earmuffs*) as symbols, and strings that are numeric (depending on the value of *read-base*, defaults to 10) as numbers. So I did this:

(defun read-input (file)
  (with-open-file (in file)
    (loop
       for direction = (read in nil)
       for distance = (read in nil)
       while (and direction distance)
       collect (list direction distance))))

The nil tells read not to signal an error when it reaches the end of file, instead we just get nil as the result of a read (coincidence, not because that's the value given there). That's why I have the while loop there, when either of those turn up with nil (should the input be malformed and not have an even number of inputs). So I do two reads in succession, and collect the results into a new list (could've used a cons or a struct or anything else appropriate).

That loop could also be used to solve the problem in a single pass since we don't actually need the input, but I like to keep the reading and the processing separated.

permalinkparentcontextauthor-focusas-ofpreserve
r/revedditremoved.substack.com
🚨 NEWS 🚨
✖︎

Important: Reddit Changed How Removals Work

A recent Reddit update makes mod-removed content disappear from profile pages, which breaks Reveddit's website.

Install the browser extension to receive removal alerts.

Add to chromeAdd to firefox

What changed?

r/revedditremoved.substack.com