As a concept:
- Use strings of 0s and 1s that can be interpreted as binary numbers.
- In file 1 a 12 15 would become "111100000000000" with 1s on positions 12 to 15 (backwards).
- All "a" tags in file 2 become on string in a similar fashion "...00110...0010, all "b" tags another etc
- If you combine the strings from file 1 and file 2 with binary "and", you only need to count the 1s in the result.
I hope I did properly understood your question.