-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYear2020Day23.cs
140 lines (117 loc) · 3.15 KB
/
Year2020Day23.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdventOfCode.Common;
using JetBrains.Annotations;
namespace AdventOfCode.Solutions._2020._23;
[UsedImplicitly]
public class Year2020Day23 : ISolution
{
public object Part1(IEnumerable<string> input)
{
var cups = new LinkedList<int>();
foreach (var cup in input.First())
{
var cupValue = int.Parse(cup.ToString());
cups.AddLast(cupValue);
}
var currentCup = cups.First ?? throw new Exception("First is not found");
for (int i = 0; i < 100; i++)
{
var removedCups = new List<int>();
for (int j = 0; j < 3; j++)
{
var nextCup = currentCup.Next ?? currentCup.List?.First ?? throw new Exception();
cups.Remove(nextCup);
removedCups.Add(nextCup.Value);
}
var destinationValue = currentCup.Value - 1;
LinkedListNode<int>? destination = null;
while (true)
{
if (destinationValue <= 0)
{
var maxValue = cups.Max();
destination = cups.Find(maxValue);
break;
}
destination = cups.Find(destinationValue);
if (destination != null)
break;
destinationValue--;
}
foreach (var removedCup in removedCups)
{
cups.AddAfter(destination, new LinkedListNode<int>(removedCup));
destination = destination.Next;
}
currentCup = currentCup.Next ?? currentCup.List?.First ?? throw new Exception();
}
var sb = new StringBuilder();
var node = cups.Find(1);
while (true)
{
var next = node.Next ?? node.List?.First ?? throw new Exception();
if (next.Value == 1)
break;
sb.Append(next.Value);
node = next;
}
return sb;
}
public object Part2(IEnumerable<string> input)
{
var cups = new LinkedList<int>();
var cupsDict = new Dictionary<int, LinkedListNode<int>>();
foreach (var cup in input.First())
{
var cupValue = int.Parse(cup.ToString());
cupsDict[cupValue] = cups.AddLast(cupValue);
}
for (var i = cups.Max() + 1; i <= 1000000; i++)
{
cupsDict[i] = cups.AddLast(i);
}
var currentCup = cups.First ?? throw new Exception("First is not found");
for (int i = 0; i < 10000000; i++)
{
var removedCups = new List<int>(3);
for (int j = 0; j < 3; j++)
{
var nextCup = currentCup.Next ?? currentCup.List?.First ?? throw new Exception();
cups.Remove(nextCup);
cupsDict.Remove(nextCup.Value);
removedCups.Add(nextCup.Value);
}
var destinationValue = currentCup.Value - 1;
LinkedListNode<int>? destination;
while (true)
{
if (destinationValue <= 0)
{
var maxValue = cups.Max();
destination = cupsDict[maxValue];
break;
}
if (!cupsDict.ContainsKey(destinationValue))
{
destinationValue--;
continue;
}
destination = cupsDict[destinationValue];
break;
}
foreach (var removedCup in removedCups)
{
var addedNode = new LinkedListNode<int>(removedCup);
cups.AddAfter(destination, addedNode);
destination = destination.Next;
cupsDict.Add(removedCup, addedNode);
}
currentCup = currentCup.Next ?? currentCup.List?.First ?? throw new Exception();
}
var node = cups.Find(1);
return (long)node.Next.Value * node.Next.Next.Value;
}
}