Example #1
0
def Astar(dico, h_type, visu_bool, options):
    ideal_grid = sid.set_ideal_grid(dico)
    grid = utils.load_grid(dico, options)  #chargement de la grille
    grid = utils.cast_list_to_numpy_array(grid,
                                          dico["size"])  #cast en type numpy
    if grid is None:
        sys.exit()
    start_t = time.time() if visu_bool == False else 0
    if visu_bool is False:
        grid, states, complexity, path = shortest_way(grid, ideal_grid, dico,
                                                      h_type, visu_bool)
    elif visu_bool is True:
        visu.shortest_way_visu(grid, ideal_grid, dico, h_type)
    if grid is None and states is None and complexity is None:
        if dico["iteration"] <= 0:
            print("{} is not a correct value for iteration param.".format(
                dico["iteration"]))
        else:
            print(
                "{} iterations was not enought to find the solution for this puzzle."
                .format(dico["iteration"]))
        sys.exit()
    end_t = time.time() if visu_bool == False else 0
    if not visu_bool:
        if options.states:
            utils.print_states(path, dico)
        utils.print_taquin(grid, dico, "final")
        print("Complexity in time: ", complexity)
        print("Complexity in size: ", states)
        print("Resolution time:", round(((end_t - start_t)), 2),
              "seconds" if round(((end_t - start_t)), 2) > 1 else "second")
    else:
        print("There's no timer when the -visual option is activated")
Example #2
0
# manh(p1[, p2]) - n-dim Manhattan dist; omit p2 for dist from origin
# is_prime
# adjs - set of dx,dy values for LRUD adjacencies
# diags - set of dx,dy values for diagonals
# all_dirs set of dx,dy values for all 8 surrounding values

test = len(sys.argv) > 1
input_file = 'input' + sys.argv[0].split('.')[1].lstrip('/') + ('.test' if test else '')

p1 = 0
p2 = 0

inp = []
with open(input_file) as f:
    for line in f:
        inp.append(line.strip())
        pass # if need custom
    inp = utils.load_num_lines(f) # one int per line
    inp = utils.load_comma_sep_nums(f) # 1,2,3,...
    inp = utils.load_split_lines(f) # asdf asdf asdf ...
    inp = utils.load_one_line_of_nums(f) # 1 2 3 ...
    inp = utils.load_grid(f, str) # 2D grid of X type

# Part 1

print(f'Part 1: {p1}')

# Part 2

print(f'Part 2: {p2}')
Example #3
0
from copy import copy

import sys

import utils

test = len(sys.argv) > 1
input_file = 'input' + sys.argv[0].split('.')[1].lstrip('/') + ('.test' if test
                                                                else '')

p1 = 0

inp = []
with open(input_file) as f:
    inp = utils.load_grid(f, int)  # 2D grid

# Part 1
lows = set()
for (x, y), v in inp.items():
    valid = True
    for dx, dy in utils.adjs:
        if inp.get((x + dx, y + dy),
                   float('inf')) <= v:  # neighbor is lower than this spot
            valid = False
            break
    if valid:
        p1 += 1 + v
        lows.add((x, y))
print(f'Part 1: {p1}')
Example #4
0
#!/usr/bin/env python3

import sys

import utils

test = len(sys.argv) > 1
input_file = 'input' + sys.argv[0].split('.')[1].lstrip('/') + ('.test' if test
                                                                else '')

p1 = 0
p2 = 0

with open(input_file) as f:
    grid = utils.load_grid(f)

CLEAN = 0
WEAK = 1
INFECTED = 2
FLAGGED = 3
grid2 = {}
for (x, y), v in grid.items():
    grid2[x, y] = CLEAN if v == '.' else INFECTED

directions = 'NESW'
moves = {
    'N': (0, -1),
    'E': (1, 0),
    'S': (0, 1),
    'W': (-1, 0),
}
Example #5
0
#!/usr/bin/env python3

import sys

import utils
# get_grid_edges: min_x, min_y, max_x, max_y
# display_grid
# adjs - set of dx,dy values for LRUD adjacencies

test = len(sys.argv) > 1
input_file = 'input' + sys.argv[0].split('.')[1].lstrip('/') + ('.test' if test
                                                                else '')

inp = []
with open(input_file) as f:
    grid = utils.load_grid(f, int)  # 2D grid of X type

# Part 1
min_x, min_y, max_x, max_y = utils.get_grid_edges(grid)
start = (min_y, min_x)
end = (max_y, max_x)
p1 = utils.find_cheapest(grid, start, end)[end]
print(f'Part 1: {p1}')

# Part 2
dx = max_x - min_x + 1
dy = max_y - min_y + 1

# expand grid
for y, x in set(grid.keys()):
    for mx in range(5):