-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay06.cs
54 lines (42 loc) · 1.16 KB
/
Day06.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
namespace AdventOfCode._2021
{
public class Day06
{
int[] _input;
public Day06()
{
_input = File.ReadAllText("Inputs/Day06.txt").Split(',').Select(int.Parse).ToArray();
}
[Fact]
public void Part1()
{
ulong answer = Run(80);
Assert.Equal(386755ul, answer);
}
[Fact]
public void Part2()
{
ulong answer = Run(256);
Assert.Equal(1732731810807ul, answer);
}
private ulong Run(int iterations)
{
ulong[] fish = new ulong[9];
ulong[] next = (ulong[])fish.Clone();
foreach (int start in _input)
fish[start]++;
for (int i = 0; i < iterations; i++)
{
for (int j = 0; j < 8; j++)
next[j] = fish[j + 1];
next[6] += fish[0];
next[8] = fish[0];
(fish, next) = (next, fish);
}
ulong total = 0;
for (int i = 0; i < fish.Length; i++)
total += fish[i];
return total;
}
}
}