-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.cs
126 lines (97 loc) · 3.29 KB
/
Day14.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace AdventOfCode2018
{
class Day14 : Day
{
public override bool Test()
{
return Utils.Test(Part1, "9", "5158916779") &&
Utils.Test(Part2, "51589", "9") &&
Utils.Test(Part2, "01245", "5") &&
Utils.Test(Part2, "92510", "18") &&
Utils.Test(Part2, "59414", "2018");
}
public override string Input => "323081";
public override string Part1(string input, dynamic options)
{
var rounds = int.Parse(input);
var e1 = 0;
var e2 = 1;
List<int> recipes = new List<int> { 3, 7 };
while (recipes.Count < rounds + 10)
{
var sum = recipes[e1] + recipes[e2];
if (sum >= 10)
{
recipes.Add(1);
}
recipes.Add(sum % 10);
e1 = (e1 + 1 + recipes[e1]) % recipes.Count;
e2 = (e2 + 1 + recipes[e2]) % recipes.Count;
//print(recipes);
}
var result = "";
for (int i = 0; i < 10; i++)
{
result += recipes[recipes.Count - 10 + i];
}
return result;
}
public void print(List<int> recipes)
{
Console.WriteLine();
foreach (var i in recipes)
{
Console.Write(i + " ");
}
}
public override string Part2(string input, dynamic options)
{
var e1 = 0;
var e2 = 1;
List<int> recipes = new List<int> { 3, 7 };
int total = 0;
int target = int.Parse(input);
int maxdigits = (int)Math.Pow(10, input.Length);
for (int r = 0; ; r++)
{
var sum = recipes[e1] + recipes[e2];
if (sum >= 10)
{
recipes.Add(1);
total = 10 * total + 1;
}
total = total % maxdigits;
if (total == target)
{
return (recipes.Count - input.Length).ToString();
}
recipes.Add(sum % 10);
total = 10 * total + (sum % 10);
e1 = (e1 + 1 + recipes[e1]) % recipes.Count;
e2 = (e2 + 1 + recipes[e2]) % recipes.Count;
total = total % maxdigits;
if (total == target)
{
return (recipes.Count - input.Length).ToString();
}
// 323081
if (recipes[recipes.Count - 1] == 1 && recipes[recipes.Count - 2] == 8 && recipes[recipes.Count - 3] == 0 && recipes[recipes.Count - 4] == 3)
{
var x = 4;
}
if (r % 10000 == 0)
{
var x = 2;
}
}
}
}
}