Part One

Time taken: 50 minutes Observed by: Eric Hayes

The result I ended up with:

import fs from 'fs'
 
fs.readFile("puzzleInput.txt", 'utf-8', (err, data) => {
  const calibrationValues = data.split('\n')
  console.log({calibrationValues})
  
  const foundValues: [string, string][] = []
  let finalNumber: number = 0
 
 
  /// First digit you find add as first digit AND as last digit
  // As you iterate, overwrite additional digits as the last digit
 
  let nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
  for (const lineIndex in calibrationValues){
    foundValues.push(['', ''])
    for (const char of calibrationValues[lineIndex]){
      if (nums.includes(char)){
        if (foundValues[lineIndex][0]){
          foundValues[lineIndex][1] = char // debrief < - we don't even need two
        } else {
          foundValues[lineIndex][0] = char
          foundValues[lineIndex][1] = char
        }
      }
    }
    let additionStep = parseInt(foundValues[lineIndex][0] + foundValues[lineIndex][1])
    if (additionStep) finalNumber += additionStep
  }
  console.log(finalNumber)
})
 
 
/*
 
Eric's observations of my performance:
Freaking out
deep breath
maybe some ritual or meditation
I am refactoring a lot - it's hard to avoid that tendency, but optimizations 
only matter sometimes and especially in an interview setting they don't matter
What matters is optimization at a time complexity level
Another thing I am doing: I'm thinking of multiple problems at once and jumping
around a lot
Writing functions to compartmentalize thinking might be good
Thinking about two things at once and how they relate may be a problem
"Here I'm parsing a number", "Here I'm adding to a sum"
It ultimately hurts me
 
bun is async out of both, could have done await out of the box
List of numbers is hurting my runtime
 
for line 9, I could try to convert into number,
OR I could do same thing with set for faster lookups
 
Get much better at
- File reading like it's my business
- String parsing
 
*/

I felt very convinced throughout much of this challenge that there must be much more succinct ways to write this, and tantalized by that possibility because my own code, through my panic, was sort of difficult for me to read.

I’m not sure why I panicked, but at a certain point I did and it was sort of hard to put that cat back in the box.

The results of panicking

  • Jumping around a lot
  • Not always communicating what I was trying to do
  • Being less willing to take a moment write out pseudocode or consider alternative solutions

I did this once during an interview with Dealops as well.

Attempt 2

Turns out there were a tone of things I did that made things harder on myself.

  1. This insistence on returning back to this continually updated array of values made my process more difficult to reason about. It was sort of like, “data hoarding” - I wanted to do ETL and transform data in as many discrete steps as possible. But this is a great example of how the exact opposite is much simpler, because data structures and loops are
    1. Generally verbose
    2. specifically some of the hardest bits of syntax to reason about
    3. especially if there are similar ones lying around
  2. Some very simple regex, namely someValues.match(/\d/g) really helped simplify things.
import fs from 'fs'
 
fs.readFile("testInput.txt", 'utf-8', (err, data) => {
  const calibrationValues = data.split('\n')
  
  let finalNumber: number = 0
 
  for (const lineValue of calibrationValues){
    const foundDigits = lineValue.match(/\d/g) ?? []
    if (foundDigits.length <= 0) continue
    const n = foundDigits.length
    let foundTuple = foundDigits[0]!
    foundTuple += foundDigits[n-1]
    const num = parseInt(foundTuple) ?? 0
    finalNumber += num
  }
  console.log("final", finalNumber)
})

I’ll be honest though, I still struggled a bit. Mostly what I struggled with was

  1. Handling when foundDigits.length didn’t exist
  2. I was trying to conditionally use it in some cases and others, until I introduced if (foundDigits.length <= 0) continue, which simplified everything into one convenient conditional

But that still leaves me feeling that I should probably get to the bottom of someArray.length.

'moose'.length
// 3
let monkeys = []
monkeys.length
// 0

Hmm. Well these things are pretty straightforward. What about this:

''.length
// 0

Also zero. Man, what was I struggling with here? This is all very straightforward?