I solved this problem using 19 lines and an initial dictionary with 23 entries. Is there an easier way to solve this?