Update readme and lib and start 2015.

This commit is contained in:
felixm 2024-01-27 22:56:28 -05:00
parent b88d839bc1
commit d32bd4c04b
9 changed files with 181 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
# ---> Python
# Byte-compiled / optimized / DLL files
*.txt
__pycache__/
*.py[cod]
*$py.class

22
2015/d1.py Normal file
View File

@ -0,0 +1,22 @@
from lib import *
data = open(0).read()
part_2 = True
floor = 0
for i, c in enumerate(data):
if c == "(":
floor += 1
elif c == ")":
floor -= 1
else:
print(c)
assert False
if part_2 and floor == -1:
print(i + 1)
break
if not part_2:
print(floor)

19
2015/d2.py Normal file
View File

@ -0,0 +1,19 @@
from lib import *
data = open(0).readlines()
part_1 = True
if part_1:
a = 0
for line in data:
l, w, h = list(map(int, line.split("x")))
slack = min([l * w, w * h, h * l])
a += (2*l*w + 2*w*h + 2*h*l + slack)
else:
a = 0
for line in data:
l, w, h = list(map(int, line.split("x")))
sd = min([2 * (l + w), 2 * (w + h), 2 * (h + l)])
a += sd + (l * w * h)
print(a)

41
2015/d3.py Normal file
View File

@ -0,0 +1,41 @@
import sys
from lib import *
data = open(0).read()
part_1 = False
DIRS = {
"^": (-1, 0),
">": (0, 1),
"v": (1, 0),
"<": (0, -1),
}
if part_1:
pos = (0, 0)
poss = set([pos])
for c in data:
d = DIRS[c]
pos = pos[0] + d[0], pos[1] + d[1]
poss.add(pos)
print(len(poss))
sys.exit(0)
a = (0, 0)
b = (0, 0)
poss = set([a, b])
for i, c in enumerate(data):
if i % 2 == 0:
d = DIRS[c]
a = a[0] + d[0], a[1] + d[1]
poss.add(a)
else:
d = DIRS[c]
b = b[0] + d[0], b[1] + d[1]
poss.add(b)
print(len(poss))
sys.exit(0)

22
2015/d4.py Normal file
View File

@ -0,0 +1,22 @@
from lib import *
import hashlib
part_1 = True
if part_1:
digits = 5
else:
digits = 6
data = open(0).read().strip()
for i in range(10**9):
text = data + str(i)
md5_hash = hashlib.md5(text.encode()).hexdigest()
for c in md5_hash[:digits]:
if c != "0":
break
else:
print(i)
break

53
2015/d5.py Normal file
View File

@ -0,0 +1,53 @@
from lib import *
data = open(0).read()
part_1 = True
if part_1:
res = 0
for line in data.splitlines():
vc = 0
for c in line:
if c in "aoeui":
vc += 1
if vc < 3:
continue
prev = None
for c in line:
if c == prev:
break
prev = c
else:
continue
contains = False
ss = ["ab", "cd", "pq", "xy"]
for s in ss:
if s in line:
contains = True
if contains:
continue
res += 1
print(res)
else:
res = 0
for line in data.splitlines():
pairs = {}
for i in range(0, len(line) - 1):
p = line[i:i+2]
if p in pairs and i > pairs[p] + 1:
break
if not p in pairs:
pairs[p] = i
else:
continue
for i in range(0, len(line) - 2):
if line[i] == line[i + 2]:
break
else:
continue
res += 1
print(res)

1
2015/lib.py Symbolic link
View File

@ -0,0 +1 @@
../lib.py

View File

@ -1,3 +1,12 @@
# aocpy
Help scripts for solving Advent of Code challenges in Python.
Solutions and accompanying utility scripts for Advent of Code challenges
written in Python.
# 2015
- Day 1: 3:10
- Day 2: 5:24 :/
- Day 3: 7:26 ... :/
- Day 4: 5:11 ...
- Day 5: 14:05 o.O

17
lib.py
View File

@ -11,6 +11,9 @@ INF = float("inf")
fst = lambda l: l[0]
snd = lambda l: l[1]
def nth(n):
return lambda l: l[n]
def maps(f, xs):
if isinstance(xs, list):
return [maps(f, x) for x in xs]
@ -132,7 +135,7 @@ class Input:
def lines(self) -> list[str]:
return self.text.splitlines()
def paras(self) -> list[list[str]]:
def paras(self) -> list[str]:
return [p for p in self.text.split("\n\n")]
def grid2(self) -> Grid2D:
@ -172,11 +175,10 @@ def lcm(numbers: list[int]) -> int:
def str_to_int(line: str) -> int:
line = line.replace(" ", "")
r = re.compile(r"-?\d+")
r = re.compile(r"(-?\d+)")
m = r.findall(line)
assert len(m) == 0, "str_to_int no int"
assert len(m) > 1, "str_to_int multiple ints"
return int(m[0])
(x,) = m
return int(x)
def str_to_ints(line: str) -> list[int]:
r = re.compile(r"-?\d+")
@ -233,3 +235,8 @@ def shoelace_area(corners):
x2, y2 = corners[(i + 1) % n]
area += (x1 * y2) - (x2 * y1)
return abs(area) / 2.0
def extract_year_and_date(scriptname) -> tuple[str, str]:
r = re.compile(r"aoc(\d\d\d\d)/d(\d+).py")
[(year, day)] = r.findall(scriptname)
return (year, day)