Advent of Code

Advent of Code 2022, Day 1

Another year, another Advent of Code. Though I kind of skipped last year. I think I may have been too busy or something. I do enjoy doing these puzzles, though they aren’t really “code projects”.  Each day is just a puzzle, that is solved with math.  Most of the problem, is figuring out what exactly is being asked.  The other part is just, figuring out how to format the data set you are given.  A lot of these are kind of repetitive, open your data file, read it in, convert it to a useful format, loop through the data, performing some action, and output some sort of total.

For this year’s theme, Santa’s Elves are taking a journey to a magical grove to collect food for the reindeer.

Day 1 – Elf Food Calories

The Problem Part 1:

The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves’ expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food – in particular, the number of Calories each Elf is carrying (your puzzle input).

The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they’ve brought with them, one item per line. Each Elf separates their own inventory from the previous Elf’s inventory (if any) by a blank line.

In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they’d like to know how many Calories are being carried by the Elf carrying the most Calories.

The Problem Part 2

By the time you calculate the answer to the Elves’ question, they’ve already realized that the Elf carrying the most Calories of food might eventually run out of snacks.

To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.

So, each day consists of a Part 1 and a Part 2.  Generally speaking, each of these two parts is semi related, and can be completed with most of the same code. This one is simple enough, split the list up by lines, then split it into each elf’s calories, sort the list so the highest valies are at the front and print those three values out.  Part 1 only needs the highest, Part 2 needs the next two.

with open("Day01Input.txt") as file:
    calories = file.read()

calories = calories.replace("\n"," ")
elves = [each.split(" ") for each in calories.split("  ")]
totals = []

for each in elves:
    total = 0
    for i in each:
        total += int(i)
    totals.append(total)
 
totals.sort(reverse=True)
print(totals[0]+totals[1]+totals[2])    

Advent of Code 2020 – Wrap Up

Well, my desire to complete this totally fell apart about halfway through. I managed to get around 25 out of 50 stars total for the 25 days of tasks. I two started 11 of the 25 days, and one stared 3 more.

There are a myriad of reasons for failing to finish all the tasks. For starters, I started to have a few one star days that I would “get back to”. Most of these I have a good idea of how to solve them, but there are little problems somewhere that are stopping me from solving them.

I also just got busy/bored of it. After Day 17, I just kind of got tired of it. I also started being busier with life activities like the holidays and Doctor appointments for my wife, and just didn’t have the energy to bother. I did check on what a few of the other later tasks were and have some ideas on how to go about completing them, but it just all became too tedious.

I’m not a professional programmer, so every tasks just becomes this uphill slog and int he end, they don’t run very quickly or great. Meanwhile people are posting their one line trick code work on Reddit. I know it’s not that kind of competition, but it still kind of drags my motivation down.

I may come back and finish things up, but for now, I am still pretty good with getting half the stars.

Advent of Code 2020 – Day 15

Hey look, Day 15 is another one that took a super long time to calculate, though it was one of the easiest to code. It’s a fancy memory game, for Elves who apparently have computer brains. You start with a series of numbers, then the next number you say, is the difference between when the previous number was said, the last two times.

For example if you start saying this sequence, with 0,1,2,3 , the next number is 0, because 3 has only been said once, after that it’s 4, because 0 was said as the 5th number and the first number (5-1). After that it’s 0 again, because 4 has not been said before, then it’s 2, because 0 is at 7 and 5 (7-5), following that is 5, because 2 is at position 8 and 5 (8-5), and so on.

Both parts were the same problem, just with a different number of iterations, 2020 and 30,000,000. Calculating the 2020th number was quick, calculating the 30 millionth number, not so much.

Anyway, here is my code:


values = [0,8,15,2,12,1,4]

current = 0


while current < 30000000-7:
  check = values[-1]
  last = len(values) - values[::-1].index(check)
  if check in values[:last-1]:  
    temp=values[:last-1]
    temp.reverse()
    #print len(values)
    sec_last = len(values) - temp.index(check)-1
    next = last-sec_last
  else:
    next = 0
  values.append(next)
  #print next

  #print len(values)
  current+=1

print values[:-1]
print len(values)

