-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5p1.cs
50 lines (44 loc) · 1.37 KB
/
5p1.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
StreamReader reader = new("5.txt");
Dictionary<int, HashSet<int>> rules = new();
int score = 0;
string line;
while ((line = reader.ReadLine()) != "") // {page number}|{page number}
{
MatchCollection matchedPageNumbers = Regex.Matches(line, @"^(\d+)|(\d+)$");
int p1 = int.Parse(matchedPageNumbers[0].Value);
int p2 = int.Parse(matchedPageNumbers[1].Value);
if (!rules.TryGetValue(p1, out var set))
{
set = new HashSet<int>();
rules[p1] = set;
}
set.Add(p2);
}
while ((line = reader.ReadLine()) != null) // {page number},{page number},...
{
List<int> previousPageNumbers = new();
bool rulesUpheld = true;
foreach (Match matchedPageNumber in Regex.Matches(line, @"\d+"))
{
int p = int.Parse(matchedPageNumber.Value);
if (rules.TryGetValue(p, out var set))
{
HashSet<int> violations = new HashSet<int>(previousPageNumbers);
violations.IntersectWith(set);
if (violations.Count != 0)
{
rulesUpheld = false;
break;
}
}
previousPageNumbers.Add(p);
}
if (rulesUpheld) score += previousPageNumbers[previousPageNumbers.Count / 2];
}
Console.WriteLine(score);
reader.Close();