Skip to content

Commit 389a54e

Browse files
author
Tim Borowski
committed
Day 20 and Day 21
1 parent be70646 commit 389a54e

File tree

8 files changed

+8305
-0
lines changed

8 files changed

+8305
-0
lines changed

Day 20/Day 20.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33205.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day 20", "Day 20\Day 20.csproj", "{42ADBB80-6F2D-4B81-8620-D7CB7C871A3A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{42ADBB80-6F2D-4B81-8620-D7CB7C871A3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{42ADBB80-6F2D-4B81-8620-D7CB7C871A3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{42ADBB80-6F2D-4B81-8620-D7CB7C871A3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{42ADBB80-6F2D-4B81-8620-D7CB7C871A3A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {583C21DC-5A9E-4E3F-967D-7DB8328F77BA}
24+
EndGlobalSection
25+
EndGlobal

Day 20/Day 20/Day 20.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<RootNamespace>Day_20</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<None Update="input.txt">
13+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14+
</None>
15+
</ItemGroup>
16+
17+
</Project>

Day 20/Day 20/Program.cs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.CompilerServices;
3+
4+
namespace Day_20
5+
{
6+
// 8556
7+
// 8105
8+
// 4439
9+
// 21100 too high
10+
11+
internal class Program
12+
{
13+
static void Main(string[] args)
14+
{
15+
var numbers = File.ReadLines("input.txt").Select(long.Parse).Select(x => x * 811589153).ToArray();
16+
17+
Dictionary<long, LinkedListNode<long>> nodeLookup = new();
18+
LinkedList<long> linkedList = new();
19+
20+
21+
LinkedListNode<long> val0 = null;
22+
23+
var i = 0;
24+
foreach (var number in numbers)
25+
{
26+
var node = new LinkedListNode<long>(number);
27+
nodeLookup.Add(i, node);
28+
linkedList.AddLast(node);
29+
i++;
30+
31+
if (number == 0)
32+
{
33+
val0 = node;
34+
}
35+
}
36+
37+
Console.WriteLine(string.Join(", ", linkedList));
38+
//Console.WriteLine(string.Join(", ", linkedList));
39+
40+
for (var z = 0; z < 10; z++)
41+
{
42+
for (var ii = 0; ii < linkedList.Count; ii++)
43+
{
44+
Mix(ii, linkedList, nodeLookup);
45+
//Console.WriteLine(string.Join(", ", linkedList));
46+
}
47+
}
48+
49+
Console.WriteLine(string.Join(", ", linkedList));
50+
Console.WriteLine("finish");
51+
52+
53+
54+
var iterator = val0;
55+
var sum = 0l;
56+
for (long z = 0; z < 3; z++)
57+
{
58+
for (long t = 0; t < 1000; t++)
59+
{
60+
if (iterator == linkedList.Last)
61+
{
62+
iterator = linkedList.First;
63+
continue;
64+
}
65+
iterator = iterator.Next;
66+
}
67+
68+
sum += iterator.Value;
69+
Console.WriteLine(iterator.Value);
70+
}
71+
Console.WriteLine(sum);
72+
73+
}
74+
75+
private static void Mix(long index, LinkedList<long> linkedList, Dictionary<long, LinkedListNode<long>> nodes)
76+
{
77+
var originalNode = nodes[index];
78+
79+
var numberToMix = originalNode.Value;
80+
if (numberToMix > 0)
81+
{
82+
var iteratorNode = GetNextNode(linkedList, originalNode);
83+
linkedList.Remove(originalNode);
84+
var targetNode = GetNextNodeSlow(linkedList, numberToMix - 1, iteratorNode);
85+
linkedList.AddAfter(targetNode, originalNode);
86+
}
87+
88+
if (numberToMix < 0)
89+
{
90+
var iteratorNode = GetPrevNode(linkedList, originalNode);
91+
//Console.WriteLine($"prev node is {iteratorNode.Value}");
92+
linkedList.Remove(originalNode);
93+
var targetNode = GetPrevNodeSlow(linkedList, Math.Abs(numberToMix) - 1, iteratorNode);
94+
linkedList.AddBefore(targetNode, originalNode);
95+
}
96+
}
97+
98+
private static LinkedListNode<long> GetNextNodeSlow(LinkedList<long> linkedList, long numberToMix, LinkedListNode<long> iteratorNode)
99+
{
100+
numberToMix %= linkedList.Count;
101+
for (long i = 0; i < numberToMix; i++)
102+
{
103+
iteratorNode = GetNextNode(linkedList, iteratorNode);
104+
}
105+
return iteratorNode;
106+
}
107+
108+
private static LinkedListNode<long> GetPrevNodeSlow(LinkedList<long> linkedList, long numberToMix, LinkedListNode<long> iteratorNode)
109+
{
110+
numberToMix %= linkedList.Count;
111+
for (long i = 0; i < numberToMix; i++)
112+
{
113+
iteratorNode = GetPrevNode(linkedList, iteratorNode);
114+
//Console.WriteLine($"iterator node is now {iteratorNode.Value}");
115+
}
116+
return iteratorNode;
117+
}
118+
119+
private static LinkedListNode<long> GetNextNode(LinkedList<long> linkedList, LinkedListNode<long> nodeIterator)
120+
{
121+
if (nodeIterator == linkedList.Last)
122+
{
123+
return linkedList.First;
124+
}
125+
return nodeIterator.Next;
126+
}
127+
128+
private static LinkedListNode<long> GetPrevNode(LinkedList<long> linkedList, LinkedListNode<long> nodeIterator)
129+
{
130+
if (nodeIterator == linkedList.First)
131+
{
132+
return linkedList.Last;
133+
}
134+
return nodeIterator.Previous;
135+
}
136+
}
137+
138+
internal static class IndexOfExtension {
139+
140+
public static long IndexOf<T>(this LinkedList<T> linkedList, LinkedListNode<T> node)
141+
{
142+
var index = 0;
143+
var iterator = linkedList.First;
144+
145+
while (iterator != node)
146+
{
147+
index++;
148+
iterator = iterator.Next;
149+
}
150+
151+
return index;
152+
}
153+
154+
public static LinkedListNode<T> NodeAt<T>(this LinkedList<T> linkedList, long index)
155+
{
156+
var iterator = linkedList.First;
157+
158+
for (long i = 0; i < index; i++)
159+
{
160+
iterator = iterator.Next;
161+
}
162+
163+
return iterator;
164+
}
165+
166+
}
167+
}

0 commit comments

Comments
 (0)