-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.cs
76 lines (64 loc) · 1.66 KB
/
Day9.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
using System.Collections.Generic;
using System.IO;
class Day9 {
public static int Solution1() {
StreamReader input = File.OpenText("input9.txt");
Queue<int> previous = new Queue<int>(25);
Dictionary<int, int> sums = new Dictionary<int, int>();
for (int i = 0; i < 25; i++) {
int next = int.Parse(input.ReadLine());
foreach (int existing in previous) {
int sum = existing + next;
sums[sum] = sums.TryGetValue(sum, out int count) ? count + 1 : 1;
}
previous.Enqueue(next);
}
while (true) {
int next = int.Parse(input.ReadLine());
if (!sums.ContainsKey(next)) {
input.Close();
return next;
}
int first = previous.Dequeue();
foreach (int existing in previous) {
int sum = existing + next;
sums[sum] = sums.TryGetValue(sum, out int count) ? count + 1 : 1;
sum = first + existing;
count = sums[sum];
if (count > 1) {
sums[sum] = count - 1;
} else {
sums.Remove(sum);
}
}
previous.Enqueue(next);
}
}
public static int Solution2() {
const int target = 258585477;
Queue<int> window = new Queue<int>();
int sum = 0;
foreach (string line in File.ReadLines("input9.txt")) {
int n = int.Parse(line);
window.Enqueue(n);
sum += n;
while (sum > target) {
n = window.Dequeue();
sum -= n;
}
if (sum == target) {
int smallest = int.MaxValue;
int largest = int.MinValue;
foreach (int num in window) {
if (num > largest) {
largest = num;
} else if (num < smallest) {
smallest = num;
}
}
return smallest + largest;
}
}
return -1;
}
}