-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.cs
164 lines (129 loc) · 3.83 KB
/
05.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.IO;
namespace Day5 {
class Program {
static void Main(string[] args) {
p1();
p2();
}
static void p1() {
var reader = File.OpenText("input");
int sum = 0;
bool mapping = true;
Dictionary<int, List<int>> map = new Dictionary<int, List<int>>();
while (!reader.EndOfStream) {
var line = reader.ReadLine();
if (line == "") {
mapping = false;
} else {
if (mapping) {
string[] nums = line.Split("|");
int key = int.Parse(nums[0]);
int val = int.Parse(nums[1]);
if (!map.ContainsKey(key)) {
map[key] = new List<int>();
}
map[key].Add(val);
} else {
string[] words = line.Split(",");
List<int> nums = new List<int>();
foreach (string word in words) {
nums.Add(int.Parse(word));
}
if (valid(map, nums)) {
sum += nums[nums.Count / 2];
}
}
}
}
Console.WriteLine(sum);
}
static void p2() {
var reader = File.OpenText("input");
int sum = 0;
bool mapping = true;
Dictionary<int, List<int>> map = new Dictionary<int, List<int>>();
while (!reader.EndOfStream) {
var line = reader.ReadLine();
if (line == "") {
mapping = false;
} else {
if (mapping) {
string[] nums = line.Split("|");
int key = int.Parse(nums[0]);
int val = int.Parse(nums[1]);
if (!map.ContainsKey(key)) {
map[key] = new List<int>();
}
map[key].Add(val);
} else {
string[] words = line.Split(",");
List<int> nums = new List<int>();
foreach (string word in words) {
nums.Add(int.Parse(word));
}
if (!valid(map, nums)) {
nums = correct(map, nums);
sum += nums[nums.Count / 2];
}
}
}
}
Console.WriteLine(sum);
}
static bool valid(Dictionary<int, List<int>> map, List<int> pages) {
Dictionary<int, int> position = new Dictionary<int, int>();
for (int i = 0; i != pages.Count; ++i) {
position[pages[i]] = i;
}
foreach (var kvp in map) {
int key = kvp.Key;
foreach (int val in kvp.Value) {
if (position.ContainsKey(key) && position.ContainsKey(val)) {
if (position[key] > position[val]) {
return false;
}
}
}
}
return true;
}
static List<int> correct(Dictionary<int, List<int>> map, List<int> pages) {
Dictionary<int, List<int>> graph = new Dictionary<int, List<int>>();
Dictionary<int, int> inDegree = new Dictionary<int, int>();
foreach (var page in pages) {
graph[page] = new List<int>();
inDegree[page] = 0;
}
foreach (var kvp in map) {
int key = kvp.Key;
foreach (int val in kvp.Value) {
if (pages.Contains(key) && pages.Contains(val)) {
graph[key].Add(val);
inDegree[val]++;
}
}
}
List<int> queue = new List<int>();
foreach (var page in pages) {
if (inDegree[page] == 0) {
queue.Add(page);
}
}
List<int> sortedPages = new List<int>();
while (queue.Count > 0) {
int current = queue[0];
queue.RemoveAt(0);
sortedPages.Add(current);
foreach (var neighbor in graph[current]) {
inDegree[neighbor]--;
if (inDegree[neighbor] == 0) {
queue.Add(neighbor);
}
}
}
return sortedPages;
}
}
}