Sunday, December 1, 2024

Advent of Code: Day 1

advent-of-code
Stephen Keane
Stephen Keane@skeane.io

Advent of Code: Day 1

Day 1: Historian Hysteria

This year's AOC begins with a search for the Missing Historian:
Check out Historian Hysteria for the day1 puzzle.

Part 1

The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.

In order to solve for part 1, we need to go through the missing chief's belonging and decode one of the papers in his room (our puzzle input). The input consists of two columns. We need to sort the two columns and get the difference for each pair. The solution will be the sum of the two columns.

I started by making two .txt files one short file with the example puzzle input and another file to hold the actual input. I do that so that I can initally run my logic against the test input and then, once I'm happy with that solution, apply the code to the actual input.

inputPath = "./input.txt"
testPath = "./testInput.txt"
chosenPath = testPath

I grab the file and loop through each line to make a sorted array of numbers for each column.

def getColumn(arrayType, path):
  def splitInt (arrayType):
      if arrayType == 'left':
          return 0
      else: 
          return 1
  file = open(path, "r")
  result =  []
  for line in file.readlines():
      number = int(line.split()[splitInt(arrayType)])
      result.append(number)
  return result

# sort both arrays
rightArr = sorted(getColumn('right', chosenPath))
leftArr = sorted(getColumn('left', chosenPath))

I then create a function to get the difference between the two arrays and sum the result.

#fn to get the difference between the two arrays
def getDifference(leftArr, rightArr):
  result = []
  for i in range(len(leftArr)):
      result.append((rightArr[i] - leftArr[i]))
  return result

this worked for the test input but when I ran it against the actual input, I got a bad result. I updated the code to check whether the left or right number was greater before getting the difference.

#fn to get the difference between the two arrays
def getDifference(leftArr, rightArr):
  result = []
  for i in range(len(leftArr)):
      if leftArr[i] > rightArr[i]:
          result.append(leftArr[i] - rightArr[i])
      else:
          result.append(rightArr[i] - leftArr[i])
  return result

print (sum(getDifference(leftArr, rightArr)))

I later realized, that I could get rid of the if-else statement by using the built-in abs() function. to get the absolute value of the difference.

#fn to get the difference between the two arrays
def getDifference(leftArr, rightArr):
  result = []
  for i in range(len(leftArr)):
      result.append(abs(leftArr[i] - rightArr[i]))
  return result

print (sum(getDifference(leftArr, rightArr)))

With that change in place, I had solved for part 1!

inputPath = "./input.txt"
testPath = "./testInput.txt"
chosenPath = inputPath

#fn to get the column of numbers from the file
def getColumn(arrayType, path):
  def splitInt (arrayType):
      if arrayType == 'left':
          return 0
      else: 
          return 1
  file = open(path, "r")
  result =  []
  for line in file.readlines():
      number = int(line.split()[splitInt(arrayType)])
      result.append(number)
  return result

# sort both arrays
rightArr = sorted(getColumn('right', chosenPath))
leftArr = sorted(getColumn('left', chosenPath))

#fn to get the difference between the two arrays
def getDifference(leftArr, rightArr):
  result = []
  for i in range(len(leftArr)):
      result.append(abs(leftArr[i] - rightArr[i]))
  return result
  return result

print (sum(getDifference(leftArr, rightArr)))

Part 2

in typical, Advent Of Code fashion, we realize that we didn't do things exactly right in part 1 and need to make a correction: instead of looking for differences, we'll need to find similarities.

part 2 tells us...

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.

I can use a lot of my solution from part one with no changes. I just need to remove the sorted() function on the left and right arrays and add a function to get the similarities.

This is done by counting the number of times each number in the left array appears in the right array and multiplying that number by the number in the left array.

#fn to get similarities between the two arrays
def getSimilarities(leftArr, rightArr):
  result = []
  for i in range(len(leftArr)):
      count = rightArr.count(leftArr[i])
      result.append(leftArr[i] * count)
  return result

I then get the sum of the similarities and part 2 is solved!

inputPath = "./input.txt"
testPath = "./testInput.txt"
chosenPath = inputPath

#fn to get the column of numbers from the file
def getColumn(arrayType, path):
  def splitInt (arrayType):
      if arrayType == 'left':
          return 0
      else: 
          return 1
  file = open(path, "r")
  result =  []
  for line in file.readlines():
      number = int(line.split()[splitInt(arrayType)])
      result.append(number)
  return result

rightArr = getColumn('right', chosenPath)
leftArr = getColumn('left', chosenPath)

#fn to get similarities between the two arrays
def getSimilarities(leftArr, rightArr):
  result = []
  for i in range(len(leftArr)):
      count = rightArr.count(leftArr[i])
      result.append(leftArr[i] * count)
  return result

print(sum(getSimilarities(leftArr, rightArr)))