import { CharColl, Coll, Collection } from "util/collection";

// If blank strings/ints is not enough
export const inputMapper = (inputs: string) => inputs;

export function solution1(inputs: Collection<string>): number {
  return inputs
    .map((input) => {
      const nums = CharColl(input)
        .map((c) => parseInt(c))
        .filter((i) => !isNaN(i));

      return nums.first * 10 + nums.last;
    })
    .x.sum();
}

export function cleanString(str: string): string {
  const result: Collection<string> = Coll();
  for (const [chars, index] of CharColl(str).slideWrap(5)) {
    if (index === str.length) {
      break;
    }

    if (!isNaN(parseInt(chars.first, 10))) {
      result.push(chars.first);
      continue;
    }

    const word = chars.join("");

    switch (true) {
      case word.startsWith("one"):
        result.push("1");
        break;
      case word.startsWith("two"):
        result.push("2");
        break;
      case word.startsWith("three"):
        result.push("3");
        break;
      case word.startsWith("four"):
        result.push("4");
        break;
      case word.startsWith("five"):
        result.push("5");
        break;
      case word.startsWith("six"):
        result.push("6");
        break;
      case word.startsWith("seven"):
        result.push("7");
        break;
      case word.startsWith("eight"):
        result.push("8");
        break;
      case word.startsWith("nine"):
        result.push("9");
        break;
    }
  }

  return result.join("");
}

export function solution2(inputs: Collection<string>): number {
  return solution1(inputs.map(cleanString));
}
