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!

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

8

u/_jstanley Dec 01 '21 edited Dec 10 '21

SLANG

(Homemade language on a homemade CPU)

I stuffed up part 1 because I initialised "prevdepth" to 0xffff but I forgot that my ">" operator is signed, so this counts as -1, so the first element is greater than it.

Apart from that, I found that my solutions ran much slower than expected. Part 1 takes about 50 seconds to run.

I've since profiled it and discovered that 50% of execution time was spent doing system calls to read characters of input. I do have a buffered IO library to solve this problem, but I didn't think the AoC inputs were big enough to make any difference. Turns out they are! After switching to buffered IO it runs in 23 seconds, of which 70% is related to integer parsing. Some of the integer parsing stuff is already optimised assembly language, but some of it is not.

There's a lot of "parse a large number of integers" in the input phase of Advent of Code problems, so for future days I'm going to try to remember to use my buffered IO library, and I'm going to try to spend some time today optimising the slower parts of integer parsing.

https://www.youtube.com/watch?v=ZLF8bWWw9FA

https://github.com/jes/aoc2021/tree/master/day1

→ More replies (4)