Back Original

Day 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.