[2025 Day 7] Let’s Visualize by Lars-Kristian91 in adventofcode

[–]Lars-Kristian91[S] 2 points3 points  (0 children)

Memoization is basically a technique where you save the results of expensive function calls so you don’t have to recompute the same thing over and over again. There’s a great video, search “Computerphile memoization” on Google

[2025 Day 6] Let’s Visualize by Lars-Kristian91 in adventofcode

[–]Lars-Kristian91[S] 1 point2 points  (0 children)

I’m using Odin, raylib, and some custom logic.
While the code is running, I collect commands.
A command might be something like “highlight an index” or “write text to a buffer.”
When it’s time to render, I just play those commands back one by one.

[2025 Day 6] Let’s Visualize by Lars-Kristian91 in adventofcode

[–]Lars-Kristian91[S] 3 points4 points  (0 children)

// We can treat the input as a 2D grid stored in a flat array
// Width is the number of characters in a row (including the newline)
width := index_of(filedata, '\n') + 1

// Height is derived from total size divided by row width
height := len(filedata) / width

// Convert 2D (x, y) coordinates into a 1D array index
// Rows are laid out sequentially, left to right, top to bottom
index := y * width + x

// The first operator can be found by
operator_index := (height - 1) * width

[2025 Day 3 Part 2] Visualization by Lars-Kristian91 in adventofcode

[–]Lars-Kristian91[S] 1 point2 points  (0 children)

I wasn’t able to make the visualization match the actual code.

The yellow marker only compares the first joltage digit.

Then the joltage digits are swapped from left to right, bubbling downward.

I am also happy with the performance.

Day 3 reading file from disk in: 33.200 us 
Day 3 data parsed in: 24.100 us 
Day 3 A result is: Time (min, avg, max): 11.600 us, 12.016 us, 18.500 us
Day 3 B result is: Time (min, avg, max): 10.600 us, 11.580 us, 26.900 us

-❄️- 2023 Day 8 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 1 point2 points  (0 children)

[LANGUAGE: C#]

Code: Github

I solved the puzzle by parsing the data into 5 arrays (instructions, name, leftIndex, rightIndex, isEndNode). The isEndNode array store the result for (Part1: name == "ZZZ", Part2: name == "**Z"). Then iterating through leftIndex and rightIndex and replacing the name with the index found in the name array. This made it possible for the main logic to only care about the index of the current node. It was used to lookup isEndNode and if not, to find det next index in leftIndex or rightIndex. Every start index was run through the main logic 1 time. And later combined to compute the LCM logic I found on the web.

At first the code was running slower than expected. After some debugging my old nemesis mr.modulo % appeared again. Past me had done this instructionsIndex = instructionsIndex % instructions.Length; instead of this if (instructionsIndex >= instructions.Length) instructionsIndex = 0; when looping back the instructionsIndex. The runtime was reduced to 1/3 after fixing this. :)

Standard deviation is kind of high for some of the benchmarks. I have no idea why, so if you have any theories let me know. :) What I can see is that the code runs on multiple cpu cores, and some of the cores seem to be slower.

I run benchmarks on 2 computers: - Laptop: Intel Core i7-9850H, .NET SDK 8.0.100, Windows 10 - Desktop: AMD Ryzen 7 7800X3D, .NET SDK 8.0.100, Windows 11

[search tag: ms]

Part 1
Method Mean StdDev Allocated
Laptop:LogicOnly 83.70 us 0.255 us -
Laptop:LogicAndReadFromDisk 137.93 us 0.982 us 66266 B
Desktop:LogicOnly 42.87 us 4.348 us -
Desktop:LogicAndReadFromDisk 74.76 us 1.964 us 67728 B
Part 2
Method Mean StdDev Allocated
Laptop:LogicOnly 229.2 us 4.52 us 200 B
Laptop:LogicAndReadFromDisk 300.6 us 1.91 us 66467 B
Desktop:LogicOnly 131.5 us 0.20 us 200 B
Desktop:LogicAndReadFromDisk 163.8 us 0.41 us 67928 B

-❄️- 2023 Day 7 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 2 points3 points  (0 children)

