-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.cs
34 lines (32 loc) · 898 Bytes
/
Day15.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
using System;
using System.Linq;
using System.Collections.Generic;
const string input = "17,1,3,16,19,0";
long SpokenAt(long lastTurn)
{
var spoken = new Dictionary<long, (long, long)>();
long turn = 1;
long last = -1;
foreach (long n in input.Split(',').Select(x => Convert.ToInt64(x)))
{
spoken.Add(n, (turn, -1));
last = n;
turn++;
}
for (; turn <= lastTurn; turn++)
{
long curr = spoken[last].Item2 == -1 ? 0 : spoken[last].Item1 - spoken[last].Item2;
if (spoken.ContainsKey(curr))
{
spoken[curr] = (turn, spoken[curr].Item1);
}
else
{
spoken.Add(curr, (turn, - 1));
}
last = curr;
}
return last;
}
Console.WriteLine("Part 1: {0}", SpokenAt(2020L));
Console.WriteLine("Part 2: {0}", SpokenAt(30000000L));