mirror of
https://github.com/parnic/advent-of-code-2022.git
synced 2025-06-16 05:30:14 -05:00
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:
12
src/17.cs
12
src/17.cs
@ -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
|
||||
{
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user