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"?
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 :)
:O very nice
just wanted to say that is an absolutely genius solution. nice work!
For part 1 I used directions approach. Starting from 'X' outward in all directions
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
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? :-)
This solution is fantastic.
Could you configure OBS not to record those annoying Windows sounds?
I wish you better luck for next days...
off by 1 errors never getting tired of reappearing.
I might save some time with try except to replace r2+r
It’s a good idea but I don’t think it works with negative indices? (They are valid and wrap-around to the end)
@@jonathanpaulson5053 oh! yeah, convenient python feature
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)
Loved it!
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