On Monday, 24 November 2025 at 14:37:22 UTC, Jabba Laci wrote:
I implemented [Advent of Code 2015, Day 6, Part 1](https://adventofcode.com/2015/day/6) in D. The code is here: https://github.com/jabbalaci/AdventOfCode2015/tree/main/day06/d/part1 . It runs for 3 seconds. I found it a bit much, so I also wrote it in Python. To my surprise, the Python program also ran for 3 seconds. I expected that the D program should be much faster.

I made it faster. Compile it with `dub build --compiler=ldc2 --build=release`:

```d
#!/usr/bin/env rdmd

import std.algorithm;
import std.conv;
import std.file;
import std.stdio;
import std.string;
import std.typecons;

alias Pair = Tuple!(int, int);

enum Option
{
    off = 0,
    on = 1,
    toggle = 2
}

Pair extract_pair(const string s)
{
// NOTE(Kap): No idea, just did it this way because it seems better to me.
    foreach (i, c; s)
    {
        if (c != ',') continue;
        return tuple(s[0 .. i].to!int, s[i + 1 .. $].to!int);
    }
    return tuple(0, 0);
}

void process_line(ref int[1000][] d, const string line)
{
// NOTE(Kap): No idea, was thining that split was slow or something, but maybe not.
    string[16] parts_buffer;
    string[] parts;
    {
        size_t part_index, previous_space_index;
        foreach (i, c; line) {
            if (c != ' ') continue;
parts_buffer[part_index] = line[previous_space_index .. i];
            previous_space_index = i + 1;
            part_index += 1;
        }
parts_buffer[part_index] = line[previous_space_index .. $];
        parts = parts_buffer[0 .. part_index + 1];
    }

    Option option;
    // dfmt off
    // NOTE(Kap): I'm trying to be smart here for no reason.
    if (parts[1][$ - 1] == 'n') {
        option = Option.on;
    } else if (parts[1][$ - 1] == 'f') {
        option = Option.off;
    } else if (parts[0][$ - 1] == 'e') {
        option = Option.toggle;
    } else {
        assert(0);
    }
    // dfmt on

    if (option == Option.on || option == Option.off)
    {
        auto pair1 = extract_pair(parts[2]);
        auto pair2 = extract_pair(parts[$ - 1]);
        int x1 = pair1[0], y1 = pair1[1];
        int x2 = pair2[0], y2 = pair2[1];
        for (int i = x1; i <= x2; ++i)
        {
            for (int j = y1; j <= y2; ++j)
            {
                d[i][j] = int(option); // off: 0, on: 1
            }
        }
    }
    else if (option == Option.toggle)
    {
        auto pair1 = extract_pair(parts[1]);
        auto pair2 = extract_pair(parts[$ - 1]);
        int x1 = pair1[0], y1 = pair1[1];
        int x2 = pair2[0], y2 = pair2[1];
        for (int i = x1; i <= x2; ++i)
        {
            for (int j = y1; j <= y2; ++j)
            {
                d[i][j] = 1 - d[i][j];
            }
        }
    }
    else
    {
        assert(0);
    }
}

int get_result(int[1000][] d)
{
    // if value is 0: off, 1: on
    auto result = 0;
    foreach (ref col; d) foreach (x; col) result += x;
    return result;
}

void main()
{
    // const fname = "example1.txt";
    // const fname = "example2.txt";
    // const fname = "example3.txt";
    const fname = "input.txt";

    const lines = readText(fname).splitLines;
    // int[Pair] d;
    auto d = new int[1000][1000];

    foreach (line; lines)
    {
        process_line(d, line);
    }
    int result = get_result(d);
    writeln(result);
}
```

Reply via email to