From 14c3375905a119ce1bf93d93470f191ff39491a7 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Sat, 9 Dec 2023 23:37:58 +0100 Subject: [PATCH] Day 01 (minified) --- day01/part1.min.py | 2 ++ day01/part1.py | 7 +++---- day01/part2.py | 1 - 3 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 day01/part1.min.py diff --git a/day01/part1.min.py b/day01/part1.min.py new file mode 100644 index 0000000..8fc585b --- /dev/null +++ b/day01/part1.min.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 +import re;print(sum(int(re.search("\d",l)[0]+re.search("\d",l[::-1])[0])for l in open("input.txt"))) diff --git a/day01/part1.py b/day01/part1.py index 9f300c4..2135bd3 100644 --- a/day01/part1.py +++ b/day01/part1.py @@ -3,12 +3,11 @@ import re lines = (x.strip() for x in open("input.txt")) total = 0 -regexFirst = re.compile(f"^.*?(\\d)") -regexLast = re.compile(f"^.*(\\d)") +regex = re.compile("\d") for line in lines: - first = regexFirst.match(line).group(1) - last = regexLast.match(line).group(1) + first = regex.search(line)[0] + last = regex.search(line[::-1])[0] total += int(first + last) print(total) diff --git a/day01/part2.py b/day01/part2.py index c45630e..4ef3c8c 100644 --- a/day01/part2.py +++ b/day01/part2.py @@ -5,7 +5,6 @@ STRINGS = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine lines = (x.strip() for x in open("input.txt")) total = 0 -digitRegex = f"(\\d|{'|'.join(STRINGS)})" regexFirst = re.compile(f"^.*?(\\d|{'|'.join(STRINGS)})") regexLast = re.compile(f"^.*(\\d|{'|'.join(STRINGS)})")