Compare commits

...

7 Commits

6 changed files with 192 additions and 0 deletions

27
brechtje/3/3-1.py Normal file
View File

@ -0,0 +1,27 @@
input = open("input.txt").read().splitlines()
# line = current line that is being searched
# start = at what index to start searching
# for the first digit: 0
# for the second digit: right after the index of the first digit
# is_first_digit = boolean, True if first digit is being found, False if second digit is being found
# num_to_find = (next) number to search for in the string (line), starts at 9 decreases when not found
def find_biggest_num_index(line, start, is_first_digit):
index = -1
num_to_find = 9
while True:
index = line.find(str(num_to_find), start)
# decrease num_to_find if number is not found
# OR we are currently searching for the first digit and the number found is the last char in the string
if index == -1 or (is_first_digit and index == len(line) - 1):
num_to_find -= 1
else:
return index
answer = 0
for line in input:
first_digit_index = find_biggest_num_index(line, 0, True)
second_digit_index = find_biggest_num_index(line, first_digit_index + 1, False)
answer += int(line[first_digit_index] + line[second_digit_index])
print(answer)

34
brechtje/4/4-1.py Normal file
View File

@ -0,0 +1,34 @@
input = open('input.txt', 'r').read().splitlines()
surrounding_spaces = [
{'x': -1, 'y': -1},
{'x': 0, 'y': -1},
{'x': 1, 'y': -1},
{'x': -1, 'y': 0},
{'x': 1, 'y': 0},
{'x': -1, 'y': 1},
{'x': 0, 'y': 1},
{'x': 1, 'y': 1}
]
answer = 0
x_max = len(input[0]) - 1
y_max = len(input) - 1
for y, line in enumerate(input):
for x, char in enumerate(line):
if char == "@":
rolls = 0
for space in surrounding_spaces:
current_x = x + space['x']
current_y = y + space['y']
if current_x < 0 or current_y < 0 or current_x > x_max or current_y > y_max:
continue
elif input[current_y][current_x] == "@":
rolls += 1
if rolls > 3:
break
if rolls < 4:
answer += 1
print(answer)

45
brechtje/4/4-2.py Normal file
View File

@ -0,0 +1,45 @@
raw_input = open('input.txt', 'r').read().splitlines()
input = []
for line in raw_input:
input.append(list(line))
surrounding_spaces = [
{'x': -1, 'y': -1},
{'x': 0, 'y': -1},
{'x': 1, 'y': -1},
{'x': -1, 'y': 0},
{'x': 1, 'y': 0},
{'x': -1, 'y': 1},
{'x': 0, 'y': 1},
{'x': 1, 'y': 1}
]
prev_answer = 0
answer = 0
x_max = len(input[0]) - 1
y_max = len(input) - 1
while True:
for y, line in enumerate(input):
for x, char in enumerate(line):
if char == "@":
rolls = 0
for space in surrounding_spaces:
current_x = x + space['x']
current_y = y + space['y']
if current_x < 0 or current_y < 0 or current_x > x_max or current_y > y_max:
continue
elif input[current_y][current_x] == "@":
rolls += 1
if rolls > 3:
break
if rolls < 4:
answer += 1
input[y][x] = "."
if answer == prev_answer:
break
prev_answer = answer
print(answer)

19
brechtje/5/5-1.py Normal file
View File

@ -0,0 +1,19 @@
input = open('input.txt', 'r').read().splitlines()
ranges = []
food = []
answer = 0
for line in input:
if '-' in line:
ranges.append(list(map(int, line.split('-'))))
elif line != '':
food.append(int(line))
for item in food:
for range in ranges:
if range[0] <= item <= range[1]:
answer += 1
break
print(answer)

45
brechtje/6/6-1.py Normal file
View File

@ -0,0 +1,45 @@
input = open('input.txt', 'r').read().splitlines()
nums = []
current_num = ''
for line in input:
if current_num != '':
if column_index > len(nums) - 1:
nums.append([current_num])
else:
nums[column_index].append(current_num)
current_num = ''
column_index = 0
for char in line:
if char == ' ':
if current_num != '':
if column_index > len(nums) - 1:
nums.append([current_num])
column_index += 1
else:
nums[column_index].append(current_num)
column_index += 1
current_num = ''
else:
current_num += char
answer = 0
for column in nums:
column_combo = 0
if column[-1] == '+':
for num in column[:-1]:
column_combo += int(num)
answer += column_combo
elif column[-1] == '*':
for num in column[:-1]:
if column_combo == 0:
column_combo += int(num)
else:
column_combo *= int(num)
answer += column_combo
print(answer)

22
brechtje/7/7-1.py Normal file
View File

@ -0,0 +1,22 @@
raw_input = open("input.txt", "r").read().splitlines()
input = []
for line in raw_input:
input.append(list(line))
input[1][input[0].index('S')] = '|'
answer = 0
for y, line in enumerate(input):
for x, char in enumerate(line):
if y != len(input) - 1:
if char == '|' and input[y + 1][x] != '^':
input[y + 1][x] = '|'
elif char == '|' and input[y + 1][x] == '^':
answer += 1
input[y + 1][x - 1] = '|'
input[y + 1][x + 1] = '|'
print(''.join(line))
print(answer)