8 Direction
= Enum('Direction', 'left right up down upleft upright downleft downright')
10 delta
= {Direction
.left
: (0, -1),Direction
.right
: (0, 1),
11 Direction
.up
: (-1, 0), Direction
.down
: (1, 0),
12 Direction
.upleft
: (-1, -1), Direction
.upright
: (-1, 1),
13 Direction
.downleft
: (1, -1), Direction
.downright
: (1, 1)}
20 return [['.' for c
in range(w
)] for r
in range(h
)]
23 return lcat(cat(r
) for r
in grid
)
25 def indices(grid
, r
, c
, l
, d
):
29 inds
= [(r
+ i
* dr
, c
+ i
* dc
) for i
in range(l
)]
30 return [(i
, j
) for i
, j
in inds
37 def gslice(grid
, r
, c
, l
, d
):
38 return [grid
[i
][j
] for i
, j
in indices(grid
, r
, c
, l
, d
)]
41 def set_grid(grid
, r
, c
, d
, word
):
42 for (i
, j
), l
in zip(indices(grid
, r
, c
, len(word
), d
), word
):
47 def present_many(grid
, words
):
50 wordlens
= set(len(w
) for w
in words
)
55 for wordlen
in wordlens
:
56 word
= cat(gslice(grid
, r
, c
, wordlen
, d
))
58 presences
+= [(word
, r
, c
, d
)]
63 def read_wordsearch(fn
):
64 lines
= [l
.strip() for l
in open(fn
).readlines()]
65 w
, h
= [int(s
) for s
in lines
[0].split('x')]
67 words
= set(lines
[h
+1:])
68 return w
, h
, grid
, words
76 def found_words_length(puzzle
):
77 width
, height
, grid
, words
= puzzle
78 return sum(len(p
[0]) for p
in present_many(grid
, words
))
80 def total_found_words_length(puzzles
):
81 return sum(found_words_length(p
) for p
in puzzles
)
91 def unused_vowels(puzzle
, presences
):
92 width
, height
, grid
, words
= puzzle
94 unused_grid
= [[c
for c
in r
] for r
in grid
]
95 for w
, r
, c
, d
in presences
:
96 set_grid(unused_grid
, r
, c
, d
, '.' * len(w
))
97 unused_vowel_count
= sum(1 for l
in unused_grid
for c
in l
if c
in 'aeiou')
98 return unused_vowel_count
101 puzzle
= read_wordsearch('10-wordsearch.txt')
103 width
, height
, grid
, words
= puzzle
104 presences
= present_many(grid
, words
)
105 # found_words = [p[0] for p in presences]
107 print("Part 1: Found {} words".format(len(presences
)))
108 print("Part 2: {} unused vowels".format(unused_vowels(puzzle
, presences
)))