DEV Community

loading...

Discussion on: Daily Challenge #166 - Cat and Mouse

Collapse
citizen428 profile image
Michael Kohl • Edited

The quick & dirty Ruby version:

def cat_and_mouse(s, distance)
  c, m, d = %w[C m D].map { |c| s.index(c) }
  return 'Boring without all three' unless c && m && d
  return 'Escaped' if (c - m).abs > distance + 1
  return 'Protected' if d && (d > c && d < m || d > m && d < c)
  'Caught'
end
Forem Open with the Forem app