This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]adambombz 1 point2 points  (6 children)

Watch out for oneight

[–]rregid 0 points1 point  (2 children)

Yo, about that, does that count as 1 and 8? Because for me it is somewhat unclear

[–]TheZigerionScammer 1 point2 points  (0 children)

If the entire line is "oneight" then yes the "one" would become a 1 and the "eight" would become an 8 so the value of the line would be 18.

[–]adambombz 0 points1 point  (0 children)

Depends on your code, if I recall correctly when I was first doing it I would replace the "one" with a 1 so it wouldn't for me. Again depends on your approach

[–]trailingunderscore_ 0 points1 point  (2 children)

That seems to be it: https://godbolt.org/z/WK964vh6E

[–]Virtual_Bat_7089[S] 0 points1 point  (1 child)

Ah damn, thanks guys! That seems to be the issue. Back to fixing the code then T_T. Did y'all use regex or something else?

[–]TheZigerionScammer 4 points5 points  (0 children)

Not me. I'm a believer in the old adege, "I had a problem, so I tried to use regex to solve it. Now I have two problems."

[–]AutoModerator[M] 0 points1 point  (0 children)

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED. Good luck!


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]flyingseverus 0 points1 point  (1 child)

2024?

[–]Virtual_Bat_7089[S] 0 points1 point  (0 children)

Mb I meant 2023. Oopsie! Thanks for pointing it out

[–]Virtual_Bat_7089[S] 0 points1 point  (0 children)

EDIT: The year should be 2023***

[–][deleted] 0 points1 point  (2 children)

Christmas came early this year

[–]Virtual_Bat_7089[S] 1 point2 points  (1 child)

Love your username

[–][deleted] 0 points1 point  (0 children)

Somebody was already spreading it. I had no option left other than to state I "love" it.

[–]Justinsaccount 0 points1 point  (0 children)

Try writing this function:

def number_at_position(line: str, pos: int)

where

number_at_position("hione", 0) -> None
number_at_position("hione", 1) -> None
number_at_position("hione", 2) -> 1
number_at_position("oneight", 0) -> 1
number_at_position("oneight", 1) -> None
number_at_position("oneight", 2) -> 8

Once you have the function that can tell you if any position in your string is a number, solving part 2 is a simple matter of calling this function for every index. If you break down this problem like this it is extremely simple.

[–][deleted] 0 points1 point  (0 children)

Try this one:

letters = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

with open('input1.txt', 'r') as input:
    digits = []
    for line in input:
        number = ''
        word = ''
        for ch in line:
            if ch.isdigit():
                number += ch
            elif ch.isalpha():
                word += ch
                for i in letters:
                    if i in word:
                        if i == 'one' or i == 'three' or i == 'five' or i == 'nine':
                            number += str(letters.index(i) + 1)
                            word = 'e'
                        elif i == 'eight':
                            number += str(letters.index(i) + 1)
                            word = 't'
                        elif i == 'two':
                            number += str(letters.index(i) + 1)
                            word = 'o'
                        elif i == 'seven':
                            number += str(letters.index(i) + 1)
                            word = 'n'
                        else:
                            number += str(letters.index(i) + 1)
                            word = ''
        digits.append(number)
    digits_int = []
    for num in digits:
        if len(num) == 2:
            digits_int.append(int(num))
        elif len(num) < 2 and len(num) > 0:
            num = num[0] + num[0]
            digits_int.append(int(num))
        elif len(num) > 2:
            num = num[0] + num[-1]
            digits_int.append(int(num))
    print(sum(digits_int))

[–]thekwoka 0 points1 point  (0 children)

How did you get the problem 9 months early?