Code formatting

This commit is contained in:
akp 2020-12-18 01:51:37 +00:00
parent 0b5ad226d9
commit 23d7a7a500
No known key found for this signature in database
GPG key ID: D3E7EAA31B39637E
8 changed files with 67 additions and 29 deletions

View file

@ -1,7 +1,7 @@
from typing import List, Tuple
def find_value_n(instr:str, threshold:int) -> Tuple[int, List[int]]:
def find_value_n(instr: str, threshold: int) -> Tuple[int, List[int]]:
inp = [int(x) for x in instr.strip().split(",")]
indexes = {}
@ -20,7 +20,7 @@ def find_value_n(instr:str, threshold:int) -> Tuple[int, List[int]]:
new_number = c - previous_occurance_index
indexes[previous_number] = c
inp.append(new_number)
return inp[-1], inp
return inp[-1], inp

View file

@ -2,14 +2,16 @@ import common
import matplotlib.pyplot as plt
from typing import List
def make_graph(data:List[int], n:int):
def make_graph(data: List[int], n: int):
line_colour = "#3572a5"
plt.plot(list(range(len(data))), data, color=line_colour)
plt.title(f"AoC 2020, day 15 part {n}")
plt.xlabel("Iteration number")
plt.ylabel("Value")
def visualise(instr:str):
def visualise(instr: str):
print("Running part one...")
_, one = common.find_value_n(instr, 2020)
print("Making graph")
@ -26,4 +28,4 @@ def visualise(instr:str):
print("Saving high resolution")
fig = plt.gcf()
fig.set_size_inches(20.48, 10.8)
fig.savefig('1-hires.png', dpi=100)
fig.savefig("1-hires.png", dpi=100)

View file

@ -1,22 +1,28 @@
from typing import List, Tuple
class Rule:
name: str
ranges: Tuple[int, ...]
def __init__(self, instr:str) -> None:
def __init__(self, instr: str) -> None:
# arrival location: 26-482 or 504-959
field, conditions = instr.strip().split(": ")
self.name = field
ranges = conditions.split("or")
self.ranges = tuple([int(x) for x in ranges[0].strip().split("-")] + [int(x) for x in ranges[1].strip().split("-")])
self.ranges = tuple(
[int(x) for x in ranges[0].strip().split("-")]
+ [int(x) for x in ranges[1].strip().split("-")]
)
class Ticket:
fields: Tuple[int]
def __init__(self, instr:str) -> None:
def __init__(self, instr: str) -> None:
self.fields = tuple([int(x) for x in instr.strip().split(",")])
def parse(instr: str) -> Tuple[List[Rule], Ticket, List[Ticket]]:
raw_rules, raw_my_ticket, raw_other_tickets = instr.strip().split("\n\n")
@ -25,14 +31,22 @@ def parse(instr: str) -> Tuple[List[Rule], Ticket, List[Ticket]]:
my_ticket = Ticket(raw_my_ticket.split("\n")[-1])
other_tickets = [Ticket(x) for x in raw_other_tickets.strip("nearby tickets:\n").split("\n")]
other_tickets = [
Ticket(x) for x in raw_other_tickets.strip("nearby tickets:\n").split("\n")
]
return rules, my_ticket, other_tickets
def test_value(value:int, condition:Tuple[int, ...]) -> bool:
return condition[0] <= value <= condition[1] or condition[2] <= value <= condition[3]
def find_invalid(rules:List[Rule], tickets:List[Ticket]) -> Tuple[List[int], List[int]]:
def test_value(value: int, condition: Tuple[int, ...]) -> bool:
return (
condition[0] <= value <= condition[1] or condition[2] <= value <= condition[3]
)
def find_invalid(
rules: List[Rule], tickets: List[Ticket]
) -> Tuple[List[int], List[int]]:
# returns invalid values and indexes of invalid tickets
invalid_values = []
@ -48,4 +62,4 @@ def find_invalid(rules:List[Rule], tickets:List[Ticket]) -> Tuple[List[int], Lis
invalid_values.append(field)
invalid_indexes.append(i)
return invalid_values, invalid_indexes
return invalid_values, invalid_indexes

View file

@ -3,7 +3,7 @@ from common import *
def partOne(instr: str) -> int:
rules, _, tickets = parse(instr)
invalid_values, _ = find_invalid(rules, tickets)
return sum(invalid_values)

View file

@ -11,7 +11,7 @@ def partTwo(instr: str) -> int:
for idx in invalid_indexes:
tickets.pop(idx)
ticket_length = len(tickets[0].fields)
num_tickets = len(tickets)
num_rules = len(rules)

View file

@ -6,11 +6,17 @@ active_marker = "#"
inactive_marker = "."
translation_vectors_3d = [p for p in itertools.product([-1, 0, 1], repeat=3) if p != (0, 0, 0)]
translation_vectors_4d = [p for p in itertools.product([-1, 0, 1], repeat=4) if p != (0, 0, 0, 0)]
translation_vectors_3d = [
p for p in itertools.product([-1, 0, 1], repeat=3) if p != (0, 0, 0)
]
translation_vectors_4d = [
p for p in itertools.product([-1, 0, 1], repeat=4) if p != (0, 0, 0, 0)
]
def parse(instr: str, key:Callable[[int, int], int]) -> Dict[Tuple[int, int, int], str]:
def parse(
instr: str, key: Callable[[int, int], int]
) -> Dict[Tuple[int, int, int], str]:
input_lists = [[x for x in y] for y in instr.strip().split("\n")]
rtvl = {}
@ -20,4 +26,4 @@ def parse(instr: str, key:Callable[[int, int], int]) -> Dict[Tuple[int, int, int
if col != inactive_marker:
rtvl[key(x, y)] = col
return rtvl
return rtvl

View file

@ -2,16 +2,21 @@ from common import *
from typing import Dict, Tuple
def count_neighbours(matrix:Dict[Tuple[int, int, int], str], position:Tuple[int, int, int]) -> int:
def count_neighbours(
matrix: Dict[Tuple[int, int, int], str], position: Tuple[int, int, int]
) -> int:
num_neighbours = 0
x, y, z = position
for (x_delta, y_delta, z_delta) in translation_vectors_3d:
if matrix.get((x + x_delta, y+y_delta, z+z_delta), inactive_marker) == active_marker:
if (
matrix.get((x + x_delta, y + y_delta, z + z_delta), inactive_marker)
== active_marker
):
num_neighbours += 1
return num_neighbours
def iterate(matrix:Dict[Tuple[int, int, int], str]) -> Dict[Tuple[int, int, int], str]:
def iterate(matrix: Dict[Tuple[int, int, int], str]) -> Dict[Tuple[int, int, int], str]:
new = {}
# get min/max for x, y and z values
@ -28,7 +33,9 @@ def iterate(matrix:Dict[Tuple[int, int, int], str]) -> Dict[Tuple[int, int, int]
for x in range(min_x - 2, max_x + 2):
num_neighbours = count_neighbours(matrix, (x, y, z))
current_state = matrix.get((x, y, z), inactive_marker)
if (num_neighbours == 2 and current_state == active_marker) or num_neighbours == 3:
if (
num_neighbours == 2 and current_state == active_marker
) or num_neighbours == 3:
new[(x, y, z)] = active_marker
return new

View file

@ -1,16 +1,23 @@
from common import *
def count_neighbours(matrix:Dict[Tuple[int, int, int, int], str], position:Tuple[int, int, int, int]) -> int:
def count_neighbours(
matrix: Dict[Tuple[int, int, int, int], str], position: Tuple[int, int, int, int]
) -> int:
num_neighbours = 0
x, y, z, w = position
for (x_delta, y_delta, z_delta, w_delta) in translation_vectors_4d:
if matrix.get((x + x_delta, y+y_delta, z+z_delta, w+w_delta), inactive_marker) == active_marker:
if (
matrix.get(
(x + x_delta, y + y_delta, z + z_delta, w + w_delta), inactive_marker
)
== active_marker
):
num_neighbours += 1
return num_neighbours
def iterate(matrix:Dict[Tuple[int, int, int], str]) -> Dict[Tuple[int, int, int], str]:
def iterate(matrix: Dict[Tuple[int, int, int], str]) -> Dict[Tuple[int, int, int], str]:
new = {}
# get min/max for x, y and z values
@ -30,7 +37,9 @@ def iterate(matrix:Dict[Tuple[int, int, int], str]) -> Dict[Tuple[int, int, int]
for x in range(min_x - 2, max_x + 2):
num_neighbours = count_neighbours(matrix, (x, y, z, w))
current_state = matrix.get((x, y, z, w), inactive_marker)
if (num_neighbours == 2 and current_state == active_marker) or num_neighbours == 3:
if (
num_neighbours == 2 and current_state == active_marker
) or num_neighbours == 3:
new[(x, y, z, w)] = active_marker
return new
@ -38,7 +47,7 @@ def iterate(matrix:Dict[Tuple[int, int, int], str]) -> Dict[Tuple[int, int, int]
def partTwo(instr: str) -> int:
matrix = parse(instr, lambda x, y: (x, y, 0, 0))
for _ in range(6):
matrix = iterate(matrix)