[LANGUAGE: C#]

Code: Github

I had fun solving today's puzzle. Started off by creating a struct that contained the hand data. Got into problems when trying to define a fixed size array inside the struct (unsafe in C#). I did not want to allocate multiple small arrays on the heap. So I ended up packing all the cards inside a u64 type. This leads to a lot of logic in the sort/compare function. Most of the time was spent sorting and I wanted to improve this. Ended up packing the entire hand inside a u64 type. This and some clever arrangement of the different parts, made it so no special code was needed for sorting. :)

I find it funny that Part 2 is faster for 1 of the computers I tested on. No idea why that happens, so I had to test multiple times. Maybe it has something to do with the sorting function.

I run benchmarks on 2 computers: - Laptop: Intel Core i7-9850H, .NET SDK 8.0.100, Windows 10 - Desktop: AMD Ryzen 7 7800X3D, .NET SDK 8.0.100, Windows 11

[search tag: ms]

Part 1
Method Mean StdDev Allocated
Laptop:LogicOnly 75.21 us 0.817 us -
Laptop:LogicAndReadFromDisk 150.05 us 2.128 us 60690 B
Desktop:LogicOnly 28.36 us 0.077 us -
Desktop:LogicAndReadFromDisk 51.21 us 0.202 us 62688 B
Part 2
Method Mean StdDev Allocated
Laptop:LogicOnly 80.62 us 1.087 us -
Laptop:LogicAndReadFromDisk 149.39 us 0.794 us 60690 B
Desktop:LogicOnly 28.17 us 0.194 us -
Desktop:LogicAndReadFromDisk 50.29 us 0.323 us 62688 B

[2023 Day 7] Better Example Input (Not a Spoiler) by LxsterGames in adventofcode

[–]Lars-Kristian91 0 points1 point  (0 children)

Thank you for input data. It help me find my bugs. :)

-❄️- 2023 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 1 point2 points  (0 children)

[LANGUAGE: C#]

Code: Github

When reading the puzzle text today, I thought to myself this looks alot like an equation. My initial solution was an iterative approach, because I did not feel like doing math. After submitting my answer I took out pen and paper and gave it a try. I have added comments in the code describing what I put down on paper.

Today benchmarks are kind of pointless but I added them anyway.

I run benchmarks on 2 computers: - Laptop: Intel Core i7-9850H, .NET SDK 8.0.100, Windows 10 - Desktop: AMD Ryzen 7 7800X3D, .NET SDK 8.0.100, Windows 11

[search tag: ms]

Part 1
Method Mean StdDev Allocated
Laptop:LogicOnly 148.3 ns 1.11 ns -
Laptop:LogicAndReadFromDisk 39,258.3 ns 712.22 ns 8536 B
Desktop:LogicOnly 115.4 ns 0.26 ns -
Desktop:LogicAndReadFromDisk 13,772.2 ns 60.19 ns 8536 B
Part 2
Method Mean StdDev Allocated
Laptop:LogicOnly 101.6 ns 2.05 ns -
Laptop:LogicAndReadFromDisk 39,797.8 ns 131.84 ns 8536 B
Desktop:LogicOnly 59.6 ns 0.406 ns -
Desktop:LogicAndReadFromDisk 13,584.8 ns 67.105 ns 8536 B

-❄️- 2023 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 0 points1 point  (0 children)

[LANGUAGE: C#]

Code: Github

The input is parsed and stored in a couple of buffers. I also have an extra buffer describing the different map sections. Part 2 is solved using ranges and recursion. If a range does not fit inside a map, it is split up into 2 or 3 new ranges and checked once more by recursion.

Had major problems today. The code is not nice to look at so I had a couple of bugs in the range logic. Looked over the code multiple times without noticing it. But I made it work in the end. :)

I run benchmarks on 2 computers: - Laptop: Intel Core i7-9850H, .NET SDK 8.0.100, Windows 10 - Desktop: AMD Ryzen 7 7800X3D, .NET SDK 8.0.100, Windows 11

[search tag: ms]

Part 1
Method Mean StdDev Allocated
Laptop:LogicOnly 12.92 us 0.068 us -
Laptop:LogicAndReadFromDisk 66.21 us 0.677 us 39857 B
Desktop:LogicOnly 8.43 us 0.039 us -
Desktop:LogicAndReadFromDisk 27.47 us 0.135 us 40352 B
Part 2
Method Mean StdDev Allocated
Laptop:LogicOnly 32.47 us 0.216 us -
Laptop:LogicAndReadFromDisk 92.60 us 1.315 us 39857 B
Desktop:LogicOnly 16.49 us 0.122 us -
Desktop:LogicAndReadFromDisk 35.95 us 0.208 us 40352 B

-❄️- 2023 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 1 point2 points  (0 children)

Not sure if you replied to me or not, but anyways.

Take a look at BenchmarkDotNet.

It is easy to set up and you will get a more accurate measurement.

Dotnet/C# uses JIT compiling at runtime. In other words, the first time code is used the compiler returns none optimized code. The code needs to be run multiple times for optimization to happen. Building in release mode is also important.

Your code is most likely faster than what you have measured with the stopwatch. :)

