-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.jl
86 lines (74 loc) · 1.82 KB
/
day2.jl
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
@enum Shape begin
rock = 1
paper = 2
scissors = 3
end
@enum Outcome begin
lose = 1
draw = 2
win = 3
end
function to_shape(shape::Char)
if shape == 'A' || shape == 'X'
rock::Shape
elseif shape == 'B' || shape == 'Y'
paper::Shape
elseif shape == 'C' || shape == 'Z'
scissors::Shape
end
end
function to_outcome(token::Char)
if token == 'X'
lose::Outcome
elseif token == 'Y'
draw::Outcome
elseif token == 'Z'
win::Outcome
end
end
function get_shape(other_shape::Shape, outcome::Outcome)
if outcome == draw
other_shape
elseif outcome == win
if other_shape == rock
paper::Shape
elseif other_shape == paper
scissors::Shape
elseif other_shape == scissors
rock::Shape
end
elseif outcome == lose
if other_shape == rock
scissors::Shape
elseif other_shape == paper
rock::Shape
elseif other_shape == scissors
paper::Shape
end
end
end
function get_score(your_shape::Shape, other_shape::Shape)
local score = Int(your_shape)
if your_shape == other_shape
score += 3
elseif your_shape == rock && other_shape == scissors
score += 6
elseif your_shape == scissors && other_shape == paper
score += 6
elseif your_shape == paper && other_shape == rock
score += 6
end
score
end
rounds = readlines("day2.data.txt")
total_score = 0
for i in eachindex(rounds)
other_shape = to_shape(first(rounds[i]))
# Part 1
#your_shape = to_shape(last(rounds[i]))
# Part 2
outcome = to_outcome(last(rounds[i]))
your_shape = get_shape(other_shape, outcome)
global total_score += get_score(your_shape, other_shape)
end
println(total_score)