day 17-19

This commit is contained in:
Jed
2021-12-19 01:16:01 -05:00
parent 5b9bb53880
commit 38a22f46a2
10 changed files with 1590 additions and 0 deletions

1
2021/17.input Normal file
View File

@@ -0,0 +1 @@
target area: x=230..283, y=-107..-57

32
2021/17.py Normal file
View File

@@ -0,0 +1,32 @@
import sys, re
with open('./2021/17.input') as input_file:
tgt_x_min, tgt_x_max, tgt_y_min, tgt_y_max = map(
int, re.match("^target area: x=(.+?)\.\.(.+?), y=(.+?)\.\.(.+?)$",
input_file.read().strip()).groups())
highest = 0
unique = 0
x_range = max(abs(tgt_x_min), abs(tgt_x_max))
y_range = max(abs(tgt_y_min), abs(tgt_y_max))
for vel_y_0 in range(-y_range, y_range + 1):
for vel_x_0 in range(-x_range, x_range + 1):
pos_x, pos_y = 0, 0
vel_x, vel_y = vel_x_0, vel_y_0
height = 0
while pos_x <= tgt_x_max and pos_y >= tgt_y_min:
pos_x += vel_x
pos_y += vel_y
vel_x -= (vel_x > 0) - (vel_x < 0)
vel_y -= 1
height = max(height, pos_y)
if (tgt_x_min <= pos_x <= tgt_x_max and
tgt_y_min <= pos_y <= tgt_y_max):
highest = max(highest, height)
unique += 1
break
print("Part 1:", highest)
print("Part 2:", unique)

1
2021/17.test Normal file
View File

@@ -0,0 +1 @@
target area: x=20..30, y=-10..-5

100
2021/18.input Normal file
View File