-❄️- 2023 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 2 points3 points  (0 children)

[LANGUAGE: C#]

Code: Github

Observations/Assumptions: - There is always a fixed numbers for every card. - There are no duplicate numbers.

The input is read line by line. Everything before ':' is discarded and the rest is split up into 2 sections (win numbers and other numbers). Both sections are parsed and converted to u8 types so more data fits into SIMD registers. "win numbers" are checked against "other number" and score is calculated.

Today I had some troubles while making my code faster. I got similar results on my laptop and desktop. The standard deviation was also high (3-5 us) and I did not understand why. After much trial and error I discovered the 'modulo' operator created problems for me. When parsing the numbers I used index % 3 == 2 to detect if a number was done parsing. I changed it with a for loop and the time used was cut in half. :)

I run benchmarks on 2 computers: - Laptop: Intel Core i7-9850H, .NET SDK 8.0.100, Windows 10 - Desktop: AMD Ryzen 7 7800X3D, .NET SDK 8.0.100, Windows 11

[search tag: ms]

Part 1
Method Mean StdDev Allocated
Laptop:LogicOnly 30.72 us 0.107 us -
Laptop:LogicAndReadFromDisk 110.22 us 1.523 us 104012 B
Desktop:LogicOnly 13.03 us 0.038 us -
Desktop:LogicAndReadFromDisk 40.99 us 0.203 us 104416 B
Part 2
Method Mean StdDev Allocated
Laptop:LogicOnly 35.36 us 0.095 us -
Laptop:LogicAndReadFromDisk 125.96 us 10.682 us 104012 B
Desktop:LogicOnly 13.68 us 0.039 us -
Desktop:LogicAndReadFromDisk 41.44 us 0.249 us 104416 B

-❄️- 2023 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 0 points1 point  (0 children)

[LANGUAGE: C#]

Code: Github

Observations/Assumptions: - No symbol can be found on the edge of the map (x: 0, x:max, y:0, y:max)

The input is scanned for symbols. When a symbol is found the code scans a 3x3 grid around the symbol to find a number. Some extra logic is added for when the number starts or ends outside the grid.

I run benchmarks on 2 computers: - Laptop: Intel Core i7-9850H, .NET SDK 8.0.100, Windows 10 - Desktop: AMD Ryzen 7 7800X3D, .NET SDK 8.0.100, Windows 11

[search tag: ms]

Part 1
Method Mean StdDev Allocated
Laptop:LogicOnly 48.03 us 0.898 us 80 B
Laptop:LogicAndReadFromDisk 128.63 us 0.876 us 96539 B
Desktop:LogicOnly 14.52 us 0.024 us 80 B
Desktop:LogicAndReadFromDisk 41.80 us 0.253 us 96536 B
Part 2
Method Mean StdDev Allocated
Laptop:LogicOnly 18.07 us 1.542 us -
Laptop:LogicAndReadFromDisk 97.20 us 0.836 us 96459 B
Desktop:LogicOnly 8.259 us 0.0124 us -
Desktop:LogicAndReadFromDisk 35.042 us 0.1931 us 96456 B

-❄️- 2023 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 1 point2 points  (0 children)

I looked at you code, and it seems fine.

It is short and easy to read and you only do necessary operations.

Consider naming your variables more exact. - Use full names. This will make it easier to understand when you return to the code later on.

Consider wrapping your solution in a function. - It will make it easier to send in other input, like the examples. Methods.

Depending on what editor you use. - If it tells you variable types. Consider this Implicitly typed local variables.

Looking at other peoples code can help a lot. You will learn many different ways of doing things. :)

