Do day 3.

This commit is contained in:
2023-12-03 18:20:02 -05:00
parent 006a0d070a
commit b7bdd63334
2 changed files with 36 additions and 10 deletions

View File

@@ -2,4 +2,5 @@
- Day 1: 7:52 ... so slow brah :/ top 100 required 2:05...
- Day 2: 22:30 ... I mistyped the first and second was just bad top 100 would have been 6:16 (doable?)
- Day 3:
- Day 3: 13:08 actually decent but top 100 required 5:24
- Day 4:

43
d3.py
View File

@@ -1,40 +1,65 @@
import re
from string import ascii_lowercase, ascii_uppercase
EXAMPLE = """
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
"""
def let_to_score(cin):
d = {}
for i, c in enumerate(ascii_lowercase):
d[c] = i + 1
for i, c in enumerate(ascii_uppercase):
d[c] = i + 27
return d[cin]
def clean(text: str) -> list[str]:
return list(filter(lambda l: l.strip() != "", text.splitlines()))
def solve(lines: list[str]):
s = 0
for (i, line) in enumerate(lines):
print(i, line)
half = len(line) // 2
a, b = line[:half], line[half:]
a = set(a)
b = set(b)
z = a.intersection(b)
l = list(z)[0]
score = let_to_score(l)
s += score
# 8:42
return s
def get_score(l):
s = 0
s = set(l[0]).intersection(set(l[1])).intersection(set(l[2]))
s = list(s)[0]
return let_to_score(s)
def solve2(lines: list[str]):
s = 0
for (i, line) in enumerate(lines):
print(i, line)
for i in range(0, len(lines), 3):
s += get_score(lines[i:i+3])
return s
def main():
example = clean(EXAMPLE)
print("Example 1:", solve(example))
return
data = clean(open("i3.txt").read())
print("Solution 1:", solve(data))
return
example = clean(EXAMPLE)
print("Example 2:", solve2(example))
return
data = clean(open("i3.txt").read())
print("Solution 2:", solve2(data))
return
# 13:08
if __name__ == "__main__":
main()