Advent of Code 2023 starts in a few hours from now. I suggest to discuss D language solutions here.
But to avoid spoilers, it's best to do this with a 24h delay after each puzzle is published.
Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 01 Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
December 01 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Siarhei Siamashka | On Friday, 1 December 2023 at 01:01:31 UTC, Siarhei Siamashka wrote: >Advent of Code 2023 starts in a few hours from now. I suggest to discuss D language solutions here. Hi Siarhei. Nice to see that you are still around D forums. I thought you moved to Crystall. |
December 02 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Siarhei Siamashka | On Friday, 1 December 2023 at 01:01:31 UTC, Siarhei Siamashka wrote: >Advent of Code 2023 starts in a few hours from now. I suggest to discuss D language solutions here. Day 1 solution
I am a bloody beginner so if there are any things that are very wrong with this please point them out. |
December 02 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Miesenhardt | On Saturday, 2 December 2023 at 13:33:33 UTC, Johannes Miesenhardt wrote: >Day 1 solution here, since I swap them out based on a runtime argument. In the Discord server we also have a topic about AoC2023. So feel free to join it as well. Some other solutions that could be worth to check: https://github.com/andrewlalis/AdventOfCode2023/blob/main/day_1/solution.d |
December 03 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Miesenhardt | On Saturday, 2 December 2023 at 13:33:33 UTC, Johannes Miesenhardt wrote: >I am a bloody beginner so if there are any things that are very wrong with this please point them out. Everything is fine as long as it works and does the job. >The fact that I need a template for accepting both a string and a char[] is very weird but I went with it. The Strings can be safely passed around between functions and multiple instances of the same string can share the same memory location, the characters inside of a string are read-only. Whereas character arrays allow read/write access. Casting between character arrays and strings is a bad idea by design. Converting between strings and character arrays involves allocating memory for a new copy and this happens under the hood when calling I am also curious if there is a better way for the reversible for-loop to happen. I saw foreach and foreach_reverse but I don't think that helps me here, since I swap them out based on a runtime argument. Below is my solution for the day 1 puzzle, which used https://dlang.org/library/std/range/retro.html to search characters starting from the end:
The key features are:
|
December 03 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sergey | On Saturday, 2 December 2023 at 14:35:19 UTC, Sergey wrote: >Some other solutions that could be worth to check: https://github.com/andrewlalis/AdventOfCode2023/blob/main/day_1/solution.d It's indeed a good idea to have solutions on github, so I pushed mine here: https://github.com/ssvb/adventofcode/blob/main/2023/day01/trebuchet.d Maybe later I'll also publish all my D solutions for older puzzles from Advent of Code 2022, but that code needs a bit of cleanup. |
December 03 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Siarhei Siamashka | On Sunday, 3 December 2023 at 14:51:37 UTC, Siarhei Siamashka wrote: >[...] Thanks, this is super helpful. I have one other question, in the solution you posted and also the one I posted in the discord today. I was required to use byLineCopy. I first used byLine but I for some reason that I can't really explain only got the last line from that. I switched to byLineCopy because I saw it in other peoples solution and that magically fixed all problems I had. What exactly happened here? |
December 03 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Miesenhardt | On Sunday, 3 December 2023 at 18:56:32 UTC, Johannes Miesenhardt wrote: >On Sunday, 3 December 2023 at 14:51:37 UTC, Siarhei Siamashka wrote: >[...] Thanks, this is super helpful. I have one other question, in the solution you posted and also the one I posted in the discord today. I was required to use byLineCopy. I first used byLine but I for some reason that I can't really explain only got the last line from that. I switched to byLineCopy because I saw it in other peoples solution and that magically fixed all problems I had. What exactly happened here? byLine reuses the buffer. So it is only valid while you haven’t fetched the next line. byLineCopy makes a copy of the line to give you so it will always remain valid. In these simple small type problems I find it easier to just fetch the whole file into a string and work with that. The performance of parsing the input is negligible. -Steve |
December 03 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Miesenhardt | On Saturday, 2 December 2023 at 13:33:33 UTC, Johannes Miesenhardt wrote: >I am a bloody beginner so if there are any things that are very wrong with this please point them out. Rather than putting
Rather than the template you could only accept You may still want a template though, to specialize on the
Bonus from using a dynamic array: it would be much more annoying to have And, In your loop over numberMap, you could use
Otherwise, I think it's fine good. People might differ on style, but it doesn't look bad at all compared to some other implementations I've seen. The several ternary operators are the only awkward bit. Since you're a beginner you might find it interesting to implement a range that yields chars in reverse order, and have |
December 03 Re: Advent of Code 2023 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julian Fondren | On Sunday, 3 December 2023 at 23:44:43 UTC, Julian Fondren wrote: >
Corrected. The other doesn't compile, unless you never run it with -version=Part2 ... |