|
1 | 1 | using System.Text.Json; |
2 | 2 | using System.Text.Json.Serialization; |
3 | | -using System.Text.RegularExpressions; |
4 | 3 |
|
5 | 4 | namespace AdventOfCode.Services.Models; |
6 | 5 |
|
7 | 6 | struct Config |
8 | 7 | { |
9 | | - string _c; |
10 | | - int _y; |
11 | | - int[] _d; |
| 8 | + public string Cookie { get; set; } |
12 | 9 |
|
13 | | - public string Cookie |
14 | | - { |
15 | | - get => _c; |
16 | | - set |
17 | | - { |
18 | | - if (Regex.IsMatch(value, "^[a-z0-9]+$")) _c = value; |
19 | | - } |
20 | | - } |
21 | | - public int Year |
22 | | - { |
23 | | - get => _y; |
24 | | - set |
25 | | - { |
26 | | - if (value >= 2015 && value <= DateTime.Now.Year) _y = value; |
27 | | - } |
28 | | - } |
| 10 | + public int Year { get; set; } |
| 11 | + |
29 | 12 | [JsonConverter(typeof(DaysConverter))] |
30 | | - public int[] Days |
31 | | - { |
32 | | - get => _d; |
33 | | - set |
34 | | - { |
35 | | - bool allDaysCovered = false; |
36 | | - _d = value.Where(v => |
37 | | - { |
38 | | - if (v == 0) allDaysCovered = true; |
39 | | - return v > 0 && v < 26; |
40 | | - }).ToArray(); |
41 | | - |
42 | | - if (allDaysCovered) |
43 | | - { |
44 | | - _d = new int[] { 0 }; |
45 | | - } |
46 | | - else |
47 | | - { |
48 | | - Array.Sort(_d); |
49 | | - } |
50 | | - } |
51 | | - } |
| 13 | + public int[] Days { get; set; } |
52 | 14 |
|
53 | 15 | public void setDefaults() |
54 | 16 | { |
55 | 17 | //Make sure we're looking at EST, or it might break for most of the US |
56 | | - DateTime CURRENT_EST = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Utc).AddHours(-5); |
| 18 | + var currentEst = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Utc).AddHours(-5); |
57 | 19 | if (Cookie == default(string)) Cookie = ""; |
58 | | - if (Year == default(int)) Year = CURRENT_EST.Year; |
59 | | - if (Days == default(int[])) Days = (CURRENT_EST.Month == 12 && CURRENT_EST.Day <= 25) ? new int[] { CURRENT_EST.Day } : new int[] { 0 }; |
| 20 | + if (Year == default(int)) Year = currentEst.Year; |
| 21 | + if (Days == default(int[])) Days = (currentEst.Month == 12 && currentEst.Day <= 25) ? new int[] { currentEst.Day } : new int[] { 0 }; |
60 | 22 | } |
61 | 23 | } |
62 | 24 |
|
@@ -85,7 +47,10 @@ public override int[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe |
85 | 47 | break; |
86 | 48 | } |
87 | 49 |
|
88 | | - return tokens.SelectMany<string, int>(ParseString).ToArray(); |
| 50 | + var days = tokens.SelectMany<string, int>(ParseString); |
| 51 | + if (days.Contains(0)) return new[] { 0 }; |
| 52 | + |
| 53 | + return days.Where(v => v < 26 && v > 0).OrderBy(day => day).ToArray(); |
89 | 54 | } |
90 | 55 |
|
91 | 56 | private IEnumerable<int> ParseString(string str) |
|
0 commit comments