And so we begin.
I was up at 5am, ready and raring to go, as ever. The Apprentice, not so much.
Unsurprisingly, for a Day 1 puzzle, I rushed off a solution in next to no time, collecting key/value pairs for each of the counts, and then sorted them.
fn solve(data: &[i64]) -> (i64, i64) { let mut counter: HashMap<i64, usize> = HashMap::new(); data.iter().for_each(|e| *counter.entry(*e).or_default() +=1); let mut sorted = counter.iter().collect::<Vec<_>>(); sorted.sort_unstable_by(|(_,a), (_,b)| b.cmp(a)); let part1: i64 = sorted.iter().take(3).fold(1, |acc, (n,_)| acc * *n); let part2: i64 = sorted.iter().rev().take(3).fold(1, |acc, (n,_)| acc * *n); (part1, part2)}
Then I uploaded it to Reddit and disappeared back to bed for an hour. The Apprentice, as is usual for people of their age, instead slept through the excitement and dragged themselves out of bed just in time to get to school.
In the evening, we sat down after dinner, and worked through the Apprentice’s solution. They made quick work of reading in the data, and then thought through the solution using a paper and pen, deciding to use a Dictionary to store the counts of each component’s ID number.
Completely on their own account, they settled on a completely different solution to mine – find the biggest entry in the dictionary, record its key and then delete the entry, repeating three times.
for item in data: item = item.strip('\n') item = int(item) if item not in data_list: data_list[item] = 1 else: data_list[item] += 1top_keys = []for i in range(3): highest_value = 0 most_common_key = 0 for key, value in data_list.items(): if value > highest_value: highest_value = value most_common_key = key top_keys.append(most_common_key) del data_list[most_common_key]print(f"Part 1: {top_keys[0]*top_keys[1]*top_keys[2]}")bottom_keys = []for i in range(3): lowest_value = 10000000000000000 least_common_key = 0 for key, value in data_list.items(): if value < lowest_value: lowest_value = value least_common_key = key bottom_keys.append(least_common_key) del data_list[least_common_key]print(f"Part 2: {bottom_keys[0]*bottom_keys[1]*bottom_keys[2]}")
Mission Accomplished! There might be a bunch of optimisation that we can do here, but for now, let’s get this train on the rails…

Leave a Reply