Solve 2018 day 5 and 6. Nice.
This commit is contained in:
46
2018/d5.py
Normal file
46
2018/d5.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
from lib import LETTERS_LOWER
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
data = data.strip()
|
||||||
|
i = 0
|
||||||
|
while i < (len(data) - 1):
|
||||||
|
a, b = data[i], data[i + 1]
|
||||||
|
if (a.upper() == b.upper()) and (a != b):
|
||||||
|
data = data[:i] + data[i + 2 :]
|
||||||
|
i = max(0, i - 1)
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
print(len(data))
|
||||||
|
|
||||||
|
|
||||||
|
def part_2(data):
|
||||||
|
data_orig = data.strip()
|
||||||
|
min_len = float("inf")
|
||||||
|
for c in LETTERS_LOWER:
|
||||||
|
data = str(data_orig)
|
||||||
|
data = data.replace(c, "")
|
||||||
|
data = data.replace(c.upper(), "")
|
||||||
|
i = 0
|
||||||
|
while i < (len(data) - 1):
|
||||||
|
a, b = data[i], data[i + 1]
|
||||||
|
if (a.upper() == b.upper()) and (a != b):
|
||||||
|
data = data[:i] + data[i + 2 :]
|
||||||
|
i = max(0, i - 1)
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
min_len = min(min_len, len(data))
|
||||||
|
print(min_len)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
input_file = __file__.replace(".py", ".txt")
|
||||||
|
with open(input_file) as f:
|
||||||
|
data = f.read()
|
||||||
|
part_1(data)
|
||||||
|
part_2(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
66
2018/d6.py
Normal file
66
2018/d6.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
from lib import str_to_ints
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
counts = {}
|
||||||
|
x_min, x_max, y_min, y_max = 10**9, 0, 10**9, 0
|
||||||
|
for line in data.splitlines():
|
||||||
|
x, y = str_to_ints(line)
|
||||||
|
x_min = min(x_min, x)
|
||||||
|
x_max = max(x_max, x)
|
||||||
|
y_min = min(y_min, y)
|
||||||
|
y_max = max(y_max, y)
|
||||||
|
counts[(x, y)] = 0
|
||||||
|
|
||||||
|
infs = set()
|
||||||
|
for x in range(x_min, x_max + 1):
|
||||||
|
for y in range(y_min, y_max + 1):
|
||||||
|
if (x, y) in counts:
|
||||||
|
counts[(x, y)] += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
min_dist = 10**9
|
||||||
|
min_fields = []
|
||||||
|
for (nx, ny) in counts.keys():
|
||||||
|
d = abs(nx - x) + abs(ny - y)
|
||||||
|
if d == min_dist:
|
||||||
|
min_fields.append((nx, ny))
|
||||||
|
elif d < min_dist:
|
||||||
|
min_dist = d
|
||||||
|
min_fields = [(nx, ny)]
|
||||||
|
|
||||||
|
if len(min_fields) == 1:
|
||||||
|
(nx, ny), = min_fields
|
||||||
|
if x == x_min or y == y_min or x == x_max or y == y_max:
|
||||||
|
infs.add((nx, ny))
|
||||||
|
else:
|
||||||
|
counts[(nx, ny)] += 1
|
||||||
|
for c in infs:
|
||||||
|
del counts[c]
|
||||||
|
print(max(list(counts.values())))
|
||||||
|
|
||||||
|
|
||||||
|
def part_2(data):
|
||||||
|
coords = []
|
||||||
|
for line in data.splitlines():
|
||||||
|
coords.append(str_to_ints(line))
|
||||||
|
|
||||||
|
r = 0
|
||||||
|
for x in range(-500, 500):
|
||||||
|
for y in range(-500, 500):
|
||||||
|
d = sum([abs(nx - x) + abs(ny - y) for (nx, ny) in coords])
|
||||||
|
if d < 10_000:
|
||||||
|
r += 1
|
||||||
|
print(r)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
input_file = __file__.replace(".py", ".txt")
|
||||||
|
with open(input_file) as f:
|
||||||
|
data = f.read()
|
||||||
|
part_1(data)
|
||||||
|
part_2(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
21
2018/d7.py
Normal file
21
2018/d7.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from lib import str_to_ints
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
print(data)
|
||||||
|
|
||||||
|
|
||||||
|
def part_2(data):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
input_file = __file__.replace(".py", ".txt")
|
||||||
|
with open(input_file) as f:
|
||||||
|
data = f.read()
|
||||||
|
part_1(data)
|
||||||
|
part_2(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user