-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day20.cs
130 lines (115 loc) · 3.66 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
using AdventOfCode.CSharp.Common;
using System;
namespace AdventOfCode.CSharp.Y2015.Solvers;
public class Day20 : ISolver
{
// first 10 primes, add more primes if needed?
private static readonly int[] s_primes =
[
2, 3, 5, 7, 11, 13, 17, 19, 23, 29
];
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int target = new SpanReader(input).ReadPosIntUntil('\n');
solution.SubmitPart1(SolvePart1(target));
solution.SubmitPart2(SolvePart2(target));
}
private static int SolvePart1(int target)
{
// divide the target by the present multiplier and round up if not an integer.
const int presentMultiplier = 10;
target = target / presentMultiplier + (target % presentMultiplier == 0 ? 0 : 1);
// essentially we are iterating through different combinations of prime factorisations
//
int best = int.MaxValue;
int[] counts = new int[s_primes.Length];
int[] totals = new int[s_primes.Length];
int[] muls = new int[s_primes.Length];
for (int i = 0; i < s_primes.Length; i++)
{
totals[i] = 1;
muls[i] = 1;
}
int cur = 0;
int prod = 1;
int mulProd = 1;
while (cur < s_primes.Length)
{
if (prod < target && mulProd < best)
{
int prime = s_primes[cur];
mulProd *= prime;
counts[cur]++;
muls[cur] *= prime;
prod /= totals[cur];
totals[cur] += muls[cur];
prod *= totals[cur];
cur = 0;
}
else
{
if (prod >= target)
{
best = Math.Min(best, mulProd);
}
counts[cur] = 0;
mulProd /= muls[cur];
muls[cur] = 1;
prod /= totals[cur];
totals[cur] = 1;
cur++;
}
}
return best;
}
private static int SolvePart2(int target)
{
// divide the target by the present multiplier and round up if not an integer.
const int presentMultiplier = 11;
target = target / presentMultiplier + (target % presentMultiplier == 0 ? 0 : 1);
// the batch size is a highly composite number
const int batchSize = 15120;
int[] initPresents = new int[batchSize];
int[] countPresents = new int[batchSize];
for (int i = 1; i <= 50; i++)
{
if (batchSize % i != 0)
{
continue;
}
for (int j = 0; j < batchSize; j += i)
{
initPresents[j] += j / i;
countPresents[j] += batchSize / i;
}
}
int[] presents = new int[batchSize];
int start = 0;
int batchNumber = 0;
while (true)
{
Array.Copy(initPresents, presents, batchSize);
for (int i = 2; i <= 50; i++)
{
if (batchSize % i == 0)
{
continue;
}
int jStart = ((-start % i) + i) % i;
for (int j = jStart; j < batchSize; j += i)
{
presents[j] += (j + start) / i;
}
}
for (int i = 0; i < batchSize; i++)
{
if (presents[i] + countPresents[i] * batchNumber >= target)
{
return start + i;
}
}
start += batchSize;
batchNumber++;
}
}
}