day2
This commit is contained in:
35
day2/day2.jl
Normal file
35
day2/day2.jl
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env julia
|
||||||
|
|
||||||
|
infile = size(ARGS, 1) > 0 ? ARGS[1] : "day2input.txt"
|
||||||
|
println("infile = ", infile)
|
||||||
|
|
||||||
|
a_move_value = Dict("A"=>1, "B"=>2, "C"=>3)
|
||||||
|
x_move_value = Dict("X"=>1, "Y"=>2, "Z"=>3)
|
||||||
|
function score(opponent, self)
|
||||||
|
global a_move_value, x_move_value
|
||||||
|
oval = a_move_value[opponent]
|
||||||
|
sval = x_move_value[self]
|
||||||
|
|
||||||
|
diff = sval - oval
|
||||||
|
|
||||||
|
if diff == 0
|
||||||
|
# tie
|
||||||
|
return 3 + sval
|
||||||
|
elseif (diff == 1 || diff == -2)
|
||||||
|
# win
|
||||||
|
return 6 + sval
|
||||||
|
else
|
||||||
|
# loss
|
||||||
|
return sval
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
io = open(infile, "r")
|
||||||
|
total = 0
|
||||||
|
for line in eachline(io)
|
||||||
|
global total
|
||||||
|
o, s = split(line)
|
||||||
|
total += score(o, s)
|
||||||
|
end
|
||||||
|
|
||||||
|
println(total)
|
||||||
33
day2/day2b.jl
Normal file
33
day2/day2b.jl
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#!/usr/bin/env julia
|
||||||
|
|
||||||
|
infile = size(ARGS, 1) > 0 ? ARGS[1] : "day2input.txt"
|
||||||
|
println("infile = ", infile)
|
||||||
|
|
||||||
|
a_move_value = Dict("A"=>1, "B"=>2, "C"=>3)
|
||||||
|
function score(opponent, self)
|
||||||
|
oval = a_move_value[opponent]
|
||||||
|
|
||||||
|
if self == "X"
|
||||||
|
# need to lose
|
||||||
|
sval = 1 + mod(oval - 2, 3)
|
||||||
|
return sval
|
||||||
|
elseif self == "Y"
|
||||||
|
# need to tie
|
||||||
|
sval = oval
|
||||||
|
return 3 + sval
|
||||||
|
else
|
||||||
|
# need to win
|
||||||
|
sval = 1 + mod(oval, 3)
|
||||||
|
return 6 + sval
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
io = open(infile, "r")
|
||||||
|
total = 0
|
||||||
|
for line in eachline(io)
|
||||||
|
global total
|
||||||
|
o, s = split(line)
|
||||||
|
total += score(o, s)
|
||||||
|
end
|
||||||
|
|
||||||
|
println(total)
|
||||||
2500
day2/day2input.txt
Normal file
2500
day2/day2input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user