-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day16.cs
71 lines (60 loc) · 2.1 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
using AdventOfCode.CSharp.Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace AdventOfCode.CSharp.Y2015.Solvers
{
public class Day16 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
var items = new Dictionary<string, int>
{
["children"] = 3,
["cats"] = 7,
["samoyeds"] = 2,
["pomeranians"] = 3,
["akitas"] = 0,
["vizslas"] = 0,
["goldfish"] = 5,
["trees"] = 3,
["cars"] = 2,
["perfumes"] = 1
};
int sue1 = 0;
int sue2 = 0;
int sue = 1;
foreach (Range lineRange in input.SplitLines())
{
var reader = new SpanReader(input[lineRange]);
reader.SkipUntil(':');
bool canBePart1 = true;
bool canBePart2 = true;
for (int i = 0; i < 3; i++)
{
reader.SkipLength(1);
string itemName = Encoding.ASCII.GetString(reader.ReadUntil(':'));
reader.SkipLength(1);
int count = reader.ReadPosIntUntil(',');
int expectedCount = items[itemName];
bool isValidPart1 = count == expectedCount;
bool isValidPart2 = itemName switch
{
"cats" or "trees" => count > expectedCount,
"pomeranians" or "goldfish" => count < expectedCount,
_ => count == expectedCount,
};
canBePart1 = canBePart1 && isValidPart1;
canBePart2 = canBePart2 && isValidPart2;
}
if (canBePart1)
sue1 = sue;
if (canBePart2)
sue2 = sue;
sue++;
}
solution.SubmitPart1(sue1);
solution.SubmitPart2(sue2);
}
}
}