-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.dart
69 lines (60 loc) · 2.06 KB
/
day2.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
import 'dart:io';
enum Outcome { win, loose, draw }
final translationMap = {
"A": "R",
"X": "R",
"B": "P",
"Y": "P",
"C": "S",
"Z": "S"
};
final choiceScoreMap = {
"R": 1,
"P": 2,
"S": 3,
};
final outcomesScoreMap = {
Outcome.loose: 0,
Outcome.draw: 3,
Outcome.win: 6,
};
final rockMap = {"R": Outcome.draw, "P": Outcome.loose, "S": Outcome.win};
final paperMap = {"P": Outcome.draw, "R": Outcome.win, "S": Outcome.loose};
final scissorsMap = {"S": Outcome.draw, "R": Outcome.loose, "P": Outcome.win};
final winMap = {"R": rockMap, "P": paperMap, "S": scissorsMap};
final commandMap = {"X": Outcome.loose, "Y": Outcome.draw, "Z": Outcome.win};
final part2Map = {"R": rockMap2, "P": paperMap2, "S": scissorsMap2};
// if they have chosen rock, and you have command, then what do you need
final rockMap2 = {Outcome.draw: "R", Outcome.loose: "S", Outcome.win: "P"};
final paperMap2 = {Outcome.draw: "P", Outcome.loose: "R", Outcome.win: "S"};
final scissorsMap2 = {Outcome.draw: "S", Outcome.loose: "P", Outcome.win: "R"};
void day2() {
var lines = File("inputs/day2").readAsLinesSync();
// 0 lost, 3 draw, 6 win
var totalScorePart1 = 0;
var totalScorePart2 = 0;
for (var line in lines) {
var commands = line.split(" ");
var p1Choice = translationMap[commands[0]];
// part1
var p2Choice = translationMap[commands[1]];
var p2ChoiceScore = choiceScoreMap[p2Choice]!;
totalScorePart1 += p2ChoiceScore;
var p2Outcome = winMap[p2Choice]![p1Choice]!;
var p2OutcomeScore = outcomesScoreMap[p2Outcome]!;
totalScorePart1 += p2OutcomeScore;
// part2
// x loose, y draw, z win
var p2Command = commandMap[commands[1]]!;
// calculate outcome score
var p2OutcomeScore2 = outcomesScoreMap[p2Command]!;
totalScorePart2 += p2OutcomeScore2;
// calculate choice score
var p1ChoiceMap2 = part2Map[p1Choice]!;
var p2Choice2 = p1ChoiceMap2[p2Command]!;
var p2ChoiceScore2 = choiceScoreMap[p2Choice2]!;
totalScorePart2 += p2ChoiceScore2;
}
print("Part 1: $totalScorePart1");
print("Part 2: $totalScorePart2");
}