Skip to content

Commit

Permalink
Day 10 and random old stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckries committed Dec 10, 2021
1 parent ab5cff9 commit 1f90d42
Show file tree
Hide file tree
Showing 24 changed files with 2,570 additions and 126 deletions.
4 changes: 2 additions & 2 deletions AdventOfCode.2016/Day01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Day01()
[Fact]
public void Part1()
{
int answer = EnumPositions().Last().Distance;
int answer = EnumPositions().Last().Manhattan;
Assert.Equal(226, answer);
}

Expand All @@ -34,7 +34,7 @@ public void Part2()
{
if (!visited.Add(pos))
{
answer = pos.Distance;
answer = pos.Manhattan;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion AdventOfCode.2016/Day22.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static int SearchComparsion(SearchNode lhs, SearchNode rhs)

static int WeightedDistance(in SearchNode n)
{
return n.Steps + n.Data.Distance + n.EmptyTarget.Distance + n.Empty.DistanceFrom(n.EmptyTarget);
return n.Steps + n.Data.Manhattan + n.EmptyTarget.Manhattan + n.Empty.ManhattanDistanceFrom(n.EmptyTarget);
}
}

Expand Down
7 changes: 4 additions & 3 deletions AdventOfCode.2017/AdventOfCode.2017.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._2017</RootNamespace>

<IsPackable>false</IsPackable>
Expand Down Expand Up @@ -31,7 +31,8 @@
</ItemGroup>

<ItemGroup>
<None Remove="Inputs\Day10.txt" />
<None Remove="Inputs\Day16.txt" />
<None Remove="Inputs\Day11.txt" />
<None Remove="Inputs\Day12.txt" />
<None Remove="Inputs\Day13.txt" />
</ItemGroup>
</Project>
73 changes: 73 additions & 0 deletions AdventOfCode.2017/Day10.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -10,6 +12,77 @@ namespace AdventOfCode._2017
{
public class Day10
{
private class KnotHash
{
private int[] _list;
private int _current;
private int _skip;

public KnotHash(int size)
{
_list = Enumerable.Range(0, size).ToArray();
_current = 0;
_skip = 0;
}

public void Apply(int length)
{
int begin = _current;
int end = begin + length - 1;

for (int i = 0; i < length / 2; i++)
{
int tmp = _list[Index(begin)];
_list[Index(begin)] = _list[Index(end)];
_list[Index(end)] = tmp;

begin++;
end--;
}

Advance(length);
}

public IEnumerable<int> Enumerate() => _list;

private void Advance(int length)
{
_current = Index(_current + length + _skip++);
}

private int Index(int i) => i % _list.Length;
}

[Fact]
public void Part1()
{
int[] lengths = File.ReadAllText("Inputs/Day10.txt").Split(',').Select(int.Parse).ToArray();

KnotHash hash = new KnotHash(256);
foreach (int length in lengths)
hash.Apply(length);

int answer = hash.Enumerate().Take(2).Aggregate((x, y) => x * y);

Assert.Equal(23715, answer);
}

[Fact]
public void Part2()
{
int[] lengths = File.ReadAllText("Inputs/Day10.txt").Select(c => (int)c).Concat(new[] { 17, 31, 73, 47, 23 }).ToArray();
KnotHash hash = new KnotHash(256);

for (int i = 0; i < 64; i++)
foreach (int length in lengths)
hash.Apply(length);

string answer = hash.Enumerate()
.Chunk(16).Select(chunk => chunk.Aggregate((x, y) => x ^ y))
.Select(n => string.Format("{0:x2}", n))
.Aggregate(new StringBuilder(), (sb, s) => sb.Append(s), sb => sb.ToString());

Assert.Equal("541dc3180fd4b72881e39cf925a50253", answer);
}
}
}
68 changes: 68 additions & 0 deletions AdventOfCode.2017/Day11.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

using static System.Math;

namespace AdventOfCode._2017
{
public class Day11
{
string[] _input;

public Day11()
{
_input = File.ReadAllText("Inputs/Day11.txt").Split(',').ToArray();
}

[Fact]
public void Part1()
{
(int q, int r, int s) p = (0, 0, 0);

foreach (string dir in _input)
Move(dir, ref p);

int answer = Distance(p);
Assert.Equal(670, answer);
}

[Fact]
public void Part2()
{
int answer = Steps().Select(Distance).Max();
Assert.Equal(1426, answer);

IEnumerable<(int, int, int)> Steps()
{
(int q, int r, int s) p = (0, 0, 0);
foreach (string dir in _input)
{
Move(dir, ref p);
yield return p;
}
}
}

private static void Move(string dir, ref (int q, int r, int s) p)
{
switch (dir)
{
case "n": p.r--; p.s++; break;
case "ne": p.q++; p.r--; break;
case "se": p.q++; p.s--; break;
case "s": p.r++; p.s--; break;
case "sw": p.q--; p.r++; break;
case "nw": p.q--; p.s++; break;
default: throw new InvalidOperationException();
}
}

private static int Distance((int q, int r, int s) p) =>
(Abs(p.q) + Abs(p.r) + Abs(p.s)) / 2;
}
}
78 changes: 78 additions & 0 deletions AdventOfCode.2017/Day12.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace AdventOfCode._2017
{
public class Day12
{
List<List<int>> _graph;

public Day12()
{
string[] lines = File.ReadAllLines("Inputs/Day12.txt");

_graph = new(lines.Length);
for (int i = 0; i < lines.Length; i++)
_graph.Add(new());

for (int i = 0; i < lines.Length; i++)
foreach (int sink in lines[i].Split("<->")[1].Split(',', StringSplitOptions.TrimEntries).Select(int.Parse))
_graph[i].Add(sink);
}

[Fact]
public void Part1()
{
bool[] visited = new bool[_graph.Count];
int answer = Traverse(0, ref visited);

Assert.Equal(380, answer);
}

[Fact]
public void Part2()
{
bool[] visited = new bool[_graph.Count];
int count = 0;
for (int i = 0; i < _graph.Count; i++)
{
if (!visited[i])
{
count++;
Traverse(i, ref visited);
}
}

Assert.Equal(181, count);
}

private int Traverse(int start, ref bool[] visited)
{
Queue<int> toSearch = new Queue<int>();

toSearch.Enqueue(start);
int count = 0;
while (toSearch.Count > 0)
{
int current = toSearch.Dequeue();

if (visited[current])
continue;

visited[current] = true;
count++;

foreach (int next in _graph[current])
if (!visited[next])
toSearch.Enqueue(next);
}

return count;
}
}
}
58 changes: 58 additions & 0 deletions AdventOfCode.2017/Day13.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace AdventOfCode._2017
{
public class Day13
{
private class Scanner
{
public readonly int Depth;
public readonly int Range;

private readonly int _modulo;

public Scanner(int depth, int range)
{
Depth = depth;
Range = range;
_modulo = (range - 1) * 2;
}

public int PositionAtInitialTime(int t0) => (t0 + Depth) % _modulo;
}

Scanner[] _scanners;

public Day13()
{
_scanners = File.ReadAllLines("Inputs/Day13.txt")
.Select(s => s.Split(':', StringSplitOptions.TrimEntries).Select(int.Parse).ToArray())
.Select(tok => new Scanner(tok[0], tok[1]))
.ToArray();
}

[Fact]
public void Part1()
{
int answer = _scanners.Where(s => s.PositionAtInitialTime(0) == 0).Sum(s => s.Depth * s.Range);

Assert.Equal(1704, answer);
}

[Fact]
public void Part2()
{
int time = 0;
while (!_scanners.All(s => s.PositionAtInitialTime(time) != 0))
time++;

Assert.Equal(3970918, time);
}
}
}
16 changes: 2 additions & 14 deletions AdventOfCode.2017/Day15.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,8 @@ public static IEnumerable<ushort> Generate(uint factor, uint seed)
[Fact]
public void Part1()
{
//int count = 0;
//for (int i = 0; i < 40_000_000; i++)
//{
// _genA.Tick();
// _genB.Tick();

// if (_genA.Checksum == _genB.Checksum)
// count++;
//}

int count = Enumerable.Zip(
Generator.Generate(16807, 722),
Generator.Generate(48271, 354)
)
int count = Generator.Generate(16807, 722)
.Zip(Generator.Generate(48271, 354))
.Take(40_000_000)
.Count(pair => pair.First == pair.Second);

Expand Down
Loading

0 comments on commit 1f90d42

Please sign in to comment.