this post was submitted on 01 Dec 2023
18 points (100.0% liked)

NotAwfulTech

386 readers
8 users here now

a community for posting cool tech news you don’t want to sneer at

non-awfulness of tech is not required or else we wouldn’t have any posts

founded 1 year ago
MODERATORS
 

Rules: no spoilers.

The other rules are made up as we go along.

Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 3 points 1 year ago* (last edited 1 year ago) (1 children)

okay yeah so

p1 part 1 submitted, runs fine. part 2 says "wrong answer for you but right for someone else". doing a debug-print-everything run on the intermediate stages of the data after my regex all seems correct, but it's also 23h50 and it's been a looooong week. so I guess I'll take a look a fresh look at that one in the morning over coffee

part1, badly:

spoiler

import re

pattern = '\d'
cmp = re.compile(pattern)

# extremely lazy, of the ungood kind
datapath = 'data'
lines = open(datapath, 'r').read().split('\n')
candidates = []
values = []
for l in lines:
    if l != '':
        r = cmp.findall(l)
        candidates.append(r)
        values.append(int(''.join([r[0], r[-1]])))

print(candidates)
print(values)
print(sum(values))

(I really wasn't kidding about the badly)

part2:

spoilermissed the eightwo case

changes:

mapping = {...} # name/int pairs
pattern = f'(?=(\d|{"|".join(mapping.keys())}))'
lines = open(datapath, 'r').read().split('\n').remove('')
values = []
for l in lines:
    r = cmp.findall(l)
    equivs = [str(mapping.get(x, x)) for x in r]
    head, tail = [equivs[0], equivs[-1]]
    values.append(int(f"{head}{tail}"))
print(sum(values))