Notes de Jérôme Prudent
Notes publiques ...

« Notes

Advent of code 2022 day 3

Créée le 11 Sep 2023

  • I could easily split strings in two parts
  • I fooled around trying to find a practical formula to find the common character but … 🤷‍♂️ Tried SPLIT but it wouldn’t accept empty string as split string.
  • So I relied on script like in Advent of code 2022 day1 .
    function myFunction() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var range = sheet.getDataRange();
      var numRows = range.getNumRows();
      for(var i=1; i<=numRows; i++) {
        var left = range.getCell(i, 2).getValue();
        var right = range.getCell(i, 3).getValue();
        var c = commonChar(left, right);
        range.getCell(i,4).setValue(c);
      } 
    }
    
    function commonChar(a,b) {
      for (const x of a.split("")) {
        for(const y of b.split("")) {
          if (x == y) {
            var c = x.charCodeAt() 
            return (c < 97)? c-65+26+1:c-97+1;
          }
        }
      }
      throw "no common char"
    }
    
  • It’s cheating but we need those⭐️ right?
  • Part 2 brings back the grouping problem I couldn’t solve easily in Advent of code 2022 day1 (had to resort to scripting). This time it would be easy to set an id because those are group of 3. Just divide row number by 3 and we get id for each group. Having the groups won’t help, will have to rely on script to find the common char anyway. So let’s script …
  • I adapted the script a bit and got the ⭐️
  • Conclusion
    • This day was cheated all along. I didn’t quit used the spreadsheet capabilities. What would be cool is to have some lower level language (like JS) available in formulas. Maybe I don’t know enough the library capabilities but it feels very lightweight.
    • The one thing I like the most is the visual feedback you get all the time. All the parameters are here to be scrutinized, and formulas are like reactive programming.