Advent of Code - adventofcode.com

Posted on
Page
of 21
  • Running behind, but, day 1 part 2 has stumped me. refactored to account for all edge cases I could think of, solution still wrong, stole a solution from reddit, just to see what the result was, maybe to get an idea of where to go wrong, stolen solution from reddit also doesn't work. Dang.

  • What would your solution produce for oneight?

  • I had a similar problem and figured this was what caught me out, but I've done nothing to try and fix it yet.

  • Produces 18, originally didn't, but refactored it to after seeing it was a case that needed covering. Really stumped on what's up with it.

  • What would you produce for aaa1bbb

  • Here were my test cases:-

                    // Provided examples
                    {"1abc2", 1, 1, 2},
                    {"pqr3stu8vwx", 1, 3, 8},
                    {"a1b2c3d4e5f", 1, 1, 5},
                    {"treb7uchet", 1, 7, 7},
                    {"two1nine", 2, 2, 9},
                    {"eightwothree", 2, 8, 3},
                    {"abcone2threexyz", 2, 1, 3},
                    {"xtwone3four", 2, 2, 4},
                    {"4nineeightseven2", 2, 4, 2},
                    {"zoneight234", 2, 1, 4},
                    {"7pqrstsixteen", 2, 7, 6},
                    // Edge cases
                    {"two1abc2three", 1, 1, 2},
                    {"threeightwo", 2, 3, 2},
                    {"oneight", 2, 1, 8},
    

    Each test entry is a string, then either 1 or 2 depending on whether it is for part1 or part2, then the first and last numbers that should be found.

    So a line like {"two1abc2three", 1, 1, 2}, should ignore the two at the start and the three at the end as they're not searched for in part 1.

  • Even further behind, but I finally got day 1 part 1 done in Z80 assembler: https://github.com/rhowe/aoc/tree/main/2023/01

    Part 2 shouldn't be so bad

  • These are my test cases, with what they produce:

    two1nine 29
    eightwothree 83
    abcone2threexyz 13
    xtwone3four 24
    4nineeightseven2 42
    zoneight234 14
    7pqrstsixteen 76
    oneight 18
    one 1
    eightwo 82
    1 1
    aaa1bbb 1
    threeightwo 32
    

    I think I'm ok with just being "done" with day1 now. I will just not have that star. Day2 was nice and smooth.

  • one 1 there's your problem. Something like aaa1bbb should map to 11 not 1

  • That caught me out too

  • My solution to day 1 part two inspired by https://en.wikipedia.org/wiki/Five_(group)

    (replace all 'five' with '5ive' in the input, etc)

  • oh ffs. Thanks! I intentionally guarded against that as well...

  • Spoke too soon, updated that, updated test cases, ran with actual data, incorrect answer. RIP.

  • In case anyone has morbid fascination, gist with my code (javascript): https://gist.github.com/ZooeyMiller/aaaf22e14fc5401f06dcd16c3ed8b605

    Last year I tried it in haskell, thought I'd make my life easier by doing it in JS this year 🙃

  • Day 7: ugly code but got there with only a few stumbles, will clean it up once I've had a chance to think of a cleaner way of doing it all.

  • I might be way off the mark with this, it's a long time since I did anything with JavaScript but does your use of indexof fail when there is only one number in the input string? As cyclotron mentioned an input of "1" and "one" should both produce 11, indexof will only find the first index and you need to also find the last index (which in this case is the same).

  • The short answer is no, it still works.

    The flow is it takes the string, pulls the number (word) or number (int) occurrences out into an object, then organises them into an array in the order they occurred in the original string, and then maps them to the relevant number. Then takes the first and last element of that array and concatenates them together. So 1 will result in an array of ['1'], so concatenating the first and last element of that array together results in 11.

    But ty for the thought.

  • Don't have time to go through your code (and I've never really done .js) but if you post your input in a gist and the individual numbers each line produces I can see what mine does differently and give you a hint.

    (You obviously don't just want to be told what the answer should be as there's no fun in that.)

  • Might require some refactoring, but findIndex and findLastIndex might be useful, as it means you don't need to care about any of the matches in between. Avoids most of the edge cases.

  • I think your problem is here:

    Object.keys(strNumMap).reduce((res, key) => {
      const index = str.indexOf(key);
      if (index > -1) {
        res[key] = index;
      }
      return res;
    }
    

    It's not handling strings with multiple of the same number correctly. E.g. 121 is getting mapped to 12 instead of 11.

  • Nice one, that's definitely it.

  • Yep, that was it. Refactored to get arr of all indexes rather than just the first and got the right answer. Thank you!

    Would have been nice to have an example in the test data with that structure, but oh well!

  • Finally got round to getting day 1 part 2 done (again, Z80 assembler): https://github.com/rhowe/aoc/blob/main/2023/01/part2.S

  • Definitely lagging behind this year, but enjoyed the Lagrange interpolation solution today.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Advent of Code - adventofcode.com

Posted by Avatar for Drakien @Drakien

Actions