-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay20.cs
181 lines (167 loc) · 4.2 KB
/
Day20.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace AdventofCode2022 {
internal static class DayTwenty {
internal static long Part1(string input) {
string[] lines = input.Split('\n');
int sum = 0;
List<int> carry = new List<int>();
foreach(string lin in lines) {
int l = int.Parse(lin);
carry.Add(l);
}
List<(int index,int value)> pairs = new List<(int index, int value)>();
int ind = 0;
(int index, int value) origin = (0,0);
foreach(int i in carry)
{
if (i == 0) origin = (ind, i);
pairs.Add((ind, i));
ind++;
}
RotationalQueue<(int index, int value)> queue = new RotationalQueue<(int index, int value)>(pairs);
Mix(ref queue, pairs);
int indZero = queue.IndexOf(origin);
queue.RotateLeft(indZero);
queue.RotateLeft(1000);
(int index, int value) a = queue.Peek();
queue.RotateLeft(1000);
(int index, int value) b = queue.Peek();
queue.RotateLeft(1000);
(int index, int value) c = queue.Peek();
return a.value + b.value + c.value;
}
private static void Mix(ref RotationalQueue<(int index, int value)> queue, List<(int index, int value)> order)
{
foreach((int,int) i in order)
{
int f = queue.IndexOf(i);
queue.RotateLeft(f);
(int index, int value) j = queue.Dequeue();
if (j.value > 0)
{
queue.RotateLeft(j.value);
}
else if (j.value < 0)
{
queue.RotateRight(-j.value);
}
queue.Enqueue(j);
}
}
private static void MixLong(ref RotationalQueue<(long index, long value)> queue, List<(long index, long value)> order)
{
for (int k = 0; k < 10; k++)
{
foreach ((long, long) i in order)
{
long f = queue.IndexOf(i);
queue.RotateLeft(f);
(long index, long value) j = queue.Dequeue();
if (j.value > 0)
{
queue.RotateLeft(j.value);
}
else if (j.value < 0)
{
queue.RotateRight(-j.value);
}
queue.Enqueue(j);
}
}
}
internal static long Part2(string input) {
string[] lines = input.Split('\n');
int sum = 0;
List<long> carry = new List<long>();
foreach (string lin in lines)
{
int l = int.Parse(lin);
carry.Add(l* 811589153L);
}
List<(long index, long value)> pairs = new List<(long index, long value)>();
int ind = 0;
(long index, long value) origin = (0, 0);
foreach (long i in carry)
{
if (i == 0) origin = (ind, i);
pairs.Add((ind, i));
ind++;
}
RotationalQueue<(long index, long value)> queue = new RotationalQueue<(long index, long value)>(pairs);
MixLong(ref queue, pairs);
int indZero = queue.IndexOf(origin);
queue.RotateLeft(indZero);
queue.RotateLeft(1000);
(long index, long value) a = queue.Peek();
queue.RotateLeft(1000);
(long index, long value) b = queue.Peek();
queue.RotateLeft(1000);
(long index, long value) c = queue.Peek();
return a.value + b.value + c.value;
}
public class RotationalQueue<T> : IEnumerable<T>
{
List<T> backingList;
public int IndexOf(T val)
{
return backingList.IndexOf(val);
}
public RotationalQueue(List<T> values)
{
backingList = new List<T>();
backingList.AddRange(values);
}
public void RotateLeft(long rotate)
{
int dist = (int)rotate;
if (rotate < backingList.Count)
{
backingList = backingList.Skip(dist).Concat(backingList.Take(dist)).ToList();
}
else
{
rotate = rotate % backingList.Count;
RotateLeft(rotate);
}
}
public void RotateRight(long rotate)
{
int dist = (int)rotate;
if (rotate < backingList.Count)
{
backingList = backingList.Skip(backingList.Count - dist).Concat(backingList.Take(backingList.Count - dist)).ToList();
}
else
{
rotate = rotate % backingList.Count;
RotateRight(rotate);
}
}
public void Enqueue(T val)
{
backingList.Add(val);
}
public T Dequeue()
{
T r = backingList[0];
backingList.RemoveAt(0);
return r;
}
public IEnumerator<T> GetEnumerator()
{
return backingList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return backingList.GetEnumerator();
}
public T Peek()
{
return backingList[0];
}
}
}
}