-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day6.cs
49 lines (44 loc) · 1.62 KB
/
Day6.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
using System.Collections;
public class Day6
{
public static async Task<string> Part1Async(bool example = false)
{
var data = await ParseInput(example);
var days = 80;
for (var day = 0; day < days; day++)
{
var newFish = data.Count(i => i == 0);//count numbers of 0's
data = data.Select(i => i = (i == 0) ? 6 : i - 1).ToArray();//decrease all
data = data.Concat(Enumerable.Repeat(8, newFish));//add new
}
return data.Count().ToString();
}
public static async Task<string> Part2Async(bool example = false)
{
//changed algorith because part1 was too slow for this
//now keeping track of the number of fish in each status
var data = await ParseInput(example);
var counts = new long[9];
for (int i = 0; i < 9; i++)
{
counts[i] = data.Count(x => x == i);
}
var days = 256;
for (var day = 0; day < days; day++)
{
var newFish = counts[0]; //the number of new fish
for (int i = 0; i < 8; i++)
{
counts[i] = counts[i + 1]; //each day gets the fish from the next day
}
counts[6] += newFish; //fish that were 0 go to day 6
counts[8] = newFish; // new fish go to day 8
}
return counts.Sum().ToString();
}
private static async Task<IEnumerable<int>> ParseInput(bool example)
{
var input = await File.ReadAllTextAsync($"inputs/day6{(example ? ".example" : string.Empty)}.txt");
return input.Split(",").Select(i => int.Parse(i));
}
}