Advent of Code 2024 Day 4

Поділитися
Вставка
  • Опубліковано 27 гру 2024
  • Placed 723/215.
    Problem: adventofcode.c...
    Solution: github.com/jon...
    I had two bugs in part 1; I forgot about up-right/down-left and I had an off-by-one in the "in-bounds" condition for up-right/down-left.
    Not sure what I should've done to go faster here. Probably "don't have bugs"?

КОМЕНТАРІ • 18

  • @рнт
    @рнт 23 дні тому +15

    Interesting! What I did for part 2 is to iterate through all 'A's (accounting the borders!) and check if it's X-MAS by checking if sums of corner ASCII values are equal to 'M'+'S'. I like this solution since it ignores the order of corner letters :)

    • @antoineleduc7611
      @antoineleduc7611 21 день тому +1

      :O very nice

    • @siddmittal6907
      @siddmittal6907 11 днів тому

      just wanted to say that is an absolutely genius solution. nice work!

  • @ungerfall
    @ungerfall 23 дні тому +6

    For part 1 I used directions approach. Starting from 'X' outward in all directions

  • @svinther
    @svinther 23 дні тому +4

    Good for your approach that the searchstring was quite short :-) . I used a dfs like approach, starting with all the positions having the first character of the searchstring

    • @reik2006
      @reik2006 23 дні тому +2

      Did you by any chance also make the mistake of looking in all directions for next letter in the search string without considering the direction you came from? :-)

  • @chandra_83
    @chandra_83 22 дні тому +1

    This solution is fantastic.

  • @fredoverflow
    @fredoverflow 23 дні тому +2

    Could you configure OBS not to record those annoying Windows sounds?

  • @rastislavsvoboda4363
    @rastislavsvoboda4363 23 дні тому

    I wish you better luck for next days...

  • @harisimer
    @harisimer 23 дні тому +1

    off by 1 errors never getting tired of reappearing.

  • @ahxxm
    @ahxxm 23 дні тому +1

    I might save some time with try except to replace r2+r

    • @jonathanpaulson5053
      @jonathanpaulson5053  23 дні тому +8

      It’s a good idea but I don’t think it works with negative indices? (They are valid and wrap-around to the end)

    • @ahxxm
      @ahxxm 22 дні тому

      @@jonathanpaulson5053 oh! yeah, convenient python feature

  • @rastislavsvoboda4363
    @rastislavsvoboda4363 23 дні тому

    for part2:
    def get_X_directions(r,c):
    return [[(r+0,c+0),(r+0,c+2),(r+1,c+1),(r+2,c+0),(r+2,c+2)]]
    with something like this:
    ...
    res = 0
    for r in range(R):
    for c in range(C):
    dirs = get_X_directions(r,c)
    for word in ["MSAMS", "MMASS", "SMASM", "SSAMM"]:
    n = check(dirs, word, D)
    res += n
    return res
    maybe the optimal would be
    for r in range(1, R-1):
    for c in range(1, C-1):
    if D[r][c] == "A":
    dirs = [[(r-1,c-1),(r-1,c+1),(r+1,c+1),(r+1,c-1)]]
    for word in ["MSSM", "MMSS", "SMMS", "SSMM"]:
    res += check(dirs, word, D)

  • @prateeksaraswat1
    @prateeksaraswat1 23 дні тому

    Loved it!

  • @rastislavsvoboda4363
    @rastislavsvoboda4363 23 дні тому

    first I used
    def check1(r, c, D):
    RR = len(D)
    CC = len(D[0])
    res = 0
    if c+3 in range(CC) and D[r][c] == "X" and D[r][c+1] == "M" and D[r][c+2] == "A" and D[r][c+3] == "S":
    res += 1
    if c-3 in range(CC) and D[r][c] == "X" and D[r][c-1] == "M" and D[r][c-2] == "A" and D[r][c-3] == "S":
    res += 1
    if r+3 in range(RR) and D[r][c] == "X" and D[r+1][c] == "M" and D[r+2][c] == "A" and D[r+3][c] == "S":
    res += 1
    if r-3 in range(RR) and D[r][c] == "X" and D[r-1][c] == "M" and D[r-2][c] == "A" and D[r-3][c] == "S":
    res += 1
    if r+3 in range(RR) and c+3 in range(CC) and D[r][c] == "X" and D[r+1][c+1] == "M" and D[r+2][c+2] == "A" and D[r+3][c+3] == "S":
    res += 1
    if r-3 in range(RR) and c-3 in range(CC) and D[r][c] == "X" and D[r-1][c-1] == "M" and D[r-2][c-2] == "A" and D[r-3][c-3] == "S":
    res += 1
    if r+3 in range(RR) and c-3 in range(CC) and D[r][c] == "X" and D[r+1][c-1] == "M" and D[r+2][c-2] == "A" and D[r+3][c-3] == "S":
    res += 1
    if r-3 in range(RR) and c+3 in range(CC) and D[r][c] == "X" and D[r-1][c+1] == "M" and D[r-2][c+2] == "A" and D[r-3][c+3] == "S":
    res += 1
    return res
    later refactored just for fun to two methods, first to get coordinates, second check if correct char is at appropriate location
    def get_8_directions(r, c, word):
    res = []
    L = len(word)
    for dr in [-1, 0, 1]:
    for dc in [-1, 0, 1]:
    if (dr,dc) != (0,0):
    res.append([(r + i * dr, c + i * dc) for i in range(L)])
    return res
    def check(dirs, word, M):
    res = 0
    R = len(M)
    C = len(M[0])
    for lst in dirs:
    if all([r in range(R) and c in range(C) and M[r][c] == word[i] for i, (r, c) in enumerate(lst)]):
    res += 1
    return res