Spaces:
Running
Running
File size: 5,067 Bytes
a4da721 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import time
def load_data(file):
with open(file) as f:
data = f.readlines()
patterns = data[0].strip("\n").split(",")
patterns = [p.strip(" ") for p in patterns]
designs = []
for line in data[2:]:
designs.append(line.strip("\n"))
return patterns, designs
def design_is_possible(design, patterns, checked = None, parents = None):
if checked is None:
checked = set()
if design in patterns:
return True
to_check = []
for p in patterns:
if design.startswith(p):
remainder = design[len(p):]
# print(p, remainder)
to_check.append(remainder)
# if design_is_possible(remainder, patterns, checked):
# print(p, remainder)
# to_check.append(remainder)
for rem in to_check:
if rem in checked:
continue
checked.add(rem)
if design_is_possible(rem, patterns, checked):
return True
return False
### Below, a cemetery of different ideas and tries...
# Sort patterns from largest to smallest
# patterns.sort(key=lambda x: len(x))
# patterns = patterns[::-1]
# def design_is_possible(design, patterns):
# # For each large pattern, check if it contains it, remove it, then check if still possible
# if design in patterns or design == "":
# return True
# # all_possibles = []
# for p in patterns:
# if p in design:
# r = re.search(p, design)
# l, r = r.span()
# left_design = design[:l]
# right_design = design[r:]
# print(p, left_design, right_design)
# return design_is_possible(left_design, patterns) and design_is_possible(right_design, patterns)
#
# return False
# def design_is_possible(design, patterns):
# # For each large pattern, check if it contains it, remove it, then check if still possible
# if design in patterns or design == "":
# return True
# all_possibles = []
# for p in patterns:
# if p in design:
# r = re.search(p, design)
# l, r = r.span()
# left_design = design[:l]
# right_design = design[r:]
# print(p, left_design, right_design)
# all_possibles.append(design_is_possible(left_design, patterns) and design_is_possible(right_design, patterns))
#
# if any(all_possibles):
# return True
#
#
# return False
# def design_is_possible(design, patterns):
# # For each large pattern, check if it contains it, remove it, then check if still possible
# if design in patterns:
# return True
# all_possibles = []
# for p in patterns:
# if p in design:
# r = re.search(p, design)
# l, r = r.span()
# new_design = design[:l] + design[r:]
# print(p, new_design)
# all_possibles.append(design_is_possible(new_design, patterns))
# if any(all_possibles):
# print(all_possibles)
# return True
#
# print(all_possibles)
# return any(all_possibles)
# return False
# def design_is_possible(design, patterns):
# # For each large pattern, check if it contains it, remove it, then check if still possible
# if design in patterns:
# return True
# # all_possibles = []
# for p in patterns:
# if p in design:
# r = re.search(p, design)
# l, r = r.span()
# design = design[:l] + design[r:]
# return design_is_possible(design, patterns)
#
# return False
# file = "test.txt"
# file = "test2.txt"
file = "input.txt"
# file = "test3.txt"
patterns, designs = load_data(file)
total = 0
for design in designs:
candidates = [p for p in patterns if p in design]
# print(candidates)
# print(design)
is_possible = design_is_possible(design, candidates)
# print()
# print()
if is_possible:
total += 1
# else:
# print(design, is_possible)
print(total)
## Part 2
def count_possibilities(design, patterns):
if design in patterns:
return 1
to_check = []
for p in patterns:
if design.startswith(p):
remainder = design[len(p):]
# print(p, remainder)
to_check.append(remainder)
totals = []
for rem in to_check:
totals.append(count_possibilities(rem, patterns))
return sum(totals)
# file = "input.txt"
# # file = "test.txt"
# patterns, designs = load_data(file)
# #
# #
# #
# total = 0
# for design in designs:
# print(design)
# candidates = [p for p in patterns if p in design]
# if design_is_possible(design, candidates):
# count = count_possibilities(design, candidates)
# else:
# count = 0
# total += count
# print(design, count)
#
#
# # else:
# # print(design, is_possible)
#
# print(total)
|