Skip to content

Instantly share code, notes, and snippets.

@nealian
Created March 6, 2015 08:48
Show Gist options
  • Save nealian/86228bc3db3c36f5ce3d to your computer and use it in GitHub Desktop.
Save nealian/86228bc3db3c36f5ce3d to your computer and use it in GitHub Desktop.
require 'socket'
s = TCPSocket.new("104.131.107.153", 12121)
p s.gets
second_line = false
last_line = []
point = 0
s.each_line do |line|
puts line
if line =~ /(-?[0-9]+) \+ (-?[0-9]+)$/
p $1, $2
p ($1.to_i + $2.to_i).to_s
s.write(($1.to_i + $2.to_i).to_s)
elsif line =~ /(-?[0-9]+)x \+ (-?[0-9]+)y = (-?[0-9]+)$/
p $1, $2, $3
if second_line
second_line = false
x = ((last_line[2].to_f/last_line[1].to_f) - ($3.to_f/$2.to_f)).fdiv((last_line[0].to_f/last_line[1].to_f) - ($1.to_f/$2.to_f)).round
p x.to_s
p ($3.to_i - ($1.to_i * x)).fdiv($2.to_i).round.to_s
s.write(x.to_s)
s.write(($3.to_i - ($1.to_i * x)).fdiv($2.to_i).round.to_s)
last_line = []
else
second_line = true
last_line[0] = $1
last_line[1] = $2
last_line[2] = $3
end
elsif line =~ /at point (-?[0-9]+):$/
p $1
point = $1.to_i
elsif line =~ /f\(x\) = (.*)$/
p $1
result = 0
$1.split(" + ").each do |expression|
p expression
if expression =~ /([0-9])x\^([0-9])$/
p $1, $2
p (($2.to_i * $1.to_i) * (point ** ($2.to_i - 1))).to_s
result += (($2.to_i * $1.to_i) * (point ** ($2.to_i - 1)))
elsif expression =~ /([0-9])x$/
p $1
p $1
result += $1.to_i
elsif expression =~ /([0-9])$/
p $1
p 0.to_i
result += 0
end
end
p result.to_s
s.write(result.to_s)
point = 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment