Posts

Advent of Code Day 6

Dec 6, 2020 | 3 minutes to read

Tags: geeky, software

It’s Day 6 of the Advent of Code and it was fun. Part 1 was not that hard, actually. I learned about adding & removing entries from arrays (lists) with pop and append and remove. The code is only 34 lines, and that’s with my ‘log’ statements for debugging.

Part 2 shouldn’t have been that hard, but it took me a while anyway. I got lost in the array of letters I wanted to keep versus those I shouldn’t and was close on my first try but slightly off. I didn’t account for letters added to the last two lines in a multi-line set but still not in every line. So, yeah. Challenging!

But I have 12 stars now, so I’m on track still.

Here’s my code for part 1:

# Advent 2020 Day 6 part 1 - For each group, count the number of questions 
# to which anyone answered "yes". What is the sum of those counts?

def addToLog(value):
    global log
    log += value + "\n"

formcount = 0
sumOfCounts = 0
foundltrs = []
log = ''
with open("c:\\git\\advent-2020\\6.txt", "r") as f:
    formslist = f.read().splitlines() 
f.close()

for line in formslist:
    if line == "STOP":
        break
    if line == "":
        formcount += 1
        sumOfCounts += len(foundltrs)
        addToLog('sum is now ' + str(sumOfCounts))
        foundltrs = []
        continue
    for ltr in line:
        if ltr not in foundltrs:
            foundltrs.append(ltr)
            addToLog("added ltr " + ltr + ' to foundltrs -- ' + str(foundltrs))
    
addToLog('There are ' + str(formcount) + ' forms. The sum is  ' + str(sumOfCounts) + '.')
fl = open("6-2-log.txt", "w")
fl.write(log)
fl.close()
print('There are ' + str(formcount) + ' forms. The sum is  ' + str(sumOfCounts) + '.')

And here’s my code for part 2:

# Advent 2020 Day 6 part 1 - For each group, count the number of questions 
# to which everyone answered "yes". What is the sum of those counts?

def addToLog(value):
    global log
    log += value + "\n"

formcount = 0
sumOfCounts = 0
foundltrs = []
prevline = []
prevprevline = []
removedletters = []
firstline = True
log = ''
with open("c:\\git\\advent-2020\\6.txt", "r") as f:
    formslist = f.read().splitlines() 
f.close()

for line in formslist:
    if line == "STOP":
        break
    if line == "":
        formcount += 1
        sumOfCounts += len(foundltrs)
        addToLog('foundltrs was ' + str(foundltrs) + ' sum is now ' + str(sumOfCounts))
        foundltrs = []
        removedletters = []
        firstline = True
        continue

    for ltr in line:
        if ltr not in foundltrs:
            foundltrs.append(ltr)
            addToLog("added " + ltr + ' to foundltrs -- ' + str(foundltrs))
    keepletters = []
    if firstline == False:
        for lettr in foundltrs:
            addToLog('removedletters has ' + str(removedletters))
            if lettr in line and lettr in prevline and lettr not in removedletters:
                keepletters.append(lettr)
                continue
            else:
                addToLog("will remove " + lettr)
                removedletters.append(lettr)
        foundltrs = keepletters
    prevline = line
    firstline = False
    addToLog('loop')

addToLog('There are ' + str(formcount) + ' forms. The sum is  ' + str(sumOfCounts) + '.')
fl = open("6-2-log.txt", "w")
fl.write(log)
fl.close()
print('There are ' + str(formcount) + ' forms. The sum is  ' + str(sumOfCounts) + '.')

Cheers!


You can leave a comment on this post here.