@@ -0,0 +1,100 @@
[6,[[6,3],[[1,4],[8,4]]]]
[5,[[[0,8],[1,0]],8]]
[[[6,[7,7]],[2,[6,4]]],[2,6]]
[[[[7,4],[2,7]],[4,[1,5]]],[[[0,5],5],[[2,1],[8,2]]]]
[[[[5,9],[7,2]],[0,[9,9]]],[[[5,3],[7,9]],[3,[9,1]]]]
[5,[[3,0],[[8,2],5]]]
[[2,[[0,8],7]],[4,[7,7]]]
[[[[3,4],6],[5,[4,2]]],[[9,[9,5]],2]]
[[7,[0,5]],[[1,3],[7,[4,0]]]]
[[[6,2],3],[[[2,0],9],[5,[7,2]]]]
[[4,[8,6]],8]
[[[9,8],[[7,3],[4,6]]],[5,3]]
[[[0,[8,4]],8],[[5,[4,7]],[5,9]]]
[[[[0,8],[3,7]],[5,1]],[[5,6],2]]
[[[[5,8],0],[[3,0],3]],[[6,5],[[8,0],[3,9]]]]
[[[8,[5,6]],[6,4]],[8,0]]
[7,[[5,[3,8]],3]]
[[[4,[2,0]],2],[[[8,1],[5,8]],5]]
[9,[[[6,6],[8,1]],[[7,9],9]]]
[[[6,0],[[7,2],9]],[[[7,3],[1,1]],0]]
[[[[7,8],[0,3]],[5,9]],[[2,[4,3]],7]]
[[[3,1],[3,[6,3]]],[[6,[8,9]],7]]
[[[[1,1],[0,5]],[8,1]],[0,[8,[1,4]]]]
[[[6,[8,6]],[7,8]],[[7,3],[3,[5,8]]]]
[[[2,5],[[6,8],[4,5]]],[[2,[8,2]],[2,[3,2]]]]
[[[[9,2],0],5],0]
[[7,[[3,7],[0,9]]],[6,[1,[6,9]]]]
[[[[4,5],5],5],4]
[[[6,[6,9]],[8,3]],9]
[[[[2,7],[8,6]],0],[2,[4,9]]]
[[[4,[9,8]],[7,6]],[7,[[2,7],[2,7]]]]
[[[0,8],[4,[5,9]]],[[4,[1,0]],[6,8]]]
[[[2,4],9],[[[7,9],5],[0,5]]]
[[[3,[8,6]],6],[[8,[6,7]],[[6,1],[2,1]]]]
[[[0,[0,5]],[[0,5],4]],9]
[[[[0,0],7],8],[[8,[4,6]],9]]
[[[1,[1,1]],[3,[2,5]]],[6,6]]
[[[[3,7],[6,1]],[5,4]],[[0,[2,6]],[0,1]]]
[[1,1],[3,4]]
[9,[[4,[7,8]],[3,4]]]
[[[[5,3],[5,9]],9],[[[2,4],[2,7]],[[6,3],[1,8]]]]
[[[2,[2,2]],[[8,7],9]],[[[4,6],[5,3]],[[2,6],9]]]
[[[3,8],[[5,7],7]],[[[0,9],3],1]]
[[[6,[1,9]],[2,1]],[[[7,0],[2,1]],8]]
[[[8,[9,9]],1],[[4,1],[[2,8],1]]]
[[[2,[3,7]],[[2,4],[3,5]]],[[3,[1,9]],[[1,3],[1,7]]]]
[[[[4,3],8],3],[[6,[1,7]],[[4,2],9]]]
[[[[1,9],1],[[0,7],[9,4]]],[[[7,2],[0,1]],8]]
[9,5]
[[[[6,4],4],[[3,4],0]],[[9,[7,6]],[[3,4],[7,1]]]]
[[0,2],[[[4,9],[3,4]],[2,[3,9]]]]
[[[[8,9],9],[[6,4],[2,9]]],[[4,5],[[1,8],2]]]
[[[6,[9,5]],[4,[1,0]]],[[[4,1],[3,5]],[3,3]]]
[[[7,1],[[5,4],8]],[[0,[9,4]],7]]
[[[4,[0,3]],[[0,2],8]],[[0,[9,6]],[[6,3],[3,2]]]]
[[[[5,5],8],[[4,5],3]],[3,[[0,2],0]]]
[[[[9,5],[1,0]],[[9,1],[0,9]]],[[1,[9,1]],[1,3]]]
[[9,[[5,7],8]],[[9,[9,3]],[3,[0,1]]]]
[[[5,6],[9,8]],[2,9]]
[[[9,[3,8]],[9,0]],[[8,[6,2]],1]]
[3,[4,[1,[0,4]]]]
[[9,[[8,5],[8,0]]],[[1,6],[8,4]]]
[[7,[[6,8],5]],[[9,[1,3]],[[6,5],[0,8]]]]
[[[6,0],[9,[3,5]]],[8,6]]
[[[1,[2,3]],[[5,2],4]],[1,[[7,3],2]]]
[[[2,[1,1]],3],[[8,[5,5]],[[7,5],[8,9]]]]
[[[4,0],[8,6]],[[[7,1],7],0]]
[2,6]
[[[[5,4],[9,7]],4],[0,6]]
[[4,[0,5]],[1,[[1,6],[6,2]]]]
[[[7,8],[0,6]],[0,[[2,9],[1,5]]]]
[1,[[[4,4],1],[[3,2],[2,5]]]]
[[[[9,8],[2,4]],[1,2]],[[[5,1],9],[[0,8],[5,2]]]]
[[8,[[2,6],[4,6]]],[[0,[2,9]],[[2,2],[7,2]]]]
[[[7,[8,1]],[[8,8],7]],[3,[7,[7,9]]]]
[[6,[[3,1],[3,6]]],[[[5,8],[9,8]],[2,[7,4]]]]
[[[4,[2,0]],[3,[3,3]]],[[6,[8,5]],5]]
[[[3,2],3],[[8,2],8]]
[[7,[[8,7],[5,8]]],[[2,0],[7,7]]]
[[[[3,3],1],[[5,1],4]],[[4,3],[[4,9],8]]]
[[[0,[5,8]],7],[4,9]]
[[0,[[7,7],[1,1]]],[[0,[5,0]],[4,5]]]
[[[[2,8],[1,6]],[[7,3],9]],[[2,8],[6,2]]]
[[1,[4,7]],[8,0]]
[3,[[[6,1],9],[[1,1],5]]]
[[[[3,0],[9,8]],[6,[8,3]]],3]
[4,[[1,[8,1]],[[6,0],2]]]
[[4,[4,[0,3]]],[[[7,5],[0,2]],[[9,7],[6,5]]]]
[[0,[4,[6,1]]],[[[1,9],[6,0]],9]]
[[[[0,2],[8,4]],[2,3]],[[9,[8,4]],1]]
[[[[1,2],[7,7]],[[3,8],3]],[[[1,1],[7,5]],6]]
[[[[1,8],[8,4]],[[4,0],1]],[0,[1,[9,4]]]]
[[[3,1],[9,5]],[[[9,5],4],[[8,7],4]]]
[[[6,[3,0]],0],[[[6,9],7],[[6,1],[6,6]]]]
[[[[9,6],[4,4]],5],9]
[[5,[[6,0],0]],1]
[3,[0,[4,[9,0]]]]
[[[5,[2,2]],3],5]
[[2,3],[9,[6,7]]]
[[[[6,8],[7,9]],[4,7]],[[1,2],[0,1]]]

