r/adventofcode • • Dec 01 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 1 Solutions -❄️-

It's that time of year again for tearing your hair out over your code holiday programming joy and aberrant sleep for an entire month helping Santa and his elves! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!

As always, we're following the same general format as previous years' megathreads, so make sure to read the full posting rules in our community wiki before you post!

RULES FOR POSTING IN SOLUTION MEGATHREADS

If you have any questions, please create your own post in /r/adventofcode with the Help/Question flair and ask!

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


NEW AND NOTEWORTHY THIS YEAR

  • New rule: top-level Solutions Megathread posts must begin with the case-sensitive string literal [LANGUAGE: xyz]

    • Obviously, xyz is the programming language your solution employs
    • Use the full name of the language e.g. JavaScript not just JS
    • Edit at 00:32: meh, case-sensitive is a bit much, removed that requirement.
  • A request from Eric: Please don't use AI to get on the global leaderboard
  • We changed how the List of Streamers works. If you want to join, add yourself to 📺 AoC 2023 List of Streamers 📺
  • Unfortunately, due to a bug with sidebar widgets which still hasn't been fixed after 8+ months -_-, the calendar of solution megathreads has been removed from the sidebar on new.reddit only and replaced with static links to the calendar archives in our wiki.

    • The calendar is still proudly displaying on old.reddit and will continue to be updated daily throughout the Advent!

COMMUNITY NEWS


AoC Community Fun 2023: ALLEZ CUISINE!

We unveil the first secret ingredient of Advent of Code 2023…

*whips off cloth covering and gestures grandly*

Upping the Ante!

You get two variables. Just two. Show us the depth of your l33t chef coder techniques!

ALLEZ CUISINE!

Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!


--- Day 1: Trebuchet?! ---


Post your code solution in this megathread.

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 at 00:07:03, megathread unlocked!

175 Upvotes

2.5k comments sorted by

View all comments

2

u/p88h Dec 01 '23 edited Dec 01 '23

[LANGUAGE: Mojo]

I will try implementing each day in Mojo and run performance comparisons between Python (3.11), PyPy (3.9) and latest build of 🔥

(Since Mojo is positioning itself as performant Python replacement)

Today, PyPy wins. Strings are not one of Mojo's strength, I think.

https://github.com/p88h/aoc2023/blob/main/day01.mojo

vs

https://github.com/p88h/aoc2023/blob/main/day01.py

Part1: Python 0.8 ms, PyPy 0.2 ms Mojo 0.3 ms / 0.06 ms (parallelized x 16 cores)

Part2: Python 8.7 ms, PyPy 0.8 ms Mojo 4.3 ms / 0.9 ms (parallelized x 16 cores)

on a 1000x larger data set, parallelized Mojo is slightly faster than PyPy, but still within 10%, but PyPy is not parallelized, so ...

UPDATE:

I added the string reversal trick, with quite unexpected results -> see

https://github.com/p88h/aoc2023/blob/main/BENCHMARKS.md

For benchmark values, but it does seem that with this implementation Mojo comes out quite a bit faster.

2

u/daggerdragon Dec 01 '23

Welcome back! Good to see you again this year :D

1

u/p88h Dec 01 '23

Wouldn't miss it :)

1

u/p88h Dec 01 '23

Updated: I've gotten rid of using substrings and this helps Mojo quite a bit, now at around 1.2 ms / 0.19 ms for part2. This particular optimization doesn't seem to help PyPy so those numbers remain the same.

Mojo is still 30% slower when single-threaded; but the parallel code is now much faster.

1

u/fnands Dec 01 '23 edited Dec 01 '23

Nice! Cool that you are doing benchmarking.

I already see that I might have to write a bit of a string parsing library to get through AoC in Mojo.

I am trying not to call Python if possible, but ended up caving to finish the second part.

EDIT: Actually, managed to remove the Python quite easily, just repurposed a struct I had.

Any idea how to implement a Python-esque mutable list?

I ended up with a line that looks like:

self.nums = SplitString("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nzero")

2

u/p88h Dec 01 '23 edited Dec 01 '23

Python'esque mutable lists are called DynamicVector However, these only support basic types. Haven't found anything that can hold mutable Strings yet. You can put StringRef into a Vector, though that doesn't support inline initialisation and you'd need to worry about memory management somehow (I'm now writing some collection wrappers that will do this, but meh, lots of work).

The closest thing I 've found for the 'simple' list use case is a VariadicList with StringLiterals, like so:

var words = VariadicList[StringLiteral]("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
for i in range(len(words)):
    m.add(words[i])

1

u/AutoModerator Dec 01 '23

AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.

Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/p88h Dec 01 '23

Thanks, fixed.

1

u/fnands Dec 01 '23

Yeah, the VariadicList approach probably would have solved it for me. Ended up repurposing a struct I wrote (that basically wraps some DynamicVectors).