Search code examples
pythonmathsum

advent of code day 1 2023 problem my own way


I have been trying to figure out advent of code 2023 day 1 for a while and im REALLY close, i just can't seem to get the sum of the numbers on my list. for reference, 'ref' is the file where the input is stored and 'adding' is where numbers are turned into a list, which is kind of an unnecessary step come to think of it since im doing it at the bottom.

speaking of which, the bottom is what im trying to figure out. when i print 'total' i just get "1" and nothing else, not sure what im doing wrong. i am very aware there are more conventional ways to do this but i wanted to figure it out myself without looking up a guide and i know im really close, so if anyone can help me figure out why the list at the end isnt adding together correctly with sum() that would be greatly appreciated. thanks!

with open('ref', 'r') as f:
    for line in f.readlines():
        digits = [char for char in line.strip() if char.isdigit()]
        first = digits[int()]
        factor = 10
        result = []
        for ele in first:
            result.append(ele * factor)
        length = len(digits)
        last = digits[length - 1]
        endnum = first + last
        if length == 1:
            endnum = (first)
        else:
            pass
        with open('adding', 'a') as f:
            f.writelines(str(endnum + ','))

file = open('adding', 'r')
x = []
total = 0
for i in file:
    x.append(i)
    y = sum([(i in x) for i in x])
    total += int(y)
    print(total)

Day 1 problem description: https://adventofcode.com/2023/day/1

The problem statement (irrelevant wording omitted):

The file consists of lines of text; on each line, the value can be found by combining the first digit and the last digit to form a single two-digit number. For example:

1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

In this example, the values of these four lines are 12, 38, 15, and 77. Adding these together produces 142. Consider your entire file. What is the sum of all of the values?


Solution

  • The issue

    The problem is that you do not add any line breaks to the adding file. I.e. the contents of the file looks something like:

    45,12,34,
    

    Therefore, when you do something like:

    for i in file:
        x.append(i)
    

    (Note that for i in file reads the file one line at a time.)

    After the first iteration, the entire file has been read and x will contain a list which looks like:

    ['45,12,34,']
    

    Now in that same loop, when you do:

    y = sum([(i in x) for i in x])
    total += int(y)
    

    You're actually just doing:

    y = sum([True]) # == 1
    total += int(y)
    

    because (i in x) returns True, and since there is only one element in the list, you end up with a list of a single True.

    Hence why your total is always 1.

    Solution

    The simplest solution I can suggest is to split the first element of your list x, by ,, convert to int, and add up all the numbers:

    total = sum(map(int, x[0].strip(',').split(',')))
    

    To break it down:

    • x[0].strip(',') will remove the trailing comma from the string
    • x[0].strip(',').split(',') will split the rest of the strings to create a list of numeric strings (isnumeric())
    • map(int, ...) converts each string to an int
    • sum(...) returns the sum of all the ints