// If blank strings/ints is not enough
export const inputMapper = (inputs: string) =>
  inputs.split(",").map((input) =>
    input.split("-").map((input) => parseInt(input))
  ) as RangePair;

type Range = [from: number, to: number];
type RangePair = [first: Range, second: Range];

function isWithin(r1: Range, r2: Range): boolean {
  return r1[0] >= r2[0] && r1[1] <= r2[1];
}

export function solution1(inputs: RangePair[]): number {
  return inputs.filter((pair) =>
    isWithin(pair[0], pair[1]) || isWithin(pair[1], pair[0])
  ).length;
}

function intersects(r1: Range, r2: Range): boolean {
  return (r1[0] >= r2[0] && r1[0] <= r2[1]) ||
    (r1[1] >= r2[0] && r1[1] <= r2[1]);
}

export function solution2(inputs: RangePair[]): number {
  return inputs.filter((pair) =>
    intersects(pair[0], pair[1]) || intersects(pair[1], pair[0])
  ).length;
}
