Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fschatbot committed Dec 7, 2024
2 parents b169339 + ad622af commit f4f12b3
Show file tree
Hide file tree
Showing 21 changed files with 705 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ __pycache__
config.py
*/inputs/*.txt
Completed.md
*.dump.*
*.dump.*
debug.html
6 changes: 2 additions & 4 deletions 2015/18.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def part1(data):
# Now we have to go though 100 steps
for _ in range(100):
# First we make a new grid
new_grid = np.zeros(grid_shape, np.bool)
new_grid = np.zeros(grid_shape, bool)
# Next we loop though the grid lights
for row in range(grid_shape[0]):
for col in range(grid_shape[1]):
Expand All @@ -56,7 +56,6 @@ def part1(data):
new_grid[row, col] = True
# Set the new grid to the old grid, update counter
grid = new_grid
print(_,end='\r')
return np.sum(grid)

def part2(data):
Expand All @@ -69,7 +68,7 @@ def part2(data):
# Now we have to go though 100 steps
for _ in range(100):
# First we make a new grid
new_grid = np.zeros(grid_shape, np.bool)
new_grid = np.zeros(grid_shape, bool)
# Next we loop though the grid lights
for row in range(grid_shape[0]):
for col in range(grid_shape[1]):
Expand All @@ -87,7 +86,6 @@ def part2(data):
new_grid[row, col] = True
# Set the new grid to the old grid, update counter
grid = new_grid
print(_,end='\r')
return np.sum(grid)

# THIS ONE WAS A LOT OF WORK
3 changes: 2 additions & 1 deletion 2015/20.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

def factor_sum(num):
# returns a list of factors of num
factors = [i+1 for i in range(num) if num % (i+1) == 0]
factors = [i for i in range(1, num+1//2) if num % i == 0]

return sum(factors)

def part1(data):
Expand Down
2 changes: 1 addition & 1 deletion 2015/6.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def proccess(line):

def part1(data):
"""The Code is supposed to run here"""
grid = np.zeros((1000, 1000), dtype=np.bool) # True represents lit and False represents off
grid = np.zeros((1000, 1000), dtype=bool) # True represents lit and False represents off
for line in data:
if line.startswith('toggle'):
x1, y1, x2, y2 = proccess(line.lstrip('toggle '))
Expand Down
61 changes: 50 additions & 11 deletions 2016/20.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
import numpy as np

split_data = True
def parse(data):
processed = []
for line in data.strip().split('\n'):
num1, num2 = line.split('-')
processed.append((int(num1), int(num2)))

return sorted(processed, key=lambda x: x[0])

split_data = parse
completed = True
raw_data = None # Not To be touched

def part1(data):
nums = np.zeros(4294967295+1, dtype=np.bool8)
for line in data:
num1, num2 = line.split('-')
nums[int(num1):int(num2)+1] = True
return np.where(nums == False)[0][0]
lowest = 0

for low, high in data:
if low <= lowest <= high:
lowest = high + 1

return lowest

def part2(data):
nums = np.zeros(4294967295+1, dtype=np.bool8)
for line in data:
num1, num2 = line.split('-')
nums[int(num1):int(num2)+1] = True
return np.sum(nums == False)
allowedRange = [(0, 4294967295)]

for low, high in data:
newRange = []
for Al_low, Al_high in allowedRange:
if Al_low < low and Al_high > high:
# The middle part of the range is in the black-list
newRange.append((Al_low, low-1))
newRange.append((high+1, Al_high))
elif low <= Al_low <= high:
# The lower part of the range is in the black-list
if Al_high <= high:
# Then the whole range is in the black-list
continue
else:
# The above part of the range is not in the black-list
newRange.append((high+1, Al_high))
elif low <= Al_high <= high:
# The above part of the range is in the black-list
if Al_low < low:
# The below part of the range is not in blacklist
newRange.append((Al_low, low-1))
else:
# The below part of the range is in the black-list
continue
else:
# The whole range is not in the black-list
newRange.append((Al_low, Al_high))

allowedRange = newRange
if len(allowedRange) == 0:
break

return sum([high - low + 1 for low, high in allowedRange])
5 changes: 1 addition & 4 deletions 2016/21.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ def part2(data):
# then we know what the password is

scrambled = "fbgdceah"
possible = []
for i in permutations(scrambled):
if scramble(i, data) == scrambled:
possible.append(''.join(i))
# There can be multiple possibilities, so we return the first one
return possible[0]
return ''.join(i)
2 changes: 1 addition & 1 deletion 2016/22.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def part2(data):
# Pretty printing the data
with open("2016/day 22.dump.txt", "w") as f:
# Printing the display into the console
display = grid.astype(np.str)
display = grid.astype(str)
display[display == '1'] = '#'
display[display == '0'] = '_'
display[display == '-1'] = '.'
Expand Down
2 changes: 1 addition & 1 deletion 2016/8.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def part2(data):
display[int(x),:] = np.roll(display[int(x),:],int(y))
# Code for part 2 begins here
# Printing the display into the console
display = display.astype(np.str)
display = display.astype(str)
display[display == '1'] = '\u2588' # Full block, makes it easier to read
display[display == '0'] = ' '
for row in display:
Expand Down
2 changes: 1 addition & 1 deletion 2022/10.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ def part2(data):
ins += 1

for row in screen: print(''.join(row))
return 'This time, there will be no returned output as its super hard to guess all the letter patterns. However, a human can read the above display and figure out the output!'
print('This time, there will be no returned output as its super hard to guess all the letter patterns. However, a human can read the above display and figure out the output!')
61 changes: 61 additions & 0 deletions 2022/18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

split_data = True
completed = True
raw_data = None # Not To be touched

def part1(data):
# One easy way is to just calculate the laviathan distance and if its 1 then subtract 2 from the upper limit
limit = len(data) * 6

checks = [(-1, 0, 0), (1, 0, 0), (0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1)]

for cube in data:
x, y, z = map(int, cube.split(','))

for dx, dy, dz in checks:
nx, ny, nz = x + dx, y + dy, z + dz
if f"{nx},{ny},{nz}" in data:
limit -= 1

return limit

def part2(data):
# Will be solved using flood-fill
cubes = []
maxX = float('-inf')
maxY = float('-inf')
maxZ = float('-inf')

for cube in data:
x, y, z = map(int, cube.split(','))
maxX = max(maxX, x)
maxY = max(maxY, y)
maxZ = max(maxZ, z)
cubes.append((x, y, z))

maxX, maxY, maxZ = maxX + 2, maxY + 2, maxZ + 2 # <- God knows why I have a two off error!

# print(f'{maxX}x{maxY}x{maxZ} grid')

surface = 0 # The number of times we hit the cube during the flood fill

checks = [(-1, 0, 0), (1, 0, 0), (0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1)]
queue = [(-1, -1, -1)]
visited = set()

while queue:
x, y, z = queue.pop(0)

for dx, dy, dz in checks:
nx, ny, nz = x + dx, y + dy, z + dz
if not (-1 <= nx < maxX and -1 <= ny < maxY and -1 <= nz < maxZ): continue
if f"{nx},{ny},{nz}" in visited: continue
if (nx, ny, nz) in cubes:
surface += 1
continue
queue.append((nx, ny, nz))
visited.add(f"{nx},{ny},{nz}")

return surface
69 changes: 69 additions & 0 deletions 2022/21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def parse(data):
monkeys = {}

for monkey in data.split('\n'):
name, operation = monkey.split(': ')
if any(op in operation for op in '+-/*'):
monkeys[name] = operation.split(' ')
else:
monkeys[name] = int(operation)

return monkeys

split_data = parse
completed = True
raw_data = None # Not To be touched

def part1(data):
def get_monkey(name):
yells = data[name]
if isinstance(yells, int): return yells
mon1 = get_monkey(yells[0])
mon2 = get_monkey(yells[2])

match yells[1]:
case '+':
return mon1 + mon2
case '-':
return mon1 - mon2
case '*':
return mon1 * mon2
case '/':
return mon1 // mon2

return get_monkey('root')


def part2(data):
# Inbuilt complex number class to the rescue!
data['root'][1] = '='

def get_monkey(name):
if name == 'humn': return complex(0, 1) # Change for part 2
yells = data[name]
if isinstance(yells, int): return yells
mon1 = get_monkey(yells[0])
mon2 = get_monkey(yells[2])

match yells[1]:
case '+':
return mon1 + mon2
case '-':
return mon1 - mon2
case '*':
return mon1 * mon2
case '/':
return mon1 / mon2
case '=':
return mon1, mon2 # Change for part 2

out1, out2 = get_monkey('root')
comp, norm = (out1, out2) if isinstance(out1, complex) else (out2, out1)

# This ends give the following algebra equation:
# comp.real + comp.imag * x = norm

return int((norm - comp.real) // comp.imag) # A bit of algebra
68 changes: 68 additions & 0 deletions 2022/22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re

def parse(data):
grid = {}
board, instructions = data.split('\n\n')

for y, line in enumerate(board.split('\n')):
for x, char in enumerate(line):
if char == ' ': continue
grid[(x, y)] = char

return grid, re.findall(r'\d+|L|R', instructions)

split_data = parse
completed = 1
raw_data = None # Not To be touched

def part1(data):
grid, instructions = data
# Super weird puzzle
movements = [(1, 0), (0, 1), (-1, 0), (0, -1)] # Go right, down, left, up
rotation = 0 # Starting out facing right

# Find the start
x, y = min(x for x, y in grid.keys() if y == 0), 0

# print('We start at:', x, y, rotation)

for op in instructions:
if op == 'R':
rotation = (rotation + 1) % 4
continue
elif op == 'L':
rotation = (rotation - 1) % 4
continue

dx, dy = movements[rotation]
# print("rot:", dx, dy, op)
for _ in range(int(op)):
nx, ny = x + dx, y + dy
future = grid.get((nx, ny))
if not future:
# We can do this because of how the map looks. Special to only these kinds of map
if rotation == 0:
nx = min(gx for gx, gy in grid.keys() if gy == ny)
elif rotation == 1:
ny = min(gy for gx, gy in grid.keys() if gx == nx)
elif rotation == 2:
nx = max(gx for gx, gy in grid.keys() if gy == ny)
elif rotation == 3:
ny = max(gy for gx, gy in grid.keys() if gx == nx)
future = grid.get((nx, ny))

if future == '.':
x, y = nx, ny # We move
elif future == '#':
break

# print("We end at", x, y, rotation)

return 1000 * (y+1) + 4 * (x+1) + (rotation % 4)

def part2(data):
# HELL NO
...
Loading

0 comments on commit f4f12b3

Please sign in to comment.