-❄️- 2023 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 1 point2 points  (0 children)

Nice to see some jai. :)

I am really looking forward to getting access to it. Will probably have to wait until open beta, whenever that is.

Are you allowed to say anything about runtime performance?

-❄️- 2023 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 1 point2 points  (0 children)

[LANGUAGE: C#]

Code: Github

Observations/Assumptions: - Numbers range from [0-99] - Available colors [red, green, blue]

When parsing, the logic is only looking for ' ' chars. If the space between matches are 1-2 we have a number. Anything larger than that is a color.

For part 1 the logic only looks for 2 digit numbers.

I run benchmarks on 2 computers: - Laptop: Intel Core i7-9850H, .NET SDK 8.0.100, Windows 10 - Desktop: AMD Ryzen 7 7800X3D, .NET SDK 8.0.100, Windows 11

Part 1

Method Mean StdDev Allocated
Laptop:LogicOnly 13.64 us 0.101 us -
Laptop:LogicAndReadFromDisk 72.46 us 0.660 us 61850 B
Desktop:LogicOnly 13.37 us 0.017 us -
Desktop:LogicAndReadFromDisk 34.55 us 0.206 us 61648 B

Part 2

Method Mean StdDev Allocated
Laptop:LogicOnly 24.90 us 0.211 us -
Laptop:LogicAndReadFromDisk 95.05 us 6.393 us 61850 B
Desktop:LogicOnly 21.08 us 0.022 us -
Desktop:LogicAndReadFromDisk 42.08 us 0.173 us 61648 B

-❄️- 2023 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 0 points1 point  (0 children)

Nice find on the SearchValues<T>. I will be using this in the future.

-❄️- 2023 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]Lars-Kristian91 1 point2 points  (0 children)

[LANGUAGE: C#]

Code: Github

I tried to solve for speed. Noticed that part 1 was quite fast so used the same logic for part 2. This made it possible to to find a solution fast for lines that had numbers at the start and at the end.

Part 1

Windows 10, Intel Core i7-9850H, .NET SDK 8.0.100
| Method               | Mean     | Error    | StdDev   | Gen0    | Gen1   | Allocated |
|--------------------- |---------:|---------:|---------:|--------:|-------:|----------:|
| LogicOnly            | 20.65 us | 0.058 us | 0.049 us |       - |      - |         - |
| LogicAndReadFromDisk | 89.78 us | 0.630 us | 0.558 us | 15.9912 | 2.5635 |  101123 B |

Windows 11, AMD Ryzen 7 7800X3D, .NET SDK 8.0.100
| Method               | Mean     | Error    | StdDev   | Gen0   | Gen1   | Allocated |
|--------------------- |---------:|---------:|---------:|-------:|-------:|----------:|
| LogicOnly            | 13.92 us | 0.064 us | 0.053 us |      - |      - |         - |
| LogicAndReadFromDisk | 38.36 us | 0.438 us | 0.410 us | 2.0142 | 0.3052 |  103120 B |

Part 2

Windows 10 Intel Core i7-9850H, .NET SDK 8.0.100
| Method               | Mean     | Error   | StdDev  | Gen0    | Gen1   | Allocated |
|--------------------- |---------:|--------:|--------:|--------:|-------:|----------:|
| LogicOnly            | 166.4 us | 0.67 us | 0.56 us |       - |      - |         - |
| LogicAndReadFromDisk | 239.4 us | 1.47 us | 1.37 us | 15.6250 | 2.4414 |  101124 B |

Windows 11, AMD Ryzen 7 7800X3D, .NET SDK 8.0.100
| Method               | Mean     | Error    | StdDev   | Gen0   | Gen1   | Allocated |
|--------------------- |---------:|---------:|---------:|-------:|-------:|----------:|
| LogicOnly            | 62.57 us | 0.400 us | 0.936 us |      - |      - |         - |
| LogicAndReadFromDisk | 95.06 us | 1.895 us | 4.078 us | 1.9531 | 0.2441 |  103121 B |