-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/fschatbot/Advent-Calendar…
- Loading branch information
Showing
21 changed files
with
705 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ __pycache__ | |
config.py | ||
*/inputs/*.txt | ||
Completed.md | ||
*.dump.* | ||
*.dump.* | ||
debug.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
... |
Oops, something went wrong.