-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022_20.cs
75 lines (63 loc) · 2.28 KB
/
2022_20.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
namespace AOC.CSharp;
public static class AOC2022_20
{
public static long Solve1(string[] lines)
{
List<long> orig = lines.Select(long.Parse).ToList();
return Solve1Efficient(orig, 1);
}
public static long Solve2(string[] lines)
{
List<long> orig = lines.Select(x => long.Parse(x) * 811589153L).ToList();
return Solve1Efficient(orig, 10);
}
private static long Solve1Efficient(List<long> orig, int mixes)
{
// Use a record with the index and value because searching for just the value is not good enough (the input
// can contain duplicates)
List<NodeVal> shuffled = orig.Select((val, i) => new NodeVal(i, val)).ToList();
NodeVal zeroVal = null;
for (int j = 0; j < mixes; j++)
{
for (int i = 0; i < orig.Count; i++)
{
NodeVal val = new(i, orig[i]);
if (orig[i] == 0)
{
zeroVal = val;
}
int oldPos = shuffled.FindIndex(x => x.Equals(val));
long newPos = oldPos + val.Val;
shuffled.RemoveAt(oldPos);
// Account for the final destination being out of range in either direction. The trick is to use the
// little mod formulas below to get into the target range. Repeated addition or subtraction works for
// part 1, but is too slow for the large numbers in part 2
if (newPos < 0)
{
newPos = shuffled.Count - (Math.Abs(newPos) % shuffled.Count);
}
else if (newPos >= shuffled.Count)
{
newPos %= shuffled.Count;
}
shuffled.Insert((int)newPos, val);
}
}
int finalIdx = shuffled.FindIndex(x => x.Equals(zeroVal));
long finalSum = 0;
for (int i = 1; i <= 3000; i++)
{
finalIdx++;
if (finalIdx == shuffled.Count)
{
finalIdx = 0;
}
if (i > 0 && i % 1000 == 0)
{
finalSum += shuffled[finalIdx].Val;
}
}
return finalSum;
}
private record NodeVal(int Idx, long Val);
}