Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created September 28, 2017 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/225eb82fbf8fa12c7909c295f6518d78 to your computer and use it in GitHub Desktop.
Save whatalnk/225eb82fbf8fa12c7909c295f6518d78 to your computer and use it in GitHub Desktop.
CSAcademy Round #50
# Travel Distance
x = 0
y = 0
gets.chomp.split("").each do |d|
if d == "N"
y += 1
elsif d == "E"
x += 1
elsif d == "S"
y -= 1
else
x -= 1
end
end
puts x.abs + y.abs
# Friday 13
d = [31,28,31,30,31,30,31,31,30,31,30,31]
a = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
jan1 = a.index(gets.chomp)
doy = 0
ans = 0
d.each do |dom|
(1..dom).each do |i|
doy += 1
if (doy + jan1) % 7 == 5 && i == 13 then
ans += 1
end
end
end
puts ans
# Check Square
def dot(a, b, c)
v1 = [b[0] - a[0], b[1] - a[1]]
v2 = [c[0] - b[0], c[1] - b[1]]
return v1[0] * v2[0] + v1[1] * v2[1]
end
def dist(a, b)
return (b[0] - a[0])**2 + (b[1] - a[1])**2
end
def square?(perm)
a1 = dot(perm[0], perm[1], perm[2]) == 0
a2 = dot(perm[1], perm[2], perm[3]) == 0
a3 = dot(perm[2], perm[3], perm[0]) == 0
a4 = dot(perm[3], perm[0], perm[1]) == 0
return a1 && a2 && a3 && a4 && dist(perm[0], perm[1]) && dist(perm[1], perm[2]) && dist(perm[2], perm[3]) && dist(perm[3], perm[0])
end
gets.chomp.to_i.times do
done = false
ps = []
ls = []
4.times do
x, y = gets.chomp.split(" ").map(&:to_i)
ps << [x, y]
end
ps.permutation do |perm|
if square?(perm)
puts 1
done = true
break
end
end
puts 0 if !done
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment