Skip to content

Commit 44928ae

Browse files
committed
chore(config): deconvolute config model a bit
1 parent 27283b8 commit 44928ae

File tree

2 files changed

+12
-47
lines changed

2 files changed

+12
-47
lines changed

AdventOfCode.Services/Models/Config.cs

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,24 @@
11
using System.Text.Json;
22
using System.Text.Json.Serialization;
3-
using System.Text.RegularExpressions;
43

54
namespace AdventOfCode.Services.Models;
65

76
struct Config
87
{
9-
string _c;
10-
int _y;
11-
int[] _d;
8+
public string Cookie { get; set; }
129

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+
2912
[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; }
5214

5315
public void setDefaults()
5416
{
5517
//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);
5719
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 };
6022
}
6123
}
6224

@@ -85,7 +47,10 @@ public override int[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe
8547
break;
8648
}
8749

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();
8954
}
9055

9156
private IEnumerable<int> ParseString(string str)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ If any solution files that you need are not already included, see **[Generating
3131
Create a new file named `config.json` at the root of the project.
3232
```json
3333
{
34-
"cookie": "session=c0nt3nt",
34+
"cookie": "c0nt3nt",
3535
"year": 2020,
3636
"days": [0]
3737
}

0 commit comments

Comments
 (0)