-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.ps1
116 lines (74 loc) · 1.56 KB
/
day2.ps1
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
# Advent of Code 2022
# Day 2 : Rock Paper Scissors
#
$groups = new-object "system.collections.arraylist"
$list = Get-Content .\input.txt
$map = @{
A ='X'
B = 'Y'
C = 'Z'
}
<#
Rock defeats Scissors
x : z
Scissors defeats Paper
z : y
Paper defeats Rock.
y : x
#>
$win = @{
x='z'
z='y'
y='x'
}
<# scoring
rock 1
paper 2
scissor 3
lose 0
draw 3
win 6
#>
$lookup = @{
X = 1
Y = 2
Z = 3
}
## Part 1
$total = 0
foreach ($play in $list) {
$score = 0
$round = $play -split " "
$them = $map.$($round[0])
$me = $round[1]
$score += $lookup.$me
if ($win.$me -eq $them ) {$score +=6 }
if ($win.$them -eq $me ) {$score +=0 }
if ($me -eq $them ) {$score += 3 }
$total += $score
"{0} : {1} : {2} : {3}"-f $them, $me, $score, $total
}
$total
## Part 2
$total = 0
foreach ($play in $list) {
$score = 0
$round = $play -split " "
$them = $map.$($round[0])
$outcome = $round[1]
<#
x: lose
y: draw
z: win
#>
if ($outcome -eq 'y') { $me = $them} # this is a draw
if ($outcome -eq 'x') { $me = $win.$them } # if they win, i've lost
if ($outcome -eq 'z') { $me = $win.GetEnumerator() | ? Value -eq $them | % key} # if they lose, i've won.
$score += $lookup.$me
if ($win.$me -eq $them ) {$score +=6 }
if ($win.$them -eq $me ) {$score +=0 }
if ($me -eq $them ) {$score += 3 }
$total += $score
#"{0} : {1} : {2} : {3}" -f $them, $me, $outcome.$me, $score, $total
}
"Total: {0}" -f $total