December 04
On Saturday, 2 December 2023 at 13:33:33 UTC, Johannes Miesenhardt wrote:
> 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.
>> But to avoid spoilers, it's best to do this with a 24h delay after each puzzle is published.
>
> Day 1 solution
>
> ```d
> version = Part2;
>
> import std.stdio;
> import std.algorithm;
> import std.array;
> import std.format;
> import std.conv;
> import std.string;
> ...

Why do you do multiple imports instead of one import std;?

I means is there any difference in CT?

Matheus.
December 04
On Monday, 4 December 2023 at 03:07:07 UTC, matheus wrote:
>> import std.stdio;
>> import std.algorithm;
>> import std.array;
>> import std.format;
>> import std.conv;
>> import std.string;
>> ...
>
> Why do you do multiple imports instead of one import std;?
>
> I means is there any difference in CT?

The code indeed compiles faster with fewer imports and this pays off in the long run for the actively developed large projects. Additionally, it's a good idea not to pollute the namespace with the functions that the developer has no intention to use.
December 04

On Monday, 4 December 2023 at 03:50:47 UTC, Siarhei Siamashka wrote:

>

On Monday, 4 December 2023 at 03:07:07 UTC, matheus wrote:

> >

import std.stdio;
import std.algorithm;
import std.array;
import std.format;
import std.conv;
import std.string;
...

Why do you do multiple imports instead of one import std;?

I means is there any difference in CT?

The code indeed compiles faster with fewer imports and this pays off in the long run for the actively developed large projects. Additionally, it's a good idea not to pollute the namespace with the functions that the developer has no intention to use.

  dmd -betterC hellobc.d ran
    6.18 ± 0.33 times faster than dmd hellosel.d
   19.76 ± 1.06 times faster than dmd hellostd.d

26ms, 136ms, 470ms.
16MB, 56MB, 154MB.
-betterC with selected import of core.stdc.stdio
selected import of std.stdio.writeln
import std.

D has the capability to compile extremely quickly. dmd compiles itself in a second or two. But it also gives you plenty of opportunities to trade that for convenience.

December 06

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?

It's very important to know how slices and garbage collector work together in D language. In particular, the results produced by the following code may look surprising to beginners:

import std;

void f1(ref int[] x) {
  x[0] += 1;
  x    ~= [9, 9, 9, 9, 9, 9];
  x[0] -= 1;
}

void f2(int[] x) {
  x[0] += 1;
  x    ~= [9, 9, 9, 9, 9, 9];
  x[0] -= 1;
}

void main() {
  int[] a = [1, 2, 3, 4];
  writefln!"original:       %s"(a); // [1, 2, 3, 4]
  f1(a);
  writefln!"after f1:       %s"(a); // [1, 2, 3, 4, 9, 9, 9, 9, 9, 9]
  f2(a);
  writefln!"after f2:       %s"(a); // [2, 2, 3, 4, 9, 9, 9, 9, 9, 9]
  f2(a);
  writefln!"after f2 again: %s"(a); // [3, 2, 3, 4, 9, 9, 9, 9, 9, 9]
  a.reserve(100);
  writefln!"reserved extra capacity to allow doing resize in-place";
  f2(a);
  writefln!"after f2 again: %s"(a); // [3, 2, 3, 4, 9, 9, 9, 9, 9, 9]
}

Coming from the other programming languages, people are usually familiar with the concept of passing function arguments either by value or by reference. And the behavior of the f2 function may seem odd in the example above. It never changes the size of the original array, but may modify its data in unexpected ways contrary to naive expectations. There's a detailed article on this topic, where all the necessary answers can be found: https://dlang.org/articles/d-array-article.html

Many functions in D language have their pairs/siblings. The potentially wasteful .byLineCopy has its potentially destructive sibling .byLine. There are also .split/.splitter or .retro/.reverse pairs with their own subtle, but important differences. The beginners may prefer to start with the less efficient, but easier to use variants with no undesirable side effects.

December 14

On Sunday, 3 December 2023 at 15:04:09 UTC, Siarhei Siamashka wrote:

>

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
https://github.com/schveiguy/adventofcode/blob/master/2023/day1/trebuchet.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

Bummer, looks like publishing the puzzle input data is forbidden: https://www.reddit.com/r/adventofcode/comments/18ehed6/re_not_sharing_inputs_psa_deleting_and_committing/

So for the purposes of automated testing and/or benchmarking, it would be necessary to invoke one of the numerous existing AoC command line client applications to download the input data before running the test. BTW, there seems to be no aoc-cli tool implemented in D yet ;-)

1 2
Next ›   Last »