All files / day1 puzzle2.js

92.85% Statements 39/42
100% Branches 4/4
80% Functions 4/5
92.85% Lines 39/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991x 1x   1x   1x                               1x                   1x 1014x       1014x 1014x       1014x 2113x 1219x 55x           2113x       1014x 9126x     1014x     1x 3x 3x     1x 1x 1x 1x 1x 1x 7x 7x   1x     1x 1x     1x 1x 1x 1x 1000x 1000x   1x     1x            
const fs = require('fs')
const path = require('path')
 
const { extractCalibrationValue } = require('./puzzle1')
 
const LETTERS_TO_DIGITS = {
  one: 1,
  two: 2,
  three: 3,
  four: 4,
  five: 5,
  six: 6,
  seven: 7,
  eight: 8,
  nine: 9,
}
 
/*************************************************************
 * This doesn't handle overlapping words like eightwo ... T_T
 * I leave it here for posterity
 *************************************************************/
const transformLettersToDigitsv1 = (inputString) => {
  for (const index of Object.entries(LETTERS_TO_DIGITS)) {
    inputString = inputString.replace(
      new RegExp(index[0], 'g'), // Replace globally !
      index[1]
    )
  }
  return inputString
}
 
const transformLettersToDigits = (inputString) => {
  const regexPattern = new RegExp(
    '(?=(one|two|three|four|five|six|seven|eight|nine))',
    'g'
  )
  let matches = [...inputString.matchAll(regexPattern)]
  let i = 0
 
  // Trick to handle overlapping cases
  // We replace oneight with oneeight for example ^^'
  for (const item of matches) {
    if (matches[i + 1]) {
      if (item[1].length + item.index > matches[i + 1].index) {
        inputString = inputString.replaceAll(
          matches[i + 1][1],
          item[1].slice(-1) + matches[i + 1][1]
        )
      }
    }
    i = i + 1
  }
 
  // Actually replace letters with digits
  for (const digit of Object.entries(LETTERS_TO_DIGITS)) {
    inputString = inputString.replaceAll(digit[0], digit[1])
  }
 
  return inputString
}
 
const resolveLine = (line) => {
  const sanitizedLine = transformLettersToDigits(line)
  return extractCalibrationValue(sanitizedLine)
}
 
const resolveCasesEnonce = () => {
  let content = fs.readFileSync(process.cwd() + '/day1/enonce.txt').toString()
  const puzzle = content.split('\n')
  puzzle.pop() // last blank line
  let sum = 0
  for (const line of puzzle) {
    const sanitizedLine = transformLettersToDigits(line)
    sum = sum + extractCalibrationValue(sanitizedLine)
  }
  return sum
}
 
const resolvePuzzle2 = () => {
  let content = fs
    .readFileSync(process.cwd() + '/day1/puzzle_input.txt')
    .toString()
  const puzzle = content.split('\n')
  puzzle.pop() // blank line
  let sum = 0
  for (const line of puzzle) {
    const sanitizedLine = transformLettersToDigits(line)
    sum = sum + extractCalibrationValue(sanitizedLine)
  }
  return sum
}
 
module.exports = {
  transformLettersToDigits,
  resolvePuzzle2,
  resolveCasesEnonce,
  resolveLine,
}