Just write the word! Part 2

Once again, if red letter is starting point and only allowed movements are: up-left, up, up-right, left, right, down-left, down and down-right, then, on how many ways word RADAR can be "written" (by using given movements for the given map)

Important observation: Notice that both ways ("blue" and "brown" way) in the following example are completely valid.

This is only one segment of the problem's map used only for explanatory purposes This is only one segment of the problem's map used only for explanatory purposes


Question link


The answer is 392.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Piotr Idzik
May 24, 2020

It is an overkill for sure, but here is my code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
solution of:
    https://brilliant.org/problems/just-write-the-word-part-2/?ref_id=1396388
"""
def make_move(cur_pos, dir_code):
    """
    returns the coordinates of a point cur_pos shifted in the given direction
    """
    dir_code_data = {'N': (0, 1), 'S': (0, -1), 'E':(1, 0), 'W':(-1, 0),
                     'NE': (1, 1), 'SE': (1, -1), 'SW': (-1, -1), 'NW': (-1, 1)}
    cur_dir = dir_code_data[dir_code]
    return tuple([cur_pos[_]+cur_dir[_] for _ in range(len(cur_pos))])

def get_rectangular_board(in_word):
    """
    returns a board of the type from the puzzle
    """
    board_data = {}
    for (char_num, cur_char) in enumerate(in_word):
        if char_num == 0:
            board_data[(0, 0)] = cur_char
        else:
            cur_pos = (char_num, char_num)
            for cur_dir in ['S', 'W', 'N', 'E']:
                for _ in range(2*char_num):
                    board_data[cur_pos] = cur_char
                    cur_pos = make_move(cur_pos, cur_dir)
    return board_data

def gen_all_paths(in_board, start_pos, dir_list, target_word):
    """
    generates all paths, which result in a target_word
    """
    def inner(cur_word, cur_path, cur_pos):
        if cur_word == target_word:
            yield cur_path
        elif len(cur_word) < len(target_word) and cur_word == target_word[:len(cur_word)]:
            for cur_dir in dir_list:
                tmp_pos = make_move(cur_pos, cur_dir)
                if tmp_pos in in_board:
                    tmp_word = cur_word+in_board[tmp_pos]
                    tmp_path = cur_path+cur_dir
                    for _ in inner(tmp_word, tmp_path, tmp_pos):
                        yield _
    for _ in inner(in_board[(start_pos)], '', start_pos):
        yield _

TARGET_WORD = 'RADAR'
CUR_BOARD = get_rectangular_board(TARGET_WORD)
ALL_DIR_LIST = ['N', 'S', 'E', 'W', 'NE', 'SE', 'SW', 'NW']
RES = sum((1 for _ in gen_all_paths(CUR_BOARD, (0, 0), ALL_DIR_LIST, TARGET_WORD)))
print(RES)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...