r/adventofcode • • Dec 25 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 25 Solutions -🎄-

--- Day 25: Sea Cucumber ---


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.


Message from the Moderators

Welcome to the last day of Advent of Code 2021! We hope you had fun this year and learned at least one new thing ;)

Keep an eye out for the community fun awards post: (link coming soon!)

-❅- Introducing Your AoC 2021 "Adventure Time!" Adventurers (and Other Prizes) -❅-

Thank you all for playing Advent of Code this year and on behalf of /u/topaz2078, /u/Aneurysm9, the beta-testers, and the rest of AoC Ops, we wish you a very Merry Christmas (or a very merry Saturday!) and a Happy New Year!


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:09:34, megathread unlocked!

41 Upvotes

243 comments sorted by

View all comments

Show parent comment

4

u/SuperSmurfen Dec 25 '21 edited Dec 25 '21

You're right, it's a bit uglier but it has a few advantages. For one a char in Rust is actually 4 bytes, so by using u8 the grid uses about 4 times less memory. So when working with ascii-only strings this can be a huge time/memory save. Another advantage, not present in this problem though, is that a byte-string b"Hello World" is directly indexable:

let s = b"Hello World";
println!("> {}", s[6] as char);

let s = "Hello World";
println!("> {}", s.chars().nth(6).unwrap()); // annoying

2

u/spunkyenigma Dec 25 '21

Ooh, directly indexable is a very good point, so many char-indice iterations during this Advent.

Next year!