This is a very short post in my collection working through this year’s Advent of Code challenges. Unlike the previous ones, this has no mathematical comments, as it was a very short exercise. This notebook is available in its original format on my github.
Given a list of strings, determine how many strings have no duplicate words.
This is a classic problem, and it’s particularly easy to solve this in python. Some might use
collections.Counter, but I think it’s more straightforward to use sets.
The key idea is that the set of words in a sentence will not include duplicates. So if taking the set of a sentence reduces its length, then there was a duplicate word.
with open("input.txt", "r") as f: lines = f.readlines() def count_lines_with_unique_words(lines): num_pass = 0 for line in lines: s = line.split() if len(s) == len(set(s)): num_pass += 1 return num_pass count_lines_with_unique_words(lines)
I think this is the first day where I would have had a shot at the leaderboard if I’d been gunning for it.
Let’s add in another constraint. Determine how many strings have no duplicate words, even after anagramming. Thus the string
is not valid, since the second word is an anagram of the first. There are many ways to tackle this as well, but I will handle anagrams by sorting the letters in each word first, and then running the bit from part 1 to identify repeated words.
with open("input.txt", "r") as f: lines = f.readlines() sorted_lines =  for line in lines: sorted_line = ' '.join([''.join(l) for l in map(sorted, line.split())]) sorted_lines.append(sorted_line) sorted_lines[:2]
['bddjjow acimrv bcjjm anr flmmos fiosv', 'bcmnoxy dfinyzz dgmp dfgioy hinrrv eeklpuu adgpw kqv']