Day 2 | Advent of Code 2024 | Better Time Complexity

Поделиться
HTML-код
  • Опубликовано: 3 дек 2024

Комментарии • 20

  • @dkd0m23
    @dkd0m23 День назад +7

    I just took the simple approach of erasing one element and checking each time if the sequence is safe :)
    I knew there was a better way! Nice to see it here :)

  • @kevinkuruvilla6435
    @kevinkuruvilla6435 День назад +2

    I'm so happy you're back

  • @f-8ght
    @f-8ght День назад +13

    Yo bro you are back

  • @ghg6525
    @ghg6525 День назад +1

    HE IS BACK

  • @bladekiller2766
    @bladekiller2766 День назад +1

    For Part 2 I solved it just by checking whether exactly one is bad level:
    - Case when exactly one inc/dec is incorrect
    - Case when exactly one has incorrect distance (x < 1 or x > 3)
    Total 5 cases need to be checked for each report, which is O(N)

    • @difflocktwo
      @difflocktwo 19 часов назад

      if you have one bad level, couldn't that mean that you have two wrong distances? one leading to it and one leading away from it?

    • @bladekiller2766
      @bladekiller2766 15 часов назад

      @difflocktwo correct, that's why I mentioned 5 cases, 1 for the initial report and 2 for each of the cases that I mentioned.
      Be careful, I'm counting the direction case as one case because I'm considering only the direction which has the most correct levels.
      Also, when I have bad distance level Im just considering the next level for conparison, so effectively, I just store an array which tells whether from the current level away to the next there is valid distance, if not, then not safe.

  • @uzdik.student
    @uzdik.student День назад +1

    legendary grandmaster

  • @NKCSS
    @NKCSS День назад +1

    For part two, when you spot an issue, removing that index plus the next is enough, except where the index is 1, then you also need to try to remove index 0, as 0..1 is the setup for asc/desc which can be wrong. After reading all the Reddit posts, was happy that I figured that out while on stream, as most people didn’t seem to get that bit 😊

  • @animesh_chouhan
    @animesh_chouhan День назад +1

    This is the best version I could come up with for part 2(full code on my GitHub):
    def check(nums):
    return all(1

    • @Errichto
      @Errichto  День назад +1

      Oh, nice. If we focus on one direction (e.g. only increasing) then we don't need to bother with three consecutive values.

    • @animesh_chouhan
      @animesh_chouhan День назад

      @@Errichto yeah that makes the first part even simpler to implement:
      ret = 0
      for l in lines:
      nums = list(map(int, l.split()))
      if all(1

    • @bladekiller2766
      @bladekiller2766 День назад

      @@animesh_chouhan This looks like O(n^2) solution

    • @animesh_chouhan
      @animesh_chouhan День назад

      ​@@bladekiller2766 the check function is called only once and check_seq returns after 1 call.

    • @animesh_chouhan
      @animesh_chouhan 23 часа назад

      @@bladekiller2766 the check function is called only once by check_seq function and then returns so O(n)

  • @abikarthick601
    @abikarthick601 День назад

    Can't you have count of increasing and decreasing number and check whether atleast one of those is 0 or 1

    • @Errichto
      @Errichto  День назад +1

      Erasing an element by change the number of increments by 0 or 1 or 2. So it's not that easy.

  • @muitd
    @muitd День назад +1

    whooa