Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.

Or are they?

The Historians can’t agree on which group made the mistakes or how to read most of the Chief’s handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren’t location IDs at all but rather misinterpreted handwriting.

This time, you’ll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list.

Here are the same example lists again:

3   4
4   3
2   5
1   3
3   9
3   3

For these example lists, here is the process of finding the similarity score:

So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9).

Once again consider your left and right lists. What is their similarity score?

Assignment

Write a bash shell script similarity that takes the pathname of a text file containing two list of location IDs side by side. The shell script must output the similarity score between the left list and the right list to stdout. For example, if we take this text file (data.txt1)

3   4
4   3
2   5
1   3
3   9
3   3

the similarity score between the left list and the right list is \(9 + 4 + 0 + 0 + 9 + 9 = 31\).

Example

In this interactive session, we assume that the text file data.txt2 is in the current directory.

$ similarity data.txt
31

Advent of Code 2024

This assignment is based on Eric Wastl’s Advent of Code 20243.