#!/usr/bin/env python # coding: utf-8 # In[63]: def count_houses(it, santas=1): positions = [] for _ in range(santas): positions.append([0,0]) houses = set() turn = 0 for pos in positions: houses.add(tuple(pos)) for ch in it: if ch == '^': positions[turn][1] += 1 elif ch == 'v': positions[turn][1] -= 1 elif ch == '>': positions[turn][0] += 1 elif ch == '<': positions[turn][0] -= 1 else: continue houses.add(tuple(positions[turn])) turn = (turn + 1) % santas return len(houses) # In[64]: import import_ipynb import helper s = helper.read_file('2015_3.txt') # In[65]: assert count_houses('>') == 2 assert count_houses('^>v<') == 4 assert count_houses('^v^v^v^v^v') == 2 # In[66]: count_houses(s) # In[67]: assert count_houses('^v', 2) == 3 assert count_houses('^>v<', 2) == 3 assert count_houses('^v^v^v^v^v', 2) == 11 # In[68]: count_houses(s, 2)