-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.cs
71 lines (57 loc) · 1.85 KB
/
Day16.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2016
{
class Day16 : Day
{
public dynamic Input
{
get
{
return new { start = "10010000000110000", length = 272 };
}
}
public string Part1(dynamic input)
{
StringBuilder databuilder = new StringBuilder();
databuilder.Append((string)input.start);
int targetlength = input.length;
while (databuilder.Length < targetlength)
{
var startingLength = databuilder.Length;
databuilder.Append("0");
for (int i = 0; i < startingLength; i++)
{
databuilder.Append(reverseChar(databuilder[startingLength - i - 1]));
}
}
var data = databuilder.ToString().Substring(0, targetlength);
var checksum = new StringBuilder();
while (checksum.Length % 2 == 0)
{
checksum.Clear();
for (int i = 0; i < data.Length / 2; i++)
{
checksum.Append(data[2 * i] == data[2 * i + 1] ? "1" : "0");
}
data = checksum.ToString();
}
return checksum.ToString();
}
private char reverseChar(char v)
{
return v == '1' ? '0' : '1';
}
public string Part2(dynamic input)
{
return Part1(new { start = "10010000000110000", length = 35651584 });
}
public void Test()
{
Utils.Test(Part1, new { start = "10000", length = 20 }, "01100");
}
}
}