-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day13.cs
59 lines (46 loc) · 1.55 KB
/
Day13.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Numerics;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2016.Solvers;
public class Day13 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int favouriteNumber = new SpanReader(input).ReadPosIntUntil('\n');
var seen = new HashSet<(int, int)>();
int steps = 0;
var frontier = new HashSet<(int, int)> { (1, 1) };
int? part1 = null;
int? part2 = null;
while (part1 == null || part2 == null)
{
var newFrontier = new HashSet<(int, int)>();
foreach ((int x, int y) in frontier)
{
if (x == 31 && y == 39)
part1 = steps;
if (IsWall(x, y) || seen.Contains((x, y)))
continue;
seen.Add((x, y));
newFrontier.Add((x - 1, y));
newFrontier.Add((x + 1, y));
newFrontier.Add((x, y - 1));
newFrontier.Add((x, y + 1));
}
if (steps == 50)
part2 = seen.Count;
frontier = newFrontier;
steps++;
}
solution.SubmitPart1(part1.Value);
solution.SubmitPart2(part2.Value);
bool IsWall(int x, int y)
{
if (x < 0 || y < 0)
return true;
int sum = x * x + 3 * x + 2 * x * y + y + y * y + favouriteNumber;
return BitOperations.PopCount((uint)sum) % 2 == 1;
}
}
}