Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions test/AdventOfCode.2022/Day03.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace AdventOfCode._2022;

public class Day03
{
List<string> _input;

public Day03()
{
_input = File.ReadAllLines("Inputs/Day03.txt").ToList();
}

[Fact]
public void Part1()
{
int answer = _input
.Select(s => (a: s[..(s.Length / 2)], b: s[(s.Length / 2)..]))
.Select(pair => pair.a.Intersect(pair.b).Single()).Sum(Score);
Assert.Equal(7793, answer);
}

[Fact]
public void Part2()
{
int answer = _input.Chunk(3)
.Select(strs => strs[1..]
.Aggregate(new HashSet<char>(strs[0]), (acc, s) =>
{
acc.IntersectWith(s);
return acc;
})
.Single())
.Sum(Score);

Assert.Equal(2499, answer);
}

private static int Score(char c) => c switch
{
>= 'a' and <= 'z' => c - 'a' + 1,
>= 'A' and <= 'Z' => c - 'A' + 27,
_ => throw new InvalidOperationException()
};
}
39 changes: 39 additions & 0 deletions test/AdventOfCode.2022/Day04.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace AdventOfCode._2022;

public class Day04
{
List<((int x, int y) a, (int x, int y) b)> input;

public Day04()
{
input = File.ReadAllLines("Inputs/Day04.txt")
.Select(line =>
{
var nums = line.Split([',', '-']).Select(int.Parse).ToArray();
return ((nums[0], nums[1]), (nums[2], nums[3]));
}).ToList();
}

[Fact]
public void Part1()
{
int answer = input.Count(pair =>
{
return pair.a.x <= pair.b.x && pair.a.y >= pair.b.y ||
pair.b.x <= pair.a.x && pair.b.y >= pair.a.y;
});

Assert.Equal(503, answer);
}

[Fact]
public void Part2()
{
int answer = input.Count(pair =>
{
return !(pair.a.x > pair.b.y || pair.a.y < pair.b.x || pair.b.x > pair.b.y || pair.b.y < pair.a.x);
});

Assert.Equal(827, answer);
}
}
89 changes: 89 additions & 0 deletions test/AdventOfCode.2022/Day05.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace AdventOfCode._2022;

public class Day05
{
List<Stack<char>> _stacks = new();
List<(int count, int from, int to)> _moves = new();

public Day05()
{
var lines = File.ReadAllLines("Inputs/Day05.txt");
int emptyLine = -1;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i] == string.Empty)
{
emptyLine = i;
break;
}
}

string idxLine = lines[emptyLine - 1];
for (int i = 0; i < idxLine.Length; i++)
{
if (idxLine[i] != ' ')
{
Stack<char> stack = new();
int y = emptyLine - 1;
while (y >= 0)
{
if (lines[y][i] != ' ')
{
stack.Push(lines[y][i]);
}
else
{
break;
}
y--;
}
_stacks.Add(stack);
}
}

for (int i = emptyLine + 1; i < lines.Length; i++)
{
var split = lines[i].Split(' ');
int count = int.Parse(split[1]);
int from = int.Parse(split[3]);
int to = int.Parse(split[5]);
_moves.Add((count, from - 1, to - 1));
}
}

[Fact]
public void Part1()
{
foreach ((int count, int from, int to) in _moves)
{
for (int i = 0; i < count; i++)
{
_stacks[to].Push(_stacks[from].Pop());
}
}

string answer = new(_stacks.Select(s => s.Peek()).ToArray());
Assert.Equal("VQZNJMWTR", answer);
}

[Fact]
public void Part2()
{
Stack<char> temp = new();
foreach ((int count, int from, int to) in _moves)
{
for (int i = 0; i < count; i++)
{
temp.Push(_stacks[from].Pop());
}

for (int i = 0; i < count; i++)
{
_stacks[to].Push(temp.Pop());
}
}

string answer = new(_stacks.Select(s => s.Peek()).ToArray());
Assert.Equal("NLCDCLVMQ", answer);
}
}
61 changes: 61 additions & 0 deletions test/AdventOfCode.2022/Day06.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Runtime.ExceptionServices;

namespace AdventOfCode._2022;

public class Day06
{
string _input;

public Day06()
{
_input = File.ReadAllText("Inputs/Day06.txt");
}

private int Search(int count)
{
Dictionary<char, int> counts = new();

for (int i = 0; i < count; i++)
{
if (!counts.ContainsKey(_input[i]))
{
counts[_input[i]] = 0;
}
counts[_input[i]]++;
}

int front = 0;
int back = count;

while (true)
{
if (counts.Count == count)
return back;

counts[_input[front]]--;
if (counts[_input[front]] == 0)
counts.Remove(_input[front]);

if (!counts.ContainsKey(_input[back]))
counts[_input[back]] = 0;
counts[_input[back]]++;

front++;
back++;
}
}

[Fact]
public void Part1()
{
int answer = Search(4);
Assert.Equal(1542, answer);
}

[Fact]
public void Part2()
{
int answer = Search(14);
Assert.Equal(3153, answer);
}
}
148 changes: 148 additions & 0 deletions test/AdventOfCode.2022/Day07.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System.Net.Security;
using System.Text;

namespace AdventOfCode._2022;

public class Day07
{
private abstract class Node
{
public required Dir? Parent { get; init; }

public required string Name { get; init; }
}

private class Dir : Node
{
public Dictionary<string, Node> Children { get; } = new();
}

private class File : Node
{
public required long Size { get; init; }
}

Dir _root;

public Day07()
{
_root = new Dir()
{
Name = "/",
Parent = null
};

List<string[]> input = System.IO.File.ReadAllLines("Inputs/Day07.txt").Select(s => s.Split(' ')).ToList();

int i = 0;
Dir current = _root;
while (i < input.Count)
{
string[] line = input[i];

if (line[0] == "$")
{
if (line[1] == "cd")
{
var target = line[2];

current = target switch
{
".." => current.Parent ?? throw new InvalidOperationException(),
"/" => _root,
_ => (Dir)current.Children[target]
};

i++;
}
else if (line[1] == "ls")
{
while (true)
{
i++;
if(i >= input.Count)
break;
line = input[i];
if (line[0] == "$")
break;

string name = line[1];

if (line[0] == "dir")
{
current.Children.Add(name, new Dir()
{
Name = name,
Parent = current
});
}
else
{
long size = long.Parse(line[0]);
current.Children.Add(name, new File()
{
Name = name,
Size = size,
Parent = current,
});
}
}
}
else
{
throw new InvalidOperationException();
}
}
else
{
throw new InvalidOperationException();
}
}
}

long SumVisit(Node n, Action<long> visitor)
{
switch (n)
{
case File file:
return file.Size;
case Dir dir:
long sum = dir.Children.Values.Sum(n => SumVisit(n, visitor));
visitor(sum);
return sum;
default:
throw new InvalidOperationException();
}
}

[Fact]
public void Part1()
{
long answer = 0;
SumVisit(_root, total =>
{
if (total <= 100000)
answer += total;
});

Assert.Equal(1723892, answer);
}

[Fact]
public void Part2()
{
long total = SumVisit(_root, _ => { });
long min_required = 30000000 - (70000000 - total);

long min_dir = long.MaxValue;
SumVisit(_root, size =>
{
if (size >= min_required && size < min_dir)
{
min_dir = size;
}
});

Assert.Equal(8474158, min_dir);
}
}
Loading