Do till day 5.

This commit is contained in:
2023-12-03 18:44:22 -05:00
parent b7bdd63334
commit 3b1c71946d
3 changed files with 163 additions and 1 deletions

View File

@@ -3,4 +3,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: 13:08 actually decent but top 100 required 5:24
- Day 4:
- Day 4: 7:08 but top 100 required 3:33 still okay
- Day 5: 11:56 but 7:58 for top 100... getting better?

64
d4.py Normal file
View File

@@ -0,0 +1,64 @@
import re
from string import ascii_lowercase, ascii_uppercase
EXAMPLE = """
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
"""
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):
s1, s2 = line.split(',')
a, b = s1.split('-')
c, d = s2.split('-')
a = int(a)
b = int(b)
c = int(c)
d = int(d)
a = set(list(range(a, b + 1)))
b = set(list(range(c, d + 1)))
if a <= b or b <= a:
s += 1
return s
def solve2(lines: list[str]):
s = 0
for (i, line) in enumerate(lines):
s1, s2 = line.split(',')
a, b = s1.split('-')
c, d = s2.split('-')
a = int(a)
b = int(b)
c = int(c)
d = int(d)
a = set(list(range(a, b + 1)))
b = set(list(range(c, d + 1)))
c = a.intersection(b)
if c:
s += 1
# 7:08
return s
def main():
example = clean(EXAMPLE)
print("Example 1:", solve(example))
data = clean(open("i4.txt").read())
print("Solution 1:", solve(data))
example = clean(EXAMPLE)
print("Example 2:", solve2(example))
data = clean(open("i4.txt").read())
print("Solution 2:", solve2(data))
if __name__ == "__main__":
main()

97
d5.py Normal file
View File

@@ -0,0 +1,97 @@
import re
from string import ascii_lowercase, ascii_uppercase
EXAMPLE = """
[D]
[N] [C]
[Z] [M] [P]
1 2 3
1 5 9
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
"""
def clean(text: str) -> list[str]:
return list(filter(lambda l: l.strip() != "", text.splitlines()))
def solve(lines: list[str]):
s = 0
stacks = [[] for _ in range(10)]
for (_, line) in enumerate(lines):
if line.startswith("move"):
continue
for (j, c) in enumerate(line):
if c in ascii_uppercase:
i = (j - 1) // 4
stacks[i].append(c)
for stack in stacks:
stack.reverse()
r = re.compile(r"move (\d+) from (\d+) to (\d+)")
for line in lines:
if not line.startswith("move"):
continue
if (m := r.match(line)):
amount, fr, to = m.groups()
amount = int(amount)
fr = int(fr) - 1
to = int(to) - 1
stacks[to] += reversed(stacks[fr][-amount:])
stacks[fr] = stacks[fr][:-amount]
m = ""
for s in stacks:
if s:
m += s[-1]
# 10:34
return m
def solve2(lines: list[str]):
s = 0
stacks = [[] for _ in range(10)]
for (_, line) in enumerate(lines):
if line.startswith("move"):
continue
for (j, c) in enumerate(line):
if c in ascii_uppercase:
i = (j - 1) // 4
stacks[i].append(c)
for stack in stacks:
stack.reverse()
r = re.compile(r"move (\d+) from (\d+) to (\d+)")
for line in lines:
if not line.startswith("move"):
continue
if (m := r.match(line)):
amount, fr, to = m.groups()
amount = int(amount)
fr = int(fr) - 1
to = int(to) - 1
stacks[to] += stacks[fr][-amount:]
stacks[fr] = stacks[fr][:-amount]
m = ""
for s in stacks:
if s:
m += s[-1]
# 11:56
return m
def main():
example = clean(EXAMPLE)
print("Example 1:", solve(example))
data = clean(open("i5.txt").read())
print("Solution 1:", solve(data))
example = clean(EXAMPLE)
print("Example 2:", solve2(example))
data = clean(open("i5.txt").read())
print("Solution 2:", solve2(data))
if __name__ == "__main__":
main()