-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.cs
122 lines (95 loc) · 2.84 KB
/
Day2.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace AOC23
{
public class Game
{
public int id = -1;
public List<int[]> draws = new();
public Game(string s)
{
this.id = int.Parse(s.Split(":")[0].Split(" ")[1]);
var draw_str = s.Split(":")[1].Split(";");
var rx_red = new Regex(@"(\d+) red");
var rx_green = new Regex(@"(\d+) green");
var rx_blue = new Regex(@"(\d+) blue");
foreach (var draw in draw_str)
{
var rrm = rx_red.Match(draw);
var grm = rx_green.Match(draw);
var brm = rx_blue.Match(draw);
var cubes = new int[3] { 0, 0, 0 };
if (rrm.Success)
cubes[0] = int.Parse(rrm.Groups[1].Value);
if (grm.Success)
cubes[1] = int.Parse(grm.Groups[1].Value);
if (brm.Success)
cubes[2] = int.Parse(brm.Groups[1].Value);
this.draws.Add(cubes);
}
}
public bool isValid(int[] limits)
{
foreach (var draw in this.draws)
{
for (int i = 0; i < 3; i++)
{
if (draw[i] > limits[i])
return false;
}
}
return true;
}
public int Power()
{
int[] max_cubes = new int[3] { -1, -1, -1 };
foreach (var draw in this.draws)
{
for (int i=0; i<3; i++)
{
if (draw[i] > max_cubes[i])
{
max_cubes[i] = draw[i];
}
}
}
return max_cubes[0] * max_cubes[1] * max_cubes[2];
}
}
public class Day2 : Day
{
public Day2(bool test) : base(2, test)
{
}
public override void Part1()
{
Console.Write("\tPart 1: ");
var limits = new int[3] { 12, 13, 14 };
var lines = this.Load();
int sum = 0;
foreach (var line in lines)
{
var g = new Game(line);
if (g.isValid(limits))
{
sum += g.id;
}
}
Console.WriteLine(sum);
}
public override void Part2()
{
Console.Write("\tPart 2: ");
var lines = this.Load();
var limits = new int[3] { 12, 13, 14 };
int sum = 0;
foreach (var line in lines)
{
var g = new Game(line);
sum += g.Power();
}
Console.WriteLine(sum);
}
}
}