Make vectors readonly again

These changes to instantiate new vectors don't materially impact the runtime of either of these days, so it seems best to make the immutability explicit.

I _can_, however, make day 24 twice as fast by manipulating vectors in-place, but that's an ugly optimization that isn't actually necessary given its already-decent runtime (100ms -> 50ms) at the expense of making the code uglier and less DRY, so for now I'm not going to do that.
This commit is contained in:
2022-12-24 15:37:49 -06:00
parent cc2b166e23
commit 610ecce813
4 changed files with 17 additions and 21 deletions

View File

@ -113,21 +113,21 @@ internal class Day17 : Day
{
if (canMove(rockIdx, rockPos, grid, ivec2.RIGHT))
{
rockPos.x++;
rockPos = new ivec2(rockPos.x + 1, rockPos.y);
}
}
else
{
if (canMove(rockIdx, rockPos, grid, ivec2.LEFT))
{
rockPos.x--;
rockPos = new ivec2(rockPos.x - 1, rockPos.y);
}
}
// inverted up/down for this...
if (canMove(rockIdx, rockPos, grid, ivec2.UP))
{
rockPos.y--;
rockPos = new ivec2(rockPos.x, rockPos.y - 1);
}
else
{
@ -174,21 +174,21 @@ internal class Day17 : Day
{
if (canMove(rockIdx, rockPos, grid, ivec2.RIGHT))
{
rockPos.x++;
rockPos = new ivec2(rockPos.x + 1, rockPos.y);
}
}
else
{
if (canMove(rockIdx, rockPos, grid, ivec2.LEFT))
{
rockPos.x--;
rockPos = new ivec2(rockPos.x - 1, rockPos.y);
}
}
// inverted up/down for this...
if (canMove(rockIdx, rockPos, grid, ivec2.UP))
{
rockPos.y--;
rockPos = new ivec2(rockPos.x, rockPos.y - 1);
}
else
{

View File

@ -536,11 +536,11 @@ internal class Day22 : Day
{
if (inst.turn == 'R')
{
facing.RotateRight();
facing = facing.GetRotatedRight();
}
else if (inst.turn == 'L')
{
facing.RotateLeft();
facing = facing.GetRotatedLeft();
}
}
}
@ -591,11 +591,11 @@ internal class Day22 : Day
{
if (inst.turn == 'R')
{
facing.RotateRight();
facing = facing.GetRotatedRight();
}
else if (inst.turn == 'L')
{
facing.RotateLeft();
facing = facing.GetRotatedLeft();
}
}
}

View File

@ -1,9 +1,9 @@
namespace aoc2022.Util;
public struct ivec2 : IEquatable<ivec2>, IComparable<ivec2>, IComparable
public readonly struct ivec2 : IEquatable<ivec2>, IComparable<ivec2>, IComparable
{
public long x = 0;
public long y = 0;
public readonly long x = 0;
public readonly long y = 0;
public static readonly ivec2 ZERO = new ivec2(0, 0);
public static readonly ivec2 ONE = new ivec2(1, 1);
@ -27,10 +27,8 @@ public struct ivec2 : IEquatable<ivec2>, IComparable<ivec2>, IComparable
public long MaxElement => System.Math.Max(x, y);
public ivec2 GetRotatedLeft() => new ivec2(y, -x);
public void RotateLeft() => this = GetRotatedLeft();
public ivec2 GetRotatedRight() => new ivec2(-y, x);
public void RotateRight() => this = GetRotatedRight();
public long Dot(ivec2 v) => (x * v.x) + (y * v.y);
public long LengthSquared => (x * x) + (y * y);

View File

@ -1,10 +1,10 @@
namespace aoc2022.Util;
public struct ivec3 : IEquatable<ivec3>, IComparable<ivec3>, IComparable
public readonly struct ivec3 : IEquatable<ivec3>, IComparable<ivec3>, IComparable
{
public long x = 0;
public long y = 0;
public long z = 0;
public readonly long x = 0;
public readonly long y = 0;
public readonly long z = 0;
public static readonly ivec3 ZERO = new ivec3(0, 0, 0);
public static readonly ivec3 ONE = new ivec3(1, 1, 1);
@ -31,10 +31,8 @@ public struct ivec3 : IEquatable<ivec3>, IComparable<ivec3>, IComparable
public long MinElement => System.Math.Min(x, System.Math.Min(y, z));
public ivec3 GetRotatedLeft() => new ivec3(y, -x, z);
public void RotateLeft() => this = GetRotatedLeft();
public ivec3 GetRotatedRight() => new ivec3(-y, x, z);
public void RotateRight() => this = GetRotatedRight();
public long Dot(ivec3 v) => (x * v.x) + (y * v.y) + (z * v.z);
public long LengthSquared => (x * x) + (y * y) + (z * z);