I had some extra prints in there for the 2020, but printing everything for 30 million just slows it down. The final length print is just to verify that it’s giving me the correct iteration value. The main this this could use for clean up is to replace the while loop “-7” subtraction with subtracting a variable equal to the length of the initial values set, so that you could potentially feed it a value set that is larger or smaller.

The trickiest part here was figuring out the positions of the last two occurrences of a number, which meant counting backwards through the array of values. For the second number, I ended up using a reverse copy of the array, because I kept getting screwball answers trying to do it directly like I had for the first number. I would have liked to make it work with the more elegant solution but I didn’t have time to keep working on it.

Advent of Code 2020 – Day 13

Today’s lesson in extremely, incredibly, stupidly, inefficient methodology, is brought to you by, the letter “i” and the number “640856202464541”. Day 13 consisted of calculating bus departure times. Part 1 was stupidly simple. You have a list of buses, each bus makes a regular round trip to wherever, and the round trip always takes the same amount of minutes. At some point in the past, the buses all left at once.

Say the buses were 4, 5, and 6, each number also being the minutes for a round trip. Every 4 minutes, bus 4 returns, every 5 minutes bus 5 returns, every 6 minutes, bus 6 returns. If you are available to leave at 9 minutes, which bus leaves first. In this example, the first bus to return would be 5 (5*2=10)

Part 1 was easy, start at your departure time, check to see if any of the numbers divide into it evenly, if not, increment by one and repeat.

with open('day13data.txt') as f:
    lines = [line.rstrip() for line in f]

time = int(lines[0])
raw = lines[1].split(',')
busses= [] 

for i in raw:
  if i != 'x':
    busses.append(int(i))

print time
print busses

for x in busses:
  bus_time=x*(round((float(time)/float(x))+.5))
  wait=bus_time-time
  print "For bus "+str(x)+": "+str(bus_time)+" and you will wait: "+str(wait)+" Ans: "+str(x*wait)

The trickiest part is that the data contained non existent buses, labeled with an “x”. These are not a factor for part 1, so they just get stripped out.

Part 2 is where the pain in the ass was. For Part 2, you have to find a starting time where each bus, will leave, one minute apart, according to their offset. This includes the “missing” buses. So for the quick example I had above of 4,5,6, this time would occur at well, 4 minutes, because I made them sequential, but the next one would occur at 64,65,66 (16*4, 13*5, 11*6). This gets even more complicated when you add in the blanks, for example, 4,x,5,x,6 occurs at 68,70,72. As you spread the numbers apart and rearrange them to be non sequential, it complicates everything more.

I actually figured out the methodology pretty quickly, and wrote a working, good bit of code pretty quickly, my problem, was picking a starting point. I am sure there is some numerology methods (I am pretty sure you can do something using remainders), to calculate even an approximate starting point. In my standard of ugly code, I was just brute forcing it.

with open('day13data.txt') as f:
    lines = [line.rstrip() for line in f]

lines_array = lines[1].split(',')
times = []
counter=0
#First for sample Data Set day13datab.txt, second for real data
#multiplier=152600
multiplier=15630639084400
offsets=[]
busses=[]

for i in lines_array:
  if i != 'x':
    busses.append(i)
    offsets.append(lines_array.index(i))

#print busses
#print offsets

while (1):
  for i in offsets:
    times.append(((int(busses[0]))*multiplier)+i)

  #print times
  counter=1
  for j in busses[1:]:
    #print j
    if (times[counter]/float(j)).is_integer():
      counter+=1
    else:
      break
        
  #print times
  #print counter
  if counter == len(busses):
    break
  counter = 0
  times =[]
  multiplier+=1
  #print multiplier

print times

The problem is, this takes a very, very, very, very, very, long time. I left this code running for hours on my server and it didn’t finish, I honestly feel like it could very likely run for days, weeks, years, and never finish. The iteration on my multiplier that gives the correct set, for my data (every person has a different data set) was 15,630,639,084,501. 15 TRILLION.

So I fudged it a bit, because I was pretty sure my code was good, but I didn’t have an eternity to wait. So I found someone else’s code, found the correct answer, then worked out my starting offset from there. Sure enough, when I start at 15,630,6390,084,400. It quickly finds the correct answer.

If I needed this code for something important, I would certainly try to clean it up. I even started working on an iteration that would sort out my bus list to start with the largest number, instead of iterating the list every (low value) it would iterate every (high value), which in my case I believe was something like 15 versus 900. This was trickier than a straight sort though because I had a second list of offsets that I needed to also re order. I got this code working for the sample data set, but it failed in my proper data set because, while the sample data set started with the lowest bus number, the actual data set did not, and I had not calculated for that.

PS, yes, I misspelled “busses” in my code

Advent of Code 2020 – Day 12

So far, I think Day 12 has been my favorite puzzle. Not too hard, not too easy, it feels like an actually useful problem to solve instead of some arbitrary manipulation of a bunch of numbers.

This puzzle involved moving a boat based on a set of instructions. The only part that felt off was that sometimes the boat moves forward, and sometimes it just sort of, slides in one cardinal direction.

def rotator(degrees, current):
  change = degrees+current
  if change < 0:
    change = change+360
  if change >=360:
    change = change-360
  #print change
  return change  
 

with open('day12data.txt') as f:
    lines = [line.rstrip() for line in f]

xpos=0
ypos=0
#0 is East
orientation=0
cardinals = ["E","S","W","N"]

for i in lines:
  #print i
  next_orient=i[0]
  amount=int(i[1:])

  print "Ship Moves: "+next_orient+" direction "+str(amount)+" times"

  if next_orient == "L":
    orientation = rotator(-amount,orientation)
  if next_orient == "R":
    orientation = rotator(amount,orientation)
  if next_orient == "F":
    next_orient = cardinals[(orientation/90)]
  #print amount
  #print next_orient
  #print cardinals[(orientation/90)]
  if next_orient == "N":
    xpos=xpos+amount
  if next_orient == "S":
    xpos=xpos-amount
  if next_orient == "E":
    ypos=ypos+amount
  if next_orient == "W":
    ypos=ypos-amount


  print xpos
  print ypos
print abs(xpos)+abs(ypos)

This whole thing was a pretty straight forward string of conditionals, and a few functions to rotate the orientation. I am actually particularly proud of how I handled the orientation here, using an array. Your orientation is expressed in degrees, which when divided by 90 gives you a number 0, 1, 2, or 3, which become indexes in an array of cardinal directions.

Part two changed things a bit, and solved the “how does a boat facing east move north” question. Instead of moving the boat, you move a navigational buoy. The boat old moves when you get a forward command, and it moves towards the buoy.

def rotator(degrees, current):
  global xpos
  global ypos
  change = degrees+current
  if change < 0:
    change = change+360
  if change >=360:
    change = change-360
  #print change
  #return change
  if change == 90 or change == 270:
   temp=xpos
   xpos=-ypos
   ypos=temp
  if change == 180 or change == 270:
   xpos=-xpos
   ypos=-ypos
 

with open('day12data.txt') as f:
    lines = [line.rstrip() for line in f]

xpos=1
ypos=10
shipx=0
shipy=0
#0 is East
orientation=0
cardinals = ["E","S","W","N"]

for i in lines:
  #print i
  next_orient=i[0]
  amount=int(i[1:])

  if next_orient == "L":
    rotator(-amount,orientation)
  if next_orient == "R":
    rotator(amount,orientation)
  if next_orient == "N":
    xpos=xpos+amount
  if next_orient == "S":
    xpos=xpos-amount
  if next_orient == "E":
    ypos=ypos+amount
  if next_orient == "W":
    ypos=ypos-amount
  if next_orient == "F":
    shipx=shipx+(xpos)*amount
    shipy=shipy+(ypos)*amount

  print "Ship Moves: "+next_orient+" direction "+str(amount)+" times"

  print "Beacon: "+str(xpos)+"E/W "+str(ypos)+"N/S"
  print "Ship: "+str(shipx)+"E/W "+str(shipy)+"N/S"
print abs(shipx)+abs(shipy)
print shipx
print shipy

The sad part is, RIP clever Orientation gimmick, though the array is still in there for posterity. When the buoy rotates, it doesn’t face a different direction, it rotates around the ship, so the functions just became swapped coordinates instead of degrees corresponding to an orientation.