Files
2024/src/Util/NumConcat.cs
Parnic 57ed9d261e Day 7 optimization
Okay, this optimization was easier than I thought it'd be.

I started by not building a new list every time I wanted to operate on the next value since that seemed like low-hanging fruit. That helped, but not as much as I expected (as is always the case with optimization, I feel).

Next I ran this under a profiler and it showed that string concatenation was the biggest offender, so I found a way to do that without involving strings (it's a bit convoluted, but optimizations tend to do that...) and that cleaned up most of the problem.

After that, getting the next element from the list was a (very minor) next-highest offender, so I cached that locally.

Finally, on a whim I decided that the tuple wasn't helping me anymore, so I removed it and was surprised to see another 100ms or so disappear. I guess constructing those is worse than I thought...gotta file that away for later. I'm a little surprised that it didn't show up in the profiler (or I didn't know how to read it, perhaps).
2024-12-07 10:50:59 -06:00

30 lines
1.0 KiB
C#

namespace aoc2024.Util;
public static class NumConcat
{
public static long Longs(long a, long b)
{
return b switch
{
< 10 => 10L * a + b,
< 100 => 100L * a + b,
< 1000 => 1000L * a + b,
< 10000 => 10000L * a + b,
< 100000 => 100000L * a + b,
< 1000000 => 1000000L * a + b,
< 10000000 => 10000000L * a + b,
< 100000000 => 100000000L * a + b,
< 1000000000 => 1000000000L * a + b,
< 10000000000 => 10000000000L * a + b,
< 100000000000 => 100000000000L * a + b,
< 1000000000000 => 1000000000000L * a + b,
< 10000000000000 => 10000000000000L * a + b,
< 100000000000000 => 100000000000000L * a + b,
< 1000000000000000 => 1000000000000000L * a + b,
< 10000000000000000 => 10000000000000000L * a + b,
< 100000000000000000 => 100000000000000000L * a + b,
_ => 1000000000000000000L * a + b
};
}
}