This year I'll do my best to master the “Advent of Code”-Challenge. I'll update my Github every day, with my solution.
StarGo to...
Day 1:
Click here, to go to the challenge.Part 1:
from math import floor
with open('input.txt', 'r') as file:
print(sum([(int(x) // 3) - 2 for x in file.read().split('\n')]))
Part 2:
from math import floor
with open('input.txt', 'r') as in_dat:
int_list = [int(val) for val in in_dat.read().split('\n')]
print(sum([int_list.append(x // 3 - 2) or x // 3 - 2 for x in int_list if (x // 3 - 2) > 0]))
Day 2:
Click here, to go to the challenge.def compute_list(in_dat):
op_c_list = in_dat[::4]
for op_c_idx in range(len(op_c_list)):
real_idx = 4 * op_c_idx
if op_c_list[op_c_idx] == 1:
in_dat[in_dat[real_idx + 3]] = in_dat[in_dat[real_idx + 1]] + in_dat[in_dat[real_idx + 2]]
elif op_c_list[op_c_idx] == 2:
in_dat[in_dat[real_idx + 3]] = in_dat[in_dat[real_idx + 1]] * in_dat[in_dat[real_idx + 2]]
elif op_c_list[op_c_idx] == 99:
return in_dat
op_c_list = in_dat[::4]
def part1_test(test_data):
test_data[1] = 12
test_data[2] = 2
print(','.join(map(str, compute_list(test_data))))
def part2_test(test_data):
target = 19690720
for noun in range(99):
for verb in range(99):
test_data[1] = noun
test_data[2] = verb
result = compute_list(test_data.copy())
if result[0] == target:
print(100 * result[1] + result[2])
exit()
with open('input.txt', 'r') as file:
data = file.read().replace('\n', '')
# part1_test([int(x) for x in data.split(",")])
part2_test([int(x) for x in data.split(",")])
Day 3:
Click here, to go to the challenge.x_dir = {'L': -1, 'R': 1, 'U': 0, 'D': 0}
y_dir = {'L': 0, 'R': 0, 'U': 1, 'D': -1}
def convert_to_tuple_list(in_str):
return [(x[:1], int(x[1:])) for x in in_str]
def generate_path(command_list):
path = []
path_cursor = -1
for direction, length in command_list:
for i in range(length):
y, x = path[path_cursor + i] if bool(path) else (0, 0)
path.append(
(y + y_dir[direction], x + x_dir[direction]))
path_cursor += length
return path
with open('full_input.txt', 'r') as in_dat:
path1 = generate_path(convert_to_tuple_list(in_dat.readline().strip().split(',')))
path2 = generate_path(convert_to_tuple_list(in_dat.readline().split(',')))
print(min([abs(p[0]) + abs(p[1]) for p in set(path1).intersection(set(path2))]))
print(min([path1.index(p) + path2.index(p) for p in
set(path1).intersection(set(path2))]) + 2) # +2 because, we dont have (0,0) in our lists
Day 4:
Click here, to go to the challenge.in_range = (264360, 746325)
def rule_check(int_val):
digits = [int(x) for x in str(int_val)]
adjacent_same = {}
for idx, val in enumerate(digits):
if idx + 1 < len(digits) and val == digits[idx + 1]:
if val not in adjacent_same.keys():
adjacent_same[val] = 0
adjacent_same[val] += 1
if idx + 1 < len(digits) and val > digits[idx + 1]:
return False
return list(adjacent_same.values()) or False
print(len([x for x in range(*in_range) if rule_check(x)])) # Part1
print(len([x for x in range(*in_range) if rule_check(x) and 1 in rule_check(x)])) # Part2
Day 5:
Click here, to go to the challenge.def compute_list(in_dat):
idx = 0
def get_real_val(par_pos):
return in_dat[idx + par_pos + 1] if len(mode_switches) > par_pos and mode_switches[par_pos] else in_dat[
in_dat[idx + par_pos + 1]]
while idx in range(len(in_dat)):
op_code = int("".join([x for x in str(in_dat[idx])][len(str(in_dat[idx])) - 2:]))
mode_switches = [bool(int(x)) for x in str(in_dat[idx])][:len(str(in_dat[idx])) - 2][::-1] if len(
str(in_dat[idx])) > 2 else []
if op_code == 1:
in_dat[in_dat[idx + 3]] = get_real_val(0) + get_real_val(1)
idx += 4
elif op_code == 2:
in_dat[in_dat[idx + 3]] = get_real_val(0) * get_real_val(1)
idx += 4
elif op_code == 3:
in_dat[in_dat[idx + 1]] = int(input("Enter a number: "))
idx += 2
elif op_code == 4:
print(in_dat[in_dat[idx + 1]])
idx += 2
elif op_code == 5:
if get_real_val(0) != 0:
idx = get_real_val(1)
else:
idx += 3
elif op_code == 6:
if get_real_val(0) == 0:
idx = get_real_val(1)
else:
idx += 3
elif op_code == 7:
in_dat[in_dat[idx + 3]] = 1 if get_real_val(0) < get_real_val(1) else 0
idx += 4
elif op_code == 8:
in_dat[in_dat[idx + 3]] = 1 if get_real_val(0) == get_real_val(1) else 0
idx += 4
elif op_code == 99:
return in_dat
return in_dat
with open('input.txt', 'r') as file:
data = file.read().replace('\n', '')
print(compute_list(([int(x) for x in data.split(",")])))
Day 6:
Click here, to go to the challenge.with open('input.txt', 'r') as file:
in_dat = dict(reversed(line.strip().split(")")) for line in file)
def get_path_from_item(item, cf=None):
out = [item[0]]
if item[1] in in_dat.keys():
out.extend(get_path_from_item((item[1], in_dat[item[1]]), cf if cf else item[0]))
return out
paths = {i[0]: get_path_from_item(i) for i in in_dat.items()}
print(sum([len(p) for p in paths.values()])) # Part 1
print(len(set(paths["YOU"]) ^ set(paths["SAN"])) - 2) # Part 2
Day 7:
Click here, to go to the challenge.from itertools import permutations
def compute_list(in_dat, inputs):
input_iter = iter(inputs)
idx = 0
i_count = 0
def get_real_val(par_pos):
return in_dat[idx + par_pos + 1] if len(mode_switches) > par_pos and mode_switches[par_pos] else in_dat[
in_dat[idx + par_pos + 1]]
while idx in range(len(in_dat)):
op_code = int("".join([x for x in str(in_dat[idx])][len(str(in_dat[idx])) - 2:]))
mode_switches = [bool(int(x)) for x in str(in_dat[idx])][:len(str(in_dat[idx])) - 2][::-1] if len(
str(in_dat[idx])) > 2 else []
if op_code == 1:
in_dat[in_dat[idx + 3]] = get_real_val(0) + get_real_val(1)
idx += 4
elif op_code == 2:
in_dat[in_dat[idx + 3]] = get_real_val(0) * get_real_val(1)
idx += 4
elif op_code == 3:
in_dat[in_dat[idx + 1]] = next(input_iter)
i_count += 1
idx += 2
elif op_code == 4:
yield in_dat[in_dat[idx + 1]]
idx += 2
elif op_code == 5:
if get_real_val(0) != 0:
idx = get_real_val(1)
else:
idx += 3
elif op_code == 6:
if get_real_val(0) == 0:
idx = get_real_val(1)
else:
idx += 3
elif op_code == 7:
in_dat[in_dat[idx + 3]] = 1 if get_real_val(0) < get_real_val(1) else 0
idx += 4
elif op_code == 8:
in_dat[in_dat[idx + 3]] = 1 if get_real_val(0) == get_real_val(1) else 0
idx += 4
elif op_code == 99:
return in_dat
return in_dat
def amplifiers(program, sequence, feedback=0): # <- Thanks to nylocx (https://github.com/nylocx/adventofcode2019)
def amplifier_input(index):
yield sequence[index]
if index == 0:
while True:
yield feedback
yield from compute_list(program.copy(), amplifier_input(index - 1))
for feedback in compute_list(program.copy(), amplifier_input(len(sequence) - 1)):
pass
return feedback
with open('input.txt', 'r') as file:
data = [int(x) for x in file.read().replace('\n', '').strip().split(",")]
max_output = 0
for seq in permutations(range(0, 5), 5):
last_res = 0
for i in seq:
last_res = next(compute_list(data.copy(), [i, last_res]))
max_output = last_res if last_res > max_output else max_output
print(max_output)
max_output = 0
for sequence in permutations(range(5, 10), 5):
output = amplifiers(data, sequence)
max_output = output if output > max_output else max_output
print(max_output)
Day 8:
Click here, to go to the challenge.res = (25, 6)
with open('input.txt', 'r') as file:
in_dat = [int(i) for i in list(file.read().strip())]
in_dat = [in_dat[i:i + (res[0] * res[1])] for i in range(0, len(in_dat), (res[0] * res[1]))]
found = in_dat[0]
for layer in in_dat:
found = layer if layer.count(0) < found.count(0) else found
print(found.count(1) * found.count(2)) # Part 1
final_img = []
for i in range(res[0] * res[1]):
for layer in in_dat:
if layer[i] != 2:
final_img.append(layer[i])
break
for i in range(0, len(final_img), (res[0])):
print(''.join([str(x) for x in final_img[i:i + (res[0])]]).replace('0', ' ').replace('1', '█')) # Part 2