-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.cs
44 lines (35 loc) · 978 Bytes
/
Day1.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
using System;
using System.Collections.Generic;
using System.IO;
class Day1 {
public static int Solution1() {
HashSet<int> solutions = new HashSet<int>();
foreach (int number in Array.ConvertAll(File.ReadAllLines("input1.txt"), int.Parse)) {
int other = 2020 - number;
if (solutions.Contains(number)) {
return other * number;
}
solutions.Add(other);
}
return -1;
}
public static int Solution2() {
Dictionary<int, int> solutions = new Dictionary<int, int>();
int[] numbers = Array.ConvertAll(File.ReadAllLines("input1.txt"), int.Parse);
foreach (int number1 in numbers) {
if (solutions.TryGetValue(number1, out int product)) {
return product * number1;
}
foreach (int number2 in numbers) {
if (number1 == number2) {
continue;
}
int sum = number1 + number2;
if (sum < 2020) {
solutions[2020 - sum] = number1 * number2;
}
}
}
return -1;
}
}