-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.cs
147 lines (122 loc) · 4.06 KB
/
Day8.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
using System.Globalization;
using System.Reflection.Metadata.Ecma335;
using System.Xml.Xsl;
namespace _2023_cs;
public class Day8
{
private struct nextNode
{
public nextNode(string name, int distance)
{
this.Name = name;
this.Distance = distance;
}
public string Name { get; set; }
public int Distance { get; set; }
}
private class Node
{
public Node(string name)
{
this.name = name;
travelled = 0;
}
public string name { get; set; }
public int travelled { get; set; }
}
public static void part2(Dictionary<string, Tuple<string, string>> map, string directions)
{
// vždy predpokladáme, že z *A sa prejde len do jedného *Z a to dokonca vždy v rovnakom stave
// smerových inštrukcií. Teda cesta z *A to *Z je rovnako dlhá ako cesta z toho *Z zase to doho istého *Z.
// preto vypočítam iba najmenší spoločný násobok dĺžky ciest a to by mal byť výsledok. Problém je že táto infomácia
// je tak trochu "vymyslená" a zo zadania priamo nevyplýva. Pre všeobecný prípad by bol výpočet značne náročnejší,
// keďže by bolo terba nájsť nejaký cyklus a zároveň kontrolovať všetky medzizastávky v tomto cykle.
List<string> startingNodes = new List<string>();
foreach (var node in map)
{
if (node.Key.Last() == 'Z')
{
startingNodes.Add(node.Key);
}
}
static int gcd(int a, int b)
{
if (a == 0)
{
return b;
}
if (b==0)
{
return a;
}
return gcd(b, a % b);
}
List<int> distances = new List<int>();
foreach (var startingNode in startingNodes)
{
string currentNOde = startingNode;
int i = 0;
bool first = true;
while (first || currentNOde[2] != 'Z')
{
first = false;
if (directions[i % directions.Length] == 'R')
{
currentNOde = map[currentNOde].Item2;
}
else
{
currentNOde = map[currentNOde].Item1;
}
i++;
}
distances.Add(i);
}
int divisor = distances[0];
for (int i = 1; i < distances.Count; i++)
{
divisor = gcd(divisor, distances[i]);
}
long multiple = divisor;
foreach (var distance in distances)
{
multiple *= distance / divisor;
}
Console.WriteLine("day 8 part 2");
Console.WriteLine(multiple);
}
public static void solve(string filename)
{
Dictionary<string, Tuple<string, string>> nodes = new Dictionary<string, Tuple<string, string>>();
string instructions;
StreamReader input = new StreamReader(filename);
string? line = input.ReadLine();
instructions = line;
line = input.ReadLine();
line = input.ReadLine();
while (line != null)
{
nodes.Add(line.Substring(0, 3),
new Tuple<string, string>(line.Substring(7, 3), line.Substring(12, 3)));
line = input.ReadLine();
}
input.Close();
part2(nodes, instructions);
string currentNOde = "AAA";
int i = 0;
while (currentNOde != "ZZZ")
{
if (instructions[i % instructions.Length] == 'R')
{
currentNOde = nodes[currentNOde].Item2;
}
else
{
currentNOde = nodes[currentNOde].Item1;
}
i++;
}
Console.WriteLine("day 8 part 1");
Console.WriteLine(i);
}
}