DEV Community

Discussion on: Daily Challenge #136 - The Deaf Rats of Hamelin

Collapse
 
sabbin profile image
Sabin Pandelovitch

Using javascript

const examples = [
  "~O~O~O~O P",
  "P O~ O~ ~O O~",
  "~O~O~O~OP~O~OO~",
  "O~ O~~OPO~~O O~"
];

const putSpaces = item => item.replace(/(.{2})/g, "$1 ");
const getDeafRats = example => {
  const [leftPad, rightPad] = example.replace(/\s+/g, "").split("P");

  return (
    (putSpaces(leftPad).match(/O~/g) || []).length +
    (putSpaces(rightPad).match(/~O/g) || []).length
  );
};

const totalDeafRats = examples.reduce(
  (total, result) => total + getDeafRats(result),
  0
);
console.log("Total", totalDeafRats);
console.log("For each example", examples.map(getDeafRats));

CodeSandbox example