-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.dart
128 lines (103 loc) · 2.83 KB
/
day02.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
import 'package:adventofcode2022/day.dart';
import 'package:adventofcode2022/helper/string_extensions.dart';
import 'package:collection/collection.dart';
class Day2 implements Day {
@override
int get dayNumber => 2;
@override
String solveFirstPuzzle(String puzzleInput) {
final games = puzzleInput.splitPerLine().map((e) => Game.firstGameVersion(e));
var totalScore = games.map((e) => e.getScore()).toList().sum;
return totalScore.toString();
}
@override
String solveSecondPuzzle(String puzzleInput) {
final games = puzzleInput.splitPerLine().map((e) => Game.secondGameVersion(e));
var totalScore = games.map((e) => e.getScore()).toList().sum;
return totalScore.toString();
}
}
enum Shape {
rock(1),
paper(2),
scissor(3);
const Shape(this.value);
final int value;
}
Map<String, Shape> shapeLookupTable = {
'A': Shape.rock,
'B': Shape.paper,
'C': Shape.scissor,
'X': Shape.rock,
'Y': Shape.paper,
'Z': Shape.scissor
};
class Game {
Game();
late Shape player;
late Shape opponent;
factory Game.firstGameVersion(String rawInput) {
return Game()
..opponent = shapeLookupTable[rawInput[0]]!
..player = shapeLookupTable[rawInput[2]]!;
}
factory Game.secondGameVersion(String rawInput) {
final currentGame = Game()..opponent = shapeLookupTable[rawInput[0]]!;
if (rawInput[2] == 'X') {
_playerShouldLose(currentGame);
}
if (rawInput[2] == 'Y') {
_gameShouldBeADraw(currentGame);
}
if (rawInput[2] == 'Z') {
_playerShouldWin(currentGame);
}
return currentGame;
}
bool playerHasWon() {
if ((player == Shape.paper && opponent == Shape.rock) ||
(player == Shape.rock && opponent == Shape.scissor) ||
(player == Shape.scissor && opponent == Shape.paper)) {
return true;
}
return false;
}
bool isDraw() {
return player == opponent;
}
int getScore() {
int score = 0;
score += player.value;
if (playerHasWon()) {
score += 6;
} else if (isDraw()) {
score += 3;
}
return score;
}
static void _playerShouldWin(Game currentGame) {
if (currentGame.opponent == Shape.paper) {
currentGame.player = Shape.scissor;
}
if (currentGame.opponent == Shape.rock) {
currentGame.player = Shape.paper;
}
if (currentGame.opponent == Shape.scissor) {
currentGame.player = Shape.rock;
}
}
static void _gameShouldBeADraw(Game currentGame) {
currentGame.player = currentGame.opponent;
}
static void _playerShouldLose(Game currentGame) {
if (currentGame.opponent == Shape.paper) {
currentGame.player = Shape.rock;
}
if (currentGame.opponent == Shape.rock) {
currentGame.player = Shape.scissor;
}
if (currentGame.opponent == Shape.scissor) {
currentGame.player = Shape.paper;
}
}
}