113
2021/18.py Normal file
View File

@@ -0,0 +1,113 @@
def add(a, b):
return [a, b]
DIGITS = '0123456789'
def try_explode(num):
num_str = str(num).replace(' ', '')
depth = 0
for idx, c in enumerate(num_str):
if c == '[':
depth += 1
if depth == 5:
# Explode!
end_idx = num_str.find(']', idx)
a, b = list(map(int, num_str[idx+1:end_idx].split(',')))
left = num_str[:idx]
for lidx in range(len(left)-1, -1, -1):
c = left[lidx]
if c in DIGITS:
term = max(i for i in range(lidx)
if left[i] not in DIGITS)
val = int(left[term+1:lidx+1])
val += a
left = left[:term+1] + str(val) + left[lidx+1:]
break
mid = '0'
right = num_str[end_idx+1:]
for ridx, c in enumerate(right):
if c in DIGITS:
term = min(i for i in range(ridx+1, len(right))
if right[i] not in DIGITS)
val = int(right[ridx:term])
val += b
right = right[:ridx] + str(val) + right[term:]
break
return eval(left + mid + right)
elif c == ']':
depth -= 1
return None
def try_split(num):
if isinstance(num, list):
a, b = num
new_a = try_split(a)
if new_a is not None:
return [new_a, b]
new_b = try_split(b)
if new_b is not None:
return [a, new_b]
return None
if num > 9:
d, m = divmod(num, 2)
return [d, d + m]
return None
def reduce(num):
while True:
new_num = try_explode(num)
if new_num is not None:
num = new_num
continue
new_num = try_split(num)
if new_num is not None:
num = new_num
continue
return num
def magnitude(num):
if isinstance(num, list):
a, b = num
a = magnitude(a)
b = magnitude(b)
return 3*a + 2*b
return num
def part1(s):
lines = s.splitlines()
fake_nums = list(map(eval, lines))
num = fake_nums[0]
for item in fake_nums[1:]:
item = reduce(item)
num = reduce(add(num, item))
answer = magnitude(num)
print(f'The answer to part one is {answer}')
def part2(s):
lines = s.splitlines()
fake_nums = list(map(eval, lines))
fake_nums = list(map(reduce, fake_nums))
best = 0
for ia, a in enumerate(fake_nums):
for ib, b in enumerate(fake_nums):
if ia == ib:
continue
mag = magnitude(reduce(add(a, b)))
best = max(best, mag)
answer = best
print(f'The answer to part two is {answer}')
with open('./2021/18.input') as input_file:
INPUT = input_file.read()
part1(INPUT)
part2(INPUT)

0
2021/18.test Normal file
View File

81
2021/19-1.py Normal file
View File

@@ -0,0 +1,81 @@
import sys; sys.dont_write_bytecode = True;
from utils import *
import typing
def do_case(inp: str, sample=False):
# READ THE PROBLEM FROM TOP TO BOTTOM OK
def sprint(*a, **k): sample and print(*a, **k)
lines: typing.List[str] = inp.splitlines()
paras: typing.List[typing.List[str]] = lmap(str.splitlines, inp.split("\n\n"))
out = 0
scanners = []
for para in paras:
points = lmap(ints, para[1:])
scanners.append(points)
# print(len(points))
# assume scanner 0 is good
FACINGS = [x for i in [-1, 1] for x in [[i, 0, 0], [0, i, 0], [0, 0, i]]]
def cross(a, b):
c = [a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]]
return c
def common(a, b):
aset = set(map(tuple, a))
# return b's points, but now relative to a
for facing in FACINGS:
for up in [f for f in FACINGS if all(abs(x) != abs(y) for x, y in zip(f, facing) if x or y)]:
# facing's
right = cross(facing, up)
matrix = [facing, up, right]
new_b = [matvec(matrix, vec) for vec in b]
for a_point in a:
for b_point in new_b:
# assume they're the same
# add a-b to all b
delta = padd(a_point, pneg(b_point))
new_new_b = [padd(delta, b) for b in new_b]
if len(aset.intersection(map(tuple, new_new_b))) >= 12:
return new_new_b
return None
# if sample:
# print(common(scanners[0], scanners[1]))
# quit()
good_scanners = [None] * len(scanners)
good_scanners[0] = scanners[0]
done_scanners = [False] * len(scanners)
while True:
for i in range(len(scanners)):
if good_scanners[i] and not done_scanners[i]:
for j in range(len(scanners)):
if i != j and good_scanners[j] is None:
test = common(good_scanners[i], scanners[j])
# sprint(test)
if test is not None:
good_scanners[j] = test
done_scanners[i] = True
print(done_scanners, lmap(bool, good_scanners))
if all(done_scanners):
break
out = set(tuple(point) for points in good_scanners for point in points)
out = len(out)
if out:
print("out: ", out)
return # RETURNED VALUE DOESN'T DO ANYTHING, PRINT THINGS INSTEAD
with open('./2021/19.input') as input_file:
do_case(input_file.read())

1058
2021/19.input Normal file

File diff suppressed because it is too large Load Diff

68
2021/19.py Normal file
View File

@@ -0,0 +1,68 @@
import sys
inf = './2021/19.input'
ll = [x for x in open(inf).read().strip().split('\n\n')]
ll = [[eval("[" + x + "]") for x in l.split("\n")[1:]] for l in ll]
coord_remaps = [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
coord_negations = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]
def apply(remap, negat, scan):
ret = []
for item in scan:
ret.append([negat[0]*item[remap[0]], negat[1]*item[remap[1]], negat[2]*item[remap[2]]])
return ret
distances_from_scan_0 = [(0,0,0)]
def find_alignment(scan_a, scan_b):
in_a = set([tuple(x) for x in scan_a])
for remap in coord_remaps:
for negat in coord_negations:
a = scan_a
b = apply(remap, negat, scan_b)
for a_pos in a:
for b_pos in b:
remap_by = [b_pos[0]-a_pos[0], b_pos[1]-a_pos[1], b_pos[2]-a_pos[2]]
matches = 0
all_remapped = []
for other_b in b:
remapped_to_a = (other_b[0]-remap_by[0], other_b[1]-remap_by[1], other_b[2]-remap_by[2])
if remapped_to_a in in_a:
matches += 1
all_remapped.append(list(remapped_to_a))
if matches >= 12:
print("match", remap_by)
distances_from_scan_0.append(tuple(remap_by))
return (True, all_remapped)
return (False, None)
good = ll[0]
aligned_indices = set()
aligned_indices.add(0)
aligned = {}
aligned[0] = ll[0]
all_aligned = []
all_aligned += [tuple(x) for x in ll[0]]
noalign = set()
while len(aligned_indices) < len(ll):
for i in range(len(ll)):
if i in aligned_indices:
continue
for j in aligned_indices:
print("Checking", i, "against", j)
if (i,j) in noalign:
continue
ok, remap = find_alignment(aligned[j], ll[i])
if ok:
aligned_indices.add(i)
aligned[i] = remap
all_aligned += [tuple(x) for x in remap]
break
noalign.add((i,j))
print(len(set(all_aligned)))
dists = []
for a in distances_from_scan_0:
for b in distances_from_scan_0:
dists.append(sum([abs(a[0]-b[0]), abs(a[1]-b[1]), abs(a[2]-b[2])]))
print(max(dists))

136
2021/19.test Normal file
View File

@@ -0,0 +1,136 @@
--- scanner 0 ---
404,-588,-901
528,-643,409
-838,591,734
390,-675,-793
-537,-823,-458
-485,-357,347
-345,-311,381
-661,-816,-575
-876,649,763
-618,-824,-621
553,345,-567
474,580,667
-447,-329,318
-584,868,-557
544,-627,-890
564,392,-477
455,729,728
-892,524,684
-689,845,-530
423,-701,434
7,-33,-71
630,319,-379
443,580,662
-789,900,-551
459,-707,401
--- scanner 1 ---
686,422,578
605,423,415
515,917,-361
-336,658,858
95,138,22
-476,619,847
-340,-569,-846
567,-361,727
-460,603,-452
669,-402,600
729,430,532
-500,-761,534
-322,571,750
-466,-666,-811
-429,-592,574
-355,545,-477
703,-491,-529
-328,-685,520
413,935,-424
-391,539,-444
586,-435,557
-364,-763,-893
807,-499,-711
755,-354,-619
553,889,-390
--- scanner 2 ---
649,640,665
682,-795,504
-784,533,-524
-644,584,-595
-588,-843,648
-30,6,44
-674,560,763
500,723,-460
609,671,-379
-555,-800,653
-675,-892,-343
697,-426,-610
578,704,681
493,664,-388
-671,-858,530
-667,343,800
571,-461,-707
-138,-166,112
-889,563,-600
646,-828,498
640,759,510
-630,509,768
-681,-892,-333
673,-379,-804
-742,-814,-386
577,-820,562
--- scanner 3 ---
-589,542,597
605,-692,669
-500,565,-823
-660,373,557
-458,-679,-417
-488,449,543
-626,468,-788
338,-750,-386
528,-832,-391
562,-778,733
-938,-730,414
543,643,-506
-524,371,-870
407,773,750
-104,29,83
378,-903,-323
-778,-728,485
426,699,580
-438,-605,-362
-469,-447,-387
509,732,623
647,635,-688
-868,-804,481
614,-800,639
595,780,-596
--- scanner 4 ---
727,592,562
-293,-554,779
441,611,-461
-714,465,-776
-743,427,-804
-660,-479,-426
832,-632,460
927,-485,-438
408,393,-506
466,436,-512
110,16,151
-258,-428,682
-393,719,612
-211,-452,876
808,-476,-593
-575,615,604
-485,667,467
-680,325,-822
-627,-443,-432
872,-547,-609
833,512,582
807,604,487
839,-516,451
891,-625,532
-652,-548,-490
30,-46,-14