Browse Source

fix subdir problem

main
Charles Reid 3 months ago
parent
commit
1e5ddb0e11
  1. 0
      Day-05/Python/example
  2. 0
      Day-05/Python/solution.py
  3. 54
      Day-07/Python/solution.py
  4. 32
      Day-08/Python/solution.py

0
Day-05/example → Day-05/Python/example

0
Day-05/solution.py → Day-05/Python/solution.py

54
Day-07/Python/solution.py

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
# Example input file
# Should return accumulated value of 3749 (part 1)
# Should return accumulated value of 11387 (part 2)
with open('example', 'r') as f:
with open("example", "r") as f:
lines = f.readlines()
# # Simpler example input file
@ -10,7 +10,7 @@ with open('example', 'r') as f: @@ -10,7 +10,7 @@ with open('example', 'r') as f:
# lines = f.readlines()
# Real input file from AoC website
with open('input', 'r') as f:
with open("input", "r") as f:
lines = f.readlines()
@ -30,7 +30,7 @@ add that test value to the final sum. Otherwise, don't. @@ -30,7 +30,7 @@ add that test value to the final sum. Otherwise, don't.
# Convert list of strings "<test_val>: <int1> <int2>" to mapping
test_map = {}
for line in lines:
if line=="":
if line == "":
continue
a, b = line.strip().split(":")
right_values = [int(j) for j in b.strip().split(" ")]
@ -40,39 +40,49 @@ for line in lines: @@ -40,39 +40,49 @@ for line in lines:
def find_valid_operators(target_value, sum_nums, operator_list, part2):
if len(operator_list) == len(sum_nums)-1:
if len(operator_list) == len(sum_nums) - 1:
# Base case, picked all operators, now check if they yield target value
accumulator = sum_nums[0]
#opstr = f"{accumulator}"
# opstr = f"{accumulator}"
for i, op in enumerate(operator_list):
if op=="*":
accumulator *= sum_nums[i+1]
elif op=="+":
accumulator += sum_nums[i+1]
elif op=="||" and part2 is True:
accumulator = int(str(accumulator) + str(sum_nums[i+1]))
if op == "*":
accumulator *= sum_nums[i + 1]
elif op == "+":
accumulator += sum_nums[i + 1]
elif op == "||" and part2 is True:
accumulator = int(str(accumulator) + str(sum_nums[i + 1]))
else:
raise Exception(f"Unknown operator {op}")
#opstr += op + str(sum_nums[i+1])
# opstr += op + str(sum_nums[i+1])
if accumulator == target_value:
#print(f"Checking operator list: {target_value} = {accumulator} = {opstr} (success)")
# print(f"Checking operator list: {target_value} = {accumulator} = {opstr} (success)")
return True
else:
#print(f"Checking operator list: {target_value} != {accumulator} = {opstr} (failure)")
# print(f"Checking operator list: {target_value} != {accumulator} = {opstr} (failure)")
return False
else:
# Recursive case
if part2:
return \
find_valid_operators(target_value, sum_nums, operator_list+["+"], part2) \
or find_valid_operators(target_value, sum_nums, operator_list+["*"], part2) \
or find_valid_operators(target_value, sum_nums, operator_list+["||"], part2)
return (
find_valid_operators(
target_value, sum_nums, operator_list + ["+"], part2
)
or find_valid_operators(
target_value, sum_nums, operator_list + ["*"], part2
)
or find_valid_operators(
target_value, sum_nums, operator_list + ["||"], part2
)
)
else:
return \
find_valid_operators(target_value, sum_nums, operator_list+["+"], part2) \
or find_valid_operators(target_value, sum_nums, operator_list+["*"], part2)
return find_valid_operators(
target_value, sum_nums, operator_list + ["+"], part2
) or find_valid_operators(
target_value, sum_nums, operator_list + ["*"], part2
)
accumulator = 0
for k, right_values in test_map.items():
@ -84,7 +94,6 @@ for k, right_values in test_map.items(): @@ -84,7 +94,6 @@ for k, right_values in test_map.items():
print(f"Part 1: accumulated value: {accumulator}")
"""
Part 2:
Repeat Part 1, but include a third concatenation operator.
@ -102,4 +111,3 @@ for k, right_values in test_map.items(): @@ -102,4 +111,3 @@ for k, right_values in test_map.items():
accumulator2 += test_value
print(f"Part 2: accumulated value: {accumulator2}")

32
Day-08/Python/solution.py

@ -3,21 +3,21 @@ from collections import defaultdict @@ -3,21 +3,21 @@ from collections import defaultdict
# Example input file
# Should return 14 unique antinode locations for part 1
with open('example', 'r') as f:
with open("example", "r") as f:
lines = f.readlines()
# Part 1 simple example input file
# Should return 2 unique antinode locations for part 1
with open('example2', 'r') as f:
with open("example2", "r") as f:
lines = f.readlines()
# Part 2 simple example input file
# Should return 9 unique antinode locations for part 2
with open('example3', 'r') as f:
with open("example3", "r") as f:
lines = f.readlines()
# Real input file (provided on advent of code site)
with open('input', 'r') as f:
with open("input", "r") as f:
lines = f.readlines()
NROWS = len(lines)
@ -62,7 +62,7 @@ def draw_grid(locs): @@ -62,7 +62,7 @@ def draw_grid(locs):
line = lines[i].strip()
newline = ""
for j in range(len(line)):
if line[j]=='.':
if line[j] == ".":
if (i, j) in locs:
newline += "#"
else:
@ -75,12 +75,12 @@ def draw_grid(locs): @@ -75,12 +75,12 @@ def draw_grid(locs):
def eliminate_invalid_antinodes(all_locs):
valid_locs = set()
for loc in all_antinode_locs:
c1 = loc[0] >= 0 and loc[0] <= NROWS-1
c2 = loc[1] >= 0 and loc[1] <= NCOLS-1
c1 = loc[0] >= 0 and loc[0] <= NROWS - 1
c2 = loc[1] >= 0 and loc[1] <= NCOLS - 1
if c1 and c2:
valid_locs.add(loc)
valid_locs = list(valid_locs)
valid_locs.sort(key = lambda x: x[0])
valid_locs.sort(key=lambda x: x[0])
return valid_locs
@ -96,8 +96,8 @@ for frequency in antenna_map.keys(): @@ -96,8 +96,8 @@ for frequency in antenna_map.keys():
# Iterate pairwise over each antenna location
antenna_locs = antenna_map[frequency]
for i in range(len(antenna_locs)):
for j in range(i+1, len(antenna_locs)):
if antenna_locs[i]==antenna_locs[j]:
for j in range(i + 1, len(antenna_locs)):
if antenna_locs[i] == antenna_locs[j]:
continue
anti1, anti2 = antinode_locations(antenna_locs[i], antenna_locs[j])
all_antinode_locs.update([anti1, anti2])
@ -116,6 +116,7 @@ exactly in line with at least two antennas of the @@ -116,6 +116,7 @@ exactly in line with at least two antennas of the
same frequency, regardless of distance.
"""
def extended_antinode_locations(p1, p2):
r1, c1 = p1
r2, c2 = p2
@ -124,20 +125,21 @@ def extended_antinode_locations(p1, p2): @@ -124,20 +125,21 @@ def extended_antinode_locations(p1, p2):
dx = c2 - c1
antis = []
for i in range(0, max(NROWS//dy, NCOLS//dx) + 1):
antis.append((r1 - i*dy, c1 - i*dx))
antis.append((r2 + i*dy, c2 + i*dx))
for i in range(0, max(NROWS // dy, NCOLS // dx) + 1):
antis.append((r1 - i * dy, c1 - i * dx))
antis.append((r2 + i * dy, c2 + i * dx))
return antis
# Iterate over each frequency
all_antinode_locs = set()
for frequency in antenna_map.keys():
# Iterate pairwise over each antenna location
antenna_locs = antenna_map[frequency]
for i in range(len(antenna_locs)):
for j in range(i+1, len(antenna_locs)):
if antenna_locs[i]==antenna_locs[j]:
for j in range(i + 1, len(antenna_locs)):
if antenna_locs[i] == antenna_locs[j]:
continue
antis = extended_antinode_locations(antenna_locs[i], antenna_locs[j])
all_antinode_locs.update(antis)

Loading…
Cancel
Save