Skip to content

Commit

Permalink
Day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkAD88 committed Dec 10, 2023
1 parent 6086b3e commit 9339701
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ C# implementations and tests using .NET 8 for the [Advent of Code 2023](https://
```text
--------Part 1--------- --------Part 2---------
Day Time Rank Score Time Rank Score
10 04:02:42 12311 0 - - -
9 01:30:02 10494 0 02:28:42 12495 0
8 00:59:01 11313 0 >24h 52028 0
7 03:00:54 15487 0 04:19:09 15254 0
Expand Down Expand Up @@ -36,3 +37,6 @@ Wow. Refactored my step finder, implemented the Chinese Remainder Theorem, and

## Day 09
Got started late and I'm still trying to get use to developing on my Mac. Decades of muscle memory isn't easy to overcome but I'm getting more comfortable with VSCode vs. Visual Studio. I still feel its a big step down. Might need to grab Rider and see how I feel with it. The first part of the puzzle wasn't that hard. The second part tripped me up - I just couldn't recognize the pattern for predicting the before state of the numbers. I must have stared at it and jotted down notes for 45 minutes before I stumbled upon a mathmatical pattern. There has to be a simpler way than the hack I used.

## Day 10
Part one tripped me up. I don't have a clue how to accomplish Part 2.
155 changes: 155 additions & 0 deletions code/Day10.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace code;

public static class Day10
{
public static long TaskOne(string[] lines)
{
var (start, vector1, vector2) = FindStartPointAndInitialVectors(lines);

var forward = start + vector1;
var backward = start + vector2;

int moves = 1;
do
{
Move(forward, vector1, lines, out vector1);
forward += vector1;
Move(backward, vector2, lines, out vector2);
backward += vector2;
moves++;
} while (forward != backward);

return moves;
}

private static void Move(Point point, Size vector, string[] lines, out Size nextVector)
{
nextVector = vector;
if (vector == new Size(0, -1))
{
MoveUp(point, lines, out nextVector);
}
else if (vector == new Size(0, 1))
{
MoveDown(point, lines, out nextVector);
}
else if (vector == new Size(-1, 0))
{
MoveLeft(point, lines, out nextVector);
}
else if (vector == new Size(1, 0))
{
MoveRight(point, lines, out nextVector);
}
}

private static void MoveUp(Point point, string[] lines, out Size nextVector)
{
var next = GetMapValue(point.X, point.Y, lines);
nextVector = next switch
{
'|' => new (0, -1),
'7' => new (-1, 0),
'F' => new (1, 0),
_ => throw new ArgumentNullException($"Invalid character enountered - {next}")
};
}

private static void MoveDown(Point point, string[] lines, out Size nextVector)
{
var next = GetMapValue(point.X, point.Y, lines);
nextVector = next switch
{
'|' => new (0, 1),
'L' => new (1, 0),
'J' => new (-1, 0),
_ => throw new ArgumentNullException($"Invalid character enountered - {next}")
};
}

private static void MoveLeft(Point point, string[] lines, out Size nextVector)
{
var next = GetMapValue(point.X, point.Y, lines);
nextVector = next switch
{
'-' => new (-1, 0),
'L' => new (0, -1),
'F' => new (0, 1),
_ => throw new ArgumentNullException($"Invalid character enountered - {next}")
};
}

private static void MoveRight(Point point, string[] lines, out Size nextVector)
{
var next = GetMapValue(point.X, point.Y, lines);
nextVector = next switch
{
'-' => new (1, 0),
'J' => new (0, -1),
'7' => new (0, 1),
_ => throw new ArgumentNullException($"Invalid character enountered - {next}")
};
}

public static long TaskTwo(string[] lines)
{
return 0;
}

private static (Point, Size, Size) FindStartPointAndInitialVectors(string[] lines)
{
Point start = default;
for (int y = 0; y < lines.Length; y++)
{
var x = lines[y].IndexOf('S');
if (x >= 0)
{
start = new (x, y);
}
}

var up = GetMapValue(start.X, start.Y - 1, lines);
var down = GetMapValue(start.X, start.Y + 1, lines);
var left = GetMapValue(start.X - 1, start.Y, lines);
var right = GetMapValue(start.X + 1, start.Y, lines);

List<Size> vectors = [];
if ("|7F".Contains(up))
{
vectors.Add(new(0, -1));
}

if ("|LJ".Contains(down))
{
vectors.Add(new(0, 1));
}

if ("-LF".Contains(left))
{
vectors.Add(new(-1, 0));
}

if("-J7".Contains(right))
{
vectors.Add(new(1, 0));
}

return (start, vectors.FirstOrDefault(), vectors.Skip(1).FirstOrDefault());
}

private static char GetMapValue(int x, int y, string[] lines)
{
if (x < 0 || x >= lines[0].Length || y < 0 || y >= lines.Length)
{
return char.MinValue;
}

return lines[y][x];
}
}
9 changes: 9 additions & 0 deletions code/code.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
<None Update="inputs\day09-full.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="inputs\day10-sample-1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="inputs\day10-sample-2.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="inputs\day10-full.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>


</ItemGroup>
Expand Down
Loading

0 comments on commit 9339701

Please sign in to comment.