Well, it’s an easy and hard-ish day.  I kind of hate the part 2s on these because they are almost always annoying.  It was a bit more interesting on the input because it was a two part input.  I’m still getting weird empty spaces at the end too, which I have just been trimming with a .pop(), but that’s a bit sloppy.  Today’s puzzle is verifying sorting of sets of numbers.

Part 1 was pretty simple, mostly because I forced myself not to “overthink it”.  I just, compared every pair set to the valid list.  Then if they were valid, added them into the total.  It feels like it’s going to miss some things, but it didn’t.  I got the correct answer in one try.

Part 2 was a pain.  It was, “take the wrong answers and fix them”.  What I did initially was just, swap the numbers if they were wrong.  This would put them one step closer to correct, afterwards, it fed them back through the check, this time flagged as bad, since the two sets need to be answered separately.  This code worked for the sample input for both parts. 

When I ran it through my actual data, this resulted in an answer of “5692”, which was incorrect.  Everything seemed correct in the code.  I even created a copy of the sorting loop with a “total3”, this time just running through bad pages.  It matched the answer of 5692.  So I started trying to guess a bit to see if I was even close I was within 100 for sure.  I went off to Reddit and pulled someone else’s code, ran my data set through it, and got the answer “5770”, which is the correct answer.

It also gave me a direction on where to look.  I tried a different sort where I just, slapped the first number at the end and sorted it back through.  It returned the same 5692.  Which would be a good sign, that both sorts were getting the same results, if the answer was correct.  On a bit of a lark, I decided to see just how off I was, 5770-5692 is 78.  In the data, on the last line, is a 78.  This was the key to solving the problem.  I uncommented some of my print statements and, sure enough, it wasn’t running the last line.

You know that .pop() I mentioned back at the start of this?  Well, it turns out that the sample data had an extra blank.  My actual data, did not, so it was just, dropping the last line of data.

Remember when I said it was a bad idea?

with open("Day05Input.txt") as file:
    data = file.read()


rules = data.split("\n\n")[0].split("\n")
pages = data.split("\n\n")[1].split("\n")
#pages.pop()
bad_pages = []

#Verify Inputs
#for each in pages:
#for each in rules:
#  print(each)

total = 0
total2 = 0
total3 = 0

for each in pages:
  these_pages = each.split(",")
  goodset = True
  running = True
#  print(these_pages)
  while running:
#    print(these_pages)
    running = False
    restart = False 
    for p1 in these_pages:
      for p2 in these_pages[these_pages.index(p1):]:
        if p1 != p2:
          pair = f"{p1}|{p2}"
          if pair in rules:
            #print("Good")
            pass
          else:
#            print(these_pages.index(p1))
            p1pos = these_pages.index(p1)
            p2pos = these_pages.index(p2)
            these_pages[p1pos] = p2
            these_pages[p2pos] = p1
#            print(these_pages.index(p1))
            #print("Bad")
            #print(pair)
            running = True
            goodset = False
 #           print("---")
 #           print(these_pages)
  if goodset:
    #print(f"Set {pages.index(each)} is good")
    total += int(these_pages[int((len(these_pages) - 1)/2)])
  else:
    bad_pages.append(these_pages)
    total2 += int(these_pages[int((len(these_pages) - 1)/2)])



for these_pages in bad_pages:
  goodset = True
  running = True
#  print(these_pages)
  while running:
#    print(these_pages)
    running = False
    restart = False 
    for p1 in these_pages:
      for p2 in these_pages[these_pages.index(p1):]:
        if p1 != p2:
          pair = f"{p1}|{p2}"
          if pair in rules:
            #print("Good")
            pass
          else:
            these_pages.append(these_pages.pop(p1))
#            print(these_pages.index(p1))
#            print(these_pages.index(p1))
            #print("Bad")
            #print(pair)
            running = True
            goodset = False
 #           print("---")
 #           print(these_pages)
  total3 += int(these_pages[int((len(these_pages) - 1)/2)])




print(total)
print(total2)
# Part 1 - 7365
#5692 Low
#5700 Low
#5800 Too High
#5750 - Wrong
#5730 - Wrong
print(total3)
## Its not working but the answer for future reference is 5770

Comments are closed.