Day 5 solution works, day 4 doesn't
This commit is contained in:
parent
4e43c85816
commit
85bb2ef568
4 changed files with 1526 additions and 0 deletions
60
2024/day04.py
Normal file
60
2024/day04.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
with open("./2024/inputs/day04.txt") as f:
|
||||||
|
puzzle = [l.strip() for l in f.readlines()]
|
||||||
|
|
||||||
|
maxY = len(puzzle) - 1
|
||||||
|
maxX = len(puzzle[0]) - 1
|
||||||
|
|
||||||
|
def possible_words(p, start):
|
||||||
|
y = start[0]
|
||||||
|
x = start[1]
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
# get the two possible horizontal matches
|
||||||
|
# skip them if x - 3 < 0 (word would be truncated on the left)
|
||||||
|
# or x + 3 > maxX (word would be truncated on the right)
|
||||||
|
if not ((x + 3) > maxX):
|
||||||
|
ret.append(str(f"{p[y][x]}{p[y][x + 1]}{p[y][x + 2]}{p[y][x + 3]}"))
|
||||||
|
|
||||||
|
if not ((x - 3) < 0):
|
||||||
|
ret.append(str(f"{p[y][x]}{p[y][x - 1]}{p[y][x - 2]}{p[y][x - 3]}"))
|
||||||
|
|
||||||
|
# get the two possible vertical matches
|
||||||
|
# skip them if y - 3 < 0 (word would be truncated on the top)
|
||||||
|
# or y + 3 > maxY (word would be truncated on the bottom)
|
||||||
|
if not ((y + 3) > maxY):
|
||||||
|
ret.append(str(f"{p[y][x]}{p[y + 1][x]}{p[y + 2][x]}{p[y + 3][x]}"))
|
||||||
|
|
||||||
|
if not ((y - 3) < 0):
|
||||||
|
ret.append(str(f"{p[y][x]}{p[y - 1][x]}{p[y - 2][x]}{p[y - 3][x]}"))
|
||||||
|
|
||||||
|
# get the four diagonals, again with some (basic) bounds checking
|
||||||
|
if not ((y + 3) > maxY) and not ((x + 3) > maxX):
|
||||||
|
ret.append(str(f"{p[y][x]}{p[y + 1][x + 1]}{p[y + 2][x + 2]}{p[y + 3][x + 3]}"))
|
||||||
|
|
||||||
|
if not ((y - 3) < 0) and not ((x - 3) < 0):
|
||||||
|
ret.append(str(f"{p[y][x]}{p[y - 1][x - 1]}{p[y - 2][x - 2]}{p[y - 3][x - 3]}"))
|
||||||
|
|
||||||
|
if not ((y + 3) > maxY) and not ((x - 3) < 0):
|
||||||
|
ret.append(str(f"{p[y][x]}{p[y + 1][x - 1]}{p[y + 2][x - 2]}{p[y + 3][x - 3]}"))
|
||||||
|
|
||||||
|
if not ((y - 3) < 0) and not ((x + 3) > maxX):
|
||||||
|
ret.append(str(f"{p[y][x]}{p[y - 1][x + 1]}{p[y - 2][x + 2]}{p[y - 3][x + 3]}"))
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
xmas_count = 0
|
||||||
|
for y in range(maxY):
|
||||||
|
for x in range(maxX):
|
||||||
|
if puzzle[y][x] == "X":
|
||||||
|
words = possible_words(puzzle, (y, x))
|
||||||
|
for word in words:
|
||||||
|
xmas_count += word.count("XMAS")
|
||||||
|
|
||||||
|
if puzzle[y][x] == "S":
|
||||||
|
words = possible_words(puzzle, (y, x))
|
||||||
|
for word in words:
|
||||||
|
xmas_count += word.count("SAMX")
|
||||||
|
|
||||||
|
print(xmas_count)
|
65
2024/day05.py
Normal file
65
2024/day05.py
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
def is_valid_update(update, order_reqs):
|
||||||
|
valid_update = True
|
||||||
|
for i in range(0, len(update)):
|
||||||
|
if update[i] in order_reqs:
|
||||||
|
for j in range(0, len(update)):
|
||||||
|
if update[j] in order_reqs[update[i]] and j < i:
|
||||||
|
valid_update = False
|
||||||
|
|
||||||
|
return valid_update
|
||||||
|
|
||||||
|
def fix_update(update, order_reqs):
|
||||||
|
if is_valid_update(update, order_reqs):
|
||||||
|
return update
|
||||||
|
else:
|
||||||
|
fixed_update = update
|
||||||
|
for i in range(0, len(update)):
|
||||||
|
if update[i] in order_reqs:
|
||||||
|
for j in range(0, len(update)):
|
||||||
|
if update[j] in order_reqs[update[i]] and j < i:
|
||||||
|
fixed_update[i], fixed_update[j] = fixed_update[j], fixed_update[i]
|
||||||
|
if is_valid_update(fixed_update, order_reqs):
|
||||||
|
return fixed_update
|
||||||
|
else:
|
||||||
|
return fix_update(fixed_update, order_reqs)
|
||||||
|
|
||||||
|
with open("./2024/inputs/day05.txt") as f:
|
||||||
|
lines = [l.strip() for l in f.readlines()]
|
||||||
|
|
||||||
|
order_reqs = {}
|
||||||
|
updates = []
|
||||||
|
nums_ended = False
|
||||||
|
|
||||||
|
for l in lines:
|
||||||
|
if l == "":
|
||||||
|
nums_ended = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not nums_ended:
|
||||||
|
before, after = [int(n) for n in l.split("|")]
|
||||||
|
if before not in order_reqs:
|
||||||
|
order_reqs[before] = [after]
|
||||||
|
else:
|
||||||
|
order_reqs[before].append(after)
|
||||||
|
|
||||||
|
else:
|
||||||
|
updates.append([int(n) for n in l.split(",")])
|
||||||
|
|
||||||
|
middle_total = 0
|
||||||
|
invalid_updates = []
|
||||||
|
for update in updates:
|
||||||
|
if is_valid_update(update, order_reqs):
|
||||||
|
middle_total += update[int((len(update) - 1) / 2)]
|
||||||
|
else:
|
||||||
|
invalid_updates.append(update)
|
||||||
|
|
||||||
|
print(middle_total)
|
||||||
|
|
||||||
|
middle_total_fixed = 0
|
||||||
|
for update in invalid_updates:
|
||||||
|
fixed_update = fix_update(update, order_reqs)
|
||||||
|
middle_total_fixed += fixed_update[int((len(fixed_update) - 1) / 2)]
|
||||||
|
|
||||||
|
print(middle_total_fixed)
|
10
2024/inputs/day04.txt
Normal file
10
2024/inputs/day04.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
....XXMAS.
|
||||||
|
.SAMXMS...
|
||||||
|
...S..A...
|
||||||
|
..A.A.MS.X
|
||||||
|
XMASAMX.MM
|
||||||
|
X.....XA.A
|
||||||
|
S.S.S.S.SS
|
||||||
|
.A.A.A.A.A
|
||||||
|
..M.M.M.MM
|
||||||
|
.X.X.XMASX
|
1391
2024/inputs/day05.txt
Normal file
1391
2024/inputs/day05.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue