Esempio n. 1
0
def get_new(grid: Grid2d):
    copy = grid.copy()

    min_b, max_b = grid.get_bounds()

    for y in range(min_b.y, max_b.y + 1):
        for x in range(min_b.x, max_b.x + 1):
            pos = Vector2.create(x, y)
            if grid[pos] == 'L' and get_num_surrounding(grid, pos, '#') == 0:
                copy[pos] = '#'
            elif grid[pos] == '#' and get_num_surrounding(grid, pos, '#') >= 4:
                copy[pos] = 'L'

    return copy
Esempio n. 2
0
from aoc_utils import Grid2d, Vector2

DIRECTIONS = [
    Vector2.create(x, y) for x in [-1, 0, 1] for y in [-1, 0, 1]
    if x != 0 or y != 0
]

get_num_surrounding = lambda grid, pos, char: sum(
    1 for d in DIRECTIONS if pos + d in grid and grid[pos + d] == char)


def get_new(grid: Grid2d):
    copy = grid.copy()

    min_b, max_b = grid.get_bounds()

    for y in range(min_b.y, max_b.y + 1):
        for x in range(min_b.x, max_b.x + 1):
            pos = Vector2.create(x, y)
            if grid[pos] == 'L' and get_num_surrounding(grid, pos, '#') == 0:
                copy[pos] = '#'
            elif grid[pos] == '#' and get_num_surrounding(grid, pos, '#') >= 4:
                copy[pos] = 'L'

    return copy


with open('./inp/11.txt') as f:
    grid = Grid2d('.', [line.rstrip() for line in f.readlines()])

last = None
Esempio n. 3
0
def rotate_point(p, d):
    if d > 0: d -= 360
    if d == -90: return Vector2(-p.y, p.x)
    if d == -180: return Vector2(-p.x, -p.y)
    if d == -270: return Vector2(p.y, -p.x)
Esempio n. 4
0
from aoc_utils import Vector2

with open('./inp/12.txt') as f:
    inp = [line.rstrip() for line in f.readlines()]


def rotate_point(p, d):
    if d > 0: d -= 360
    if d == -90: return Vector2(-p.y, p.x)
    if d == -180: return Vector2(-p.x, -p.y)
    if d == -270: return Vector2(p.y, -p.x)


facing_commands = set(['R', 'L', 'F'])
cardinal_directions = {
    "E": Vector2(1, 0),
    "N": Vector2(0, 1),
    "W": Vector2(-1, 0),
    "S": Vector2(0, -1)
}
cardinal_cmds = set(['E', 'N', 'W', 'S'])

ship = Vector2(0, 0)
waypoint = Vector2(10, 1)

for i in inp:
    cmd, amt = i[0], int(i[1:])
    if cmd in facing_commands:
        if cmd == 'F':
            ship += waypoint * amt
        else:
Esempio n. 5
0
from aoc_utils import Grid2d, Vector2

DIRECTIONS = [Vector2.create(x, y) for x in [-1, 0, 1] for y in [-1, 0, 1] if x != 0 or y != 0]

def get_nearest_seat(grid, pos, d):
    pos = pos + d
    while True:
        if pos not in grid:
            return None
        if grid[pos] != '.':
            return grid[pos]
        pos = pos + d

def get_num_surrounding(grid, pos, char): 
    sm = 0
    for d in DIRECTIONS:
        seat = get_nearest_seat(grid, pos, d)
        if seat == char: sm += 1
    return sm

def get_new(grid: Grid2d):
    copy = grid.copy()

    min_b, max_b = grid.get_bounds()

    for y in range(min_b.y, max_b.y + 1):
        for x in range(min_b.x, max_b.x + 1):
            pos = Vector2.create(x, y)
            if grid[pos] == 'L' and get_num_surrounding(grid, pos, '#') == 0:
                copy[pos] = '#'
            elif grid[pos] == '#' and get_num_surrounding(grid, pos, '#') >= 5:
Esempio n. 6
0
from aoc_utils import Vector2

with open('./inp/12.txt') as f:
    inp = [line.rstrip() for line in f.readlines()]

f_directions = {0:Vector2(1, 0),1:Vector2(0, 1),2:Vector2(-1, 0),3:Vector2(0,-1)}
f_delta = {'R':-1,'L':1}
c_directions = {"E":Vector2(1, 0),"N":Vector2(0, 1),"W":Vector2(-1, 0),"S":Vector2(0,-1)}
f_cmds = set(['R','L','F'])
c_cmds = set(['E','N','W','S'])

d = 0
pos = Vector2(0, 0)
for i in inp:
    cmd, amt = i[0], int(i[1:])
    if cmd in f_cmds:
        if cmd == 'F':
            pos += (f_directions[d] * amt)
        else:
            d = (d + (f_delta[cmd] * (amt // 90))) % 4
    elif cmd in c_cmds:
        pos += c_directions[cmd] * amt
print(pos.manhattan_distance(Vector2(0, 0)))