-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
2,570 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.