Skip to content

Commit

Permalink
2022 day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckries committed Dec 20, 2022
1 parent bb9df32 commit 528f29b
Show file tree
Hide file tree
Showing 11 changed files with 5,254 additions and 35 deletions.
8 changes: 8 additions & 0 deletions AdventOfCode.2018/AdventOfCode.2018.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
Expand Down
3 changes: 3 additions & 0 deletions AdventOfCode.2018/Day22.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public void Part1()
[Fact]
public void Part2()
{
Stopwatch sw = Stopwatch.StartNew();
int answer = Search();
var elapsed = sw.Elapsed;
Assert.Equal(elapsed, default);
Assert.Equal(999, answer);
}

Expand Down
10 changes: 2 additions & 8 deletions AdventOfCode.2018/Day23.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ readonly struct BoundingBox
public readonly IntVec3 Max;
public readonly IntVec3 Center;
public readonly bool IsPoint;
public readonly long Volume;

public BoundingBox(in IntVec3 min, in IntVec3 max)
{
Expand All @@ -63,8 +62,7 @@ public BoundingBox(in IntVec3 min, in IntVec3 max)
Min = min;
Max = max;
Center = Min + ((Max - Min) / 2);
Volume = (Max.X - Min.X + 1) * (Max.Y - Min.Y + 1) * (Max.Z - Min.Z + 1);
IsPoint = Volume == 1;
IsPoint = min.X == max.X && min.Y == max.Y && min.Z == max.Z;
}

public IntVec3 Closest(in IntVec3 point)
Expand Down Expand Up @@ -138,11 +136,7 @@ public void Part2()
int value = right.botsInRange - left.botsInRange;
if (value == 0)
{
value = (int)(left.box.Volume - right.box.Volume);
if (value == 0)
{
value = left.box.Center.Manhattan - right.box.Center.Manhattan;
}
value = left.box.Center.Manhattan - right.box.Center.Manhattan;
}

return value;
Expand Down
65 changes: 56 additions & 9 deletions AdventOfCode.2019/Day18.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AdventOfCode.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -80,20 +81,68 @@ private int DynamicHelper(int pos, int keystate, int keycount, Dictionary<(int p
return states[(pos, keystate)];
}

class ArrayEqualityComparer : IEqualityComparer<int[]>
{
public bool Equals(int[] x, int[] y)
{
if (x.Length != y.Length)
return false;

for (int i = 0; i < x.Length; i++)
{
if (x[i] != y[i])
return false;
}

return true;
}

public int GetHashCode([DisallowNull] int[] obj)
{
if (obj.Length == 0)
{
return obj.GetHashCode();
}

int hash = HashCode.Combine(obj[0]);
for (int i = 1; i < obj.Length; i++)
{
hash = HashCode.Combine(hash, obj[i]);
}
return hash;
}
}

class EqualityComparer : IEqualityComparer<(int[], int)>
{
static ArrayEqualityComparer s_arrayComparer = new ArrayEqualityComparer();

public bool Equals((int[], int) x, (int[], int) y)
{
return x.Item2 == y.Item2 && s_arrayComparer.Equals(x.Item1, y.Item1);
}

public int GetHashCode([DisallowNull] (int[], int) obj)
{
return HashCode.Combine(obj.Item2, s_arrayComparer.GetHashCode(obj.Item1));
}
}

public int MinDistance4()
{
Dictionary<(int, int, int, int, int), int> states = new Dictionary<(int, int, int, int, int), int>((1 << (_count - 4)) - 1);
Dictionary<(int[], int), int> states = new(new EqualityComparer());
return DynamicHelper4(new int[] { _count - 4, _count - 3, _count - 2, _count - 1 }, 0, 0, states);
}

private int DynamicHelper4(int[] pos, int keyState, int keycount, Dictionary<(int, int, int, int, int), int> states)
private int DynamicHelper4(int[] pos, int keyState, int keycount, Dictionary<(int[], int), int> states)
{
if (keycount == _count - 4)
return 0;

if (states.TryGetValue((pos[0], pos[1], pos[2], pos[3], keyState), out int cached))
if (states.TryGetValue((pos, keyState), out int cached))
return cached;

int min = int.MaxValue;
for (int cPos = 0; cPos < pos.Length; cPos++)
{
int oldPos = pos[cPos];
Expand All @@ -113,14 +162,12 @@ private int DynamicHelper4(int[] pos, int keyState, int keycount, Dictionary<(in
int delta = DynamicHelper4(pos, keyState | edge.SinkKeyFlag, keycount + 1, states);
int distance = delta == int.MaxValue ? int.MaxValue : edge.Distance + delta;
pos[cPos] = oldPos;
if (!states.TryGetValue((pos[0], pos[1], pos[2], pos[3], keyState), out int minDistance) || distance < minDistance)
states[(pos[0], pos[1], pos[2], pos[3], keyState)] = distance;
if (distance < min)
min = distance;
}
}

if (states.TryGetValue((pos[0], pos[1], pos[2], pos[3], keyState), out int returnValue))
return returnValue;
return int.MaxValue;
states[(pos, keyState)] = min;
return min;
}

int _count;
Expand Down
2 changes: 1 addition & 1 deletion AdventOfCode.2020/AdventOfCode.2020.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>AdventOfCode._2020</RootNamespace>

<IsPackable>false</IsPackable>
Expand Down
51 changes: 35 additions & 16 deletions AdventOfCode.2021/Day19.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace AdventOfCode._2021
{
public class Day19
{
List<List<IntVec3>> _scanners;
[DebuggerDisplay("{Index,nq}")]
private class Scanner
{
private readonly IntVec3[] _points;
private readonly IntVec3[][] _rotatedPoints;

public readonly int Index;

public IEnumerable<IntVec3> Points => _points;

public Scanner(int index, IEnumerable<IntVec3> points)
{
Index = index;
_points = points.ToArray();
_rotatedPoints = new IntVec3[24][];
}

public ICollection<IntVec3> GetRotatedPoints(int rot)
{
return _rotatedPoints[rot] ??= _points.Select(p => RotatePoint(p, rot)).ToArray();
}
}

List<Scanner> _scanners;

public Day19()
{
Expand All @@ -19,14 +37,15 @@ public Day19()
_ = sr.ReadLine();

List<IntVec3> current = new();
int index = 0;
string? line;
while (true)
{
line = sr.ReadLine();

if (string.IsNullOrEmpty(line))
{
_scanners.Add(current);
_scanners.Add(new(index++, current));
if (line is null)
break;

Expand Down Expand Up @@ -59,23 +78,23 @@ public void Part2()

private (HashSet<IntVec3>, int) Solve()
{
HashSet<IntVec3> points = new(_scanners[0]);
List<List<IntVec3>> unknownScanners = _scanners.Skip(1).ToList();
HashSet<IntVec3> points = new(_scanners[0].Points);
List<Scanner> unknownScanners = _scanners.Skip(1).ToList();
List<IntVec3> offsets = new();

while (unknownScanners.Count > 0)
{
List<IntVec3>? foundScanner = null;
List<IntVec3>? resolvedScanner = null;
Scanner? foundScanner = null;
IntVec3[]? resolvedScanner = null;
foreach (var candScanner in unknownScanners)
for (int i = 0; i < 24; i++)
{
List<IntVec3> rotation = candScanner.Select(p => RotatePoint(p, i)).ToList();
ICollection<IntVec3> rotation = candScanner.GetRotatedPoints(i);
if (TryMatchScanners(points, rotation, out IntVec3 offset))
{
offsets.Add(offset);
foundScanner = candScanner;
resolvedScanner = rotation.Select(p => p + offset).ToList();
resolvedScanner = rotation.Select(p => p + offset).ToArray();
goto found;
}
}
Expand All @@ -100,7 +119,7 @@ public void Part2()
return (points, maxDistance);
}

private bool TryMatchScanners(ICollection<IntVec3> a, List<IntVec3> b, out IntVec3 scannerOffset)
private bool TryMatchScanners(ICollection<IntVec3> a, ICollection<IntVec3> b, out IntVec3 scannerOffset)
{
scannerOffset = IntVec3.Zero;
int offsetCount;
Expand All @@ -125,7 +144,7 @@ private bool TryMatchScanners(ICollection<IntVec3> a, List<IntVec3> b, out IntVe
return false;
}

private IntVec3 RotatePoint(IntVec3 p, int rot)
private static IntVec3 RotatePoint(IntVec3 p, int rot)
{
return rot switch
{
Expand Down
31 changes: 31 additions & 0 deletions AdventOfCode.2022/AdventOfCode.2022.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>AdventOfCode._2022</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Update="Inputs\*.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading

0 comments on commit 528f29b

Please sign in to comment.