-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day16.cs
97 lines (85 loc) · 3.5 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
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
using System;
using System.Collections.Generic;
using System.IO;
class Day16 {
public static int Solution1() {
string[] input = File.ReadAllText("input16.txt").Split(new[] { "\n\n" }, StringSplitOptions.None);
bool[] rules = new bool[1000];
foreach (string[] ranges in Array.ConvertAll(input[0].Split('\n'), rule => rule.Split(new[] { ": " }, StringSplitOptions.None)[1].Split(new[] { " or " }, StringSplitOptions.None))) {
foreach (string[] range in Array.ConvertAll(ranges, range => range.Split('-'))) {
int start = int.Parse(range[0]);
int end = int.Parse(range[1]);
for (int i = start; i <= end; i++) {
rules[i] = true;
}
}
}
int sum = 0;
foreach (int rule in Array.ConvertAll(input[2].Split(new[] { '\n' }, 2)[1].Split(new[] { '\n', ',' }, StringSplitOptions.RemoveEmptyEntries), int.Parse)) {
if (!rules[rule]) {
sum += rule;
}
}
return sum;
}
public static int Solution2() {
string[] input = File.ReadAllText("input16.txt").Split(new[] { "\n\n" }, StringSplitOptions.None);
Dictionary<string, bool[]> rules = new Dictionary<string, bool[]>();
foreach (string[] rule in Array.ConvertAll(input[0].Split('\n'), rule => rule.Split(new[] { ": " }, StringSplitOptions.None))) {
bool[] validValues = new bool[1000];
rules[rule[0]] = validValues;
foreach (string[] range in Array.ConvertAll(rule[1].Split(new[] { " or " }, StringSplitOptions.None), range => range.Split('-'))) {
int start = int.Parse(range[0]);
int end = int.Parse(range[1]);
for (int i = start; i <= end; i++) {
validValues[i] = true;
}
}
}
int[] myTicket = Array.ConvertAll(input[1].Split('\n')[1].Split(','), int.Parse);
List<int[]> validTickets = new List<int[]> { myTicket };
foreach (int[] ticket in Array.ConvertAll(input[2].Split(new[] { '\n' }, 2)[1].Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries), line => Array.ConvertAll(line.Split(','), int.Parse))) {
if (Array.TrueForAll(ticket, value => {
foreach (bool[] validValues in rules.Values) {
if (validValues[value]) {
return true;
}
}
return false;
})) {
validTickets.Add(ticket);
}
}
HashSet<string>[] possibilities = new HashSet<string>[validTickets[0].Length];
for (int i = 0; i < possibilities.Length; i++) {
possibilities[i] = new HashSet<string>(rules.Keys);
}
foreach (int[] ticket in validTickets) {
for (int i = 0; i < ticket.Length; i++) {
foreach (KeyValuePair<string, bool[]> rule in rules) {
if (!rule.Value[ticket[i]]) {
possibilities[i].Remove(rule.Key);
}
}
}
}
Dictionary<string, int> fieldNames = new Dictionary<string, int>(20);
int[] order = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
Array.Sort(possibilities, order, Comparer<HashSet<string>>.Create((a, b) => a.Count - b.Count));
for (int i = 0; i < possibilities.Length; i++) {
HashSet<string>.Enumerator enumerator = possibilities[i].GetEnumerator();
enumerator.MoveNext();
string name = enumerator.Current;
fieldNames[name] = order[i];
for (int j = i + 1; j < possibilities.Length; j++) {
possibilities[j].Remove(name);
}
}
long product = 1;
foreach (string field in new[] { "departure location", "departure station", "departure platform", "departure track", "departure date", "departure time" }) {
product *= myTicket[fieldNames[field]];
}
Console.WriteLine(product);
return 0;
}
}