-
Notifications
You must be signed in to change notification settings - Fork 1
/
day02_cubes.dart
154 lines (124 loc) · 3.59 KB
/
day02_cubes.dart
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import 'dart:core';
import 'dart:convert';
import 'dart:io';
const String sampleData =
"""Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
""";
void main() {
String puzzleInput = File('2023/data/day02_input.txt').readAsStringSync();
// part 1
CubeGame sampleGame = CubeGame(sampleData);
assert(sampleGame.part1() == 8);
final stopwatchPart1 = Stopwatch()..start();
CubeGame game = CubeGame(puzzleInput);
print("part 1: ${game.part1()}");
stopwatchPart1.stop();
print("Elapsed time: ${stopwatchPart1.elapsed}");
// part 2
assert(sampleGame.part2() == 2286);
final stopwatchPart2 = Stopwatch()..start();
print("part 2: ${game.part2()}");
stopwatchPart2.stop();
print("Elapsed time: ${stopwatchPart2.elapsed}");
}
class CubeGame {
late List<GameLog> gameResults;
CubeGame(String gameInput) {
LineSplitter ls = new LineSplitter();
List<String> lines = ls.convert(gameInput);
this.gameResults = [];
lines.forEach((line) {
GameLog game = GameLog(line);
this.gameResults.add(game);
});
}
int part1() {
const int maxRed = 12;
const int maxGreen = 13;
const int maxBlue = 14;
List<GameLog> possibleGames = [];
this.gameResults.forEach((game) {
bool isPossible = true;
game.pulls.forEach((pull) {
if ((pull.numRed > maxRed) |
(pull.numGreen > maxGreen) |
(pull.numBlue > maxBlue)) {
isPossible = false;
}
});
if (isPossible) {
possibleGames.add(game);
}
});
int possibleSum = 0;
possibleGames.forEach((element) {
possibleSum += element.id;
});
return possibleSum;
}
int part2() {
int cubePowerSum = 0;
this.gameResults.forEach((game) {
int maxRed = 0;
int maxGreen = 0;
int maxBlue = 0;
game.pulls.forEach((pull) {
if (pull.numRed > maxRed) {
maxRed = pull.numRed;
}
if (pull.numBlue > maxBlue) {
maxBlue = pull.numBlue;
}
if (pull.numGreen > maxGreen) {
maxGreen = pull.numGreen;
}
});
int cubePower = maxBlue * maxGreen * maxRed;
cubePowerSum += cubePower;
});
return cubePowerSum;
}
}
class GameLog {
late int id;
late List<CubesPulled> pulls;
GameLog(String line) {
List<String> parts = line.split(": ");
List<String> gameParts = parts[0].split(" ");
this.id = int.parse(gameParts[1]);
List<String> cubePulls = parts[1].split("; ");
this.pulls = [];
cubePulls.forEach((cubePull) {
List<String> cubes = cubePull.split(", ");
int numRed = 0;
int numGreen = 0;
int numBlue = 0;
cubes.forEach((element) {
if (element.endsWith("green")) {
List<String> result = element.split(" ");
numGreen = int.parse(result[0]);
}
if (element.endsWith("blue")) {
List<String> result = element.split(" ");
numBlue = int.parse(result[0]);
}
if (element.endsWith("red")) {
List<String> result = element.split(" ");
numRed = int.parse(result[0]);
}
});
CubesPulled item = CubesPulled(numRed, numGreen, numBlue);
this.pulls.add(item);
});
}
}
class CubesPulled {
int numRed;
int numGreen;
int numBlue;
CubesPulled(this.numRed, this.numGreen, this.numBlue);
}