Solve day 18 and 19 2017.
This commit is contained in:
50
2017/d19.py
Normal file
50
2017/d19.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from lib import Grid2D, LETTERS_UPPER, add2, sub2
|
||||
|
||||
|
||||
def part_1(data):
|
||||
g = Grid2D(data)
|
||||
g.print()
|
||||
|
||||
current = None
|
||||
for c in range(g.n_cols):
|
||||
if g[(0, c)] == "|":
|
||||
current = (0, c)
|
||||
|
||||
current_dir = Grid2D.S
|
||||
letters = ""
|
||||
done = False
|
||||
steps = 0
|
||||
|
||||
while not done:
|
||||
while g[current] != " ":
|
||||
current = add2(current, current_dir)
|
||||
steps += 1
|
||||
if g[current] in LETTERS_UPPER:
|
||||
letter = g[current]
|
||||
if letter in letters:
|
||||
done = True
|
||||
break
|
||||
letters += letter
|
||||
|
||||
if g[current] == "+":
|
||||
break
|
||||
|
||||
next_field = None
|
||||
for nb in g.neighbors_ort(current):
|
||||
if nb != sub2(current, current_dir) and (g[nb] in "-|" or g[nb] in LETTERS_UPPER):
|
||||
next_field = nb
|
||||
if next_field is None:
|
||||
done = True
|
||||
else:
|
||||
current_dir = sub2(next_field, current)
|
||||
print(letters)
|
||||
print(steps)
|
||||
|
||||
|
||||
def main():
|
||||
data = open(0).read()
|
||||
part_1(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user