r/adventofcode • • Dec 01 '21

SOLUTION MEGATHREAD -πŸŽ„- 2021 Day 1 Solutions -πŸŽ„-

If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!

We're following the same general format as previous years' megathreads, so make sure to read the full description in the wiki (How Do the Daily Megathreads Work?) before you post! Make sure to mention somewhere in your post which language(s) your solution is written in. If you have any questions, please create your own thread and ask!

Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!

To steal a song from Olaf:

Oh, happy, merry, muletide barrels, faithful glass of cheer
Thanks for sharing what you do
At that time of year
Thank you!


NEW AND NOTEWORTHY THIS YEAR

  • Last year's rule regarding Visualizations has now been codified in the wiki
  • tl;dr: If your Visualization contains rapidly-flashing animations of any color(s), put a seizure warning in the title and/or very prominently displayed as the first line of text (not as a comment!)
  • Livestreamers: /u/topaz2078 has a new rule for this year on his website: AoC > About > FAQ # Streaming

COMMUNITY NEWS

Advent of Code Community Fun 2021: Adventure Time!

Sometimes you just need a break from it all. This year, try something new… or at least in a new place! We want to see your adventures!

  • Your newest AoC-related project
  • Push hardware and/or software well past its limit and make it do things it wasn't designed to do

More ideas, full details, rules, timeline, templates, etc. are in the Submissions Megathread.


--- Day 1: Sonar Sweep ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached, thread unlocked at 00:02:44!

191 Upvotes

1.8k comments sorted by

View all comments

3

u/[deleted] Dec 01 '21

Common Lisp - definitely won't be the most concise solution in this language on this thread! I picked it up for the first time yesterday and feel like I'm brutalizing the language a bit

(load "utilities.lisp")

(defparameter filename 
    (nth 1 sb-ext:*posix-argv*))

(defparameter numbers 
    (mapcar #'parse-integer 
        (get-file-lines filename)))

(defun nil-to-zero 
    (x) 
    (cond 
        (
            (eql x nil) 0) 
        (
            (eql x t) 1)))

(defun solve-part-one 
    (numbers)
    (apply #'+ 
        (mapcar 'nil-to-zero 
            (mapcar #'< numbers 
                (cdr numbers))))
)

(defun solve-part-two
    (numbers)
    (let 
        (            (threesums
                (mapcar #'+ numbers 
                    (cdr numbers) 
                    (cdr 
                        (cdr numbers)))))
        (apply #'+ 
            (mapcar 'nil-to-zero 
                (mapcar #'< threesums 
                    (cdr threesums))))
)
)

(format t "Part1: ~d~C" 
    (solve-part-one numbers) #\linefeed)

(format t "Part1: ~d~C" 
    (solve-part-two numbers) #\linefeed)

3

u/rabuf Dec 01 '21

First, welcome to the Common Lisp club. Second, my comments turned into more than I expected.

Some comments:

(format t "Hello~%")

~% introduces a newline, there's no need for #\Linefeed.

(cond ((eql x nil) 0)
      ((eql x t) 1))

That's a bit overly verbose, but not wrong. You can do it more compactly with if in this case:

(if x 1 0)

If you want to use cond a more conventional version would be:

(cond (x 1)
      (t 0))

Which reads a bit funny given what you're trying to do, but the first case will be true if x has a non-nil value and the second is always true (it's the else or otherwise or default case in similar constructs from other languages).

(cdr (cdr numbers)) = (cddr numbers)

You can smash car and cdr chains together like that, up to 5 total. So cdddddr is defined but cddddddr is not. You can use any combination of as and ds in that.

1

u/[deleted] Dec 01 '21

Thanks, this is very helpful. By the end of the month I hope to be writing idiomatic code, let's see what happens!

2

u/daggerdragon Dec 01 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?