Skip to content

Commit 3f1f99e

Browse files
committed
Completed part 1 of day 21 of year 2024
1 parent a7685b8 commit 3f1f99e

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

‎2024/21.py‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from itertools import product
5+
6+
split_data = True
7+
completed = 1
8+
raw_data = None # Not To be touched
9+
10+
numericPad = {
11+
'7': (0, 0),
12+
'8': (1, 0),
13+
'9': (2, 0),
14+
'4': (0, 1),
15+
'5': (1, 1),
16+
'6': (2, 1),
17+
'1': (0, 2),
18+
'2': (1, 2),
19+
'3': (2, 2),
20+
'0': (1, 3),
21+
'A': (2, 3)
22+
}
23+
24+
directionPad = {
25+
'^': (1, 0),
26+
'A': (2, 0),
27+
'<': (0, 1),
28+
'v': (1, 1),
29+
'>': (2, 1)
30+
}
31+
32+
def generate_paths(code, pad) -> list[list[str]]:
33+
start = pad['A']
34+
path_options = []
35+
for c in code:
36+
paths = []
37+
pos = pad[c]
38+
move_x = ">" if pos[0] - start[0] > 0 else "<"
39+
move_y = "v" if pos[1] - start[1] > 0 else "^"
40+
dx = +1 if pos[0] - start[0] > 0 else -1
41+
dy = +1 if pos[1] - start[1] > 0 else -1
42+
43+
44+
def backtrack(path, a, b, x, y):
45+
if (x,y) not in pad.values():
46+
return
47+
if a == 0 and b == 0:
48+
paths.append(path+"A")
49+
return
50+
if a > 0:
51+
backtrack(path + move_x, a-1, b, x+dx, y)
52+
if b > 0:
53+
backtrack(path + move_y, a, b-1, x, y+dy)
54+
55+
backtrack("", abs(pos[0] - start[0]), abs(pos[1] - start[1]), start[0], start[1])
56+
# Now we go through and verify that each path is on the keypad at all times.
57+
path_options.append(paths)
58+
59+
start = pos
60+
61+
return path_options
62+
63+
def options_comb(options):
64+
# def backtrack
65+
...
66+
67+
68+
def part1(data):
69+
70+
71+
72+
# What did I just end up reading...
73+
complexity = 0
74+
distribution = []
75+
for code in data:
76+
distribution.append([])
77+
# The only possible method is to figure out all the possible paths...
78+
# This then can be put through A* where score == the distance between the keys being pressed
79+
# So 029A has the distance of 8
80+
# <A^A>^^AvvvA has a distance of 28
81+
# v<<A>>^A<A>AvA<^AA>A<vAAA>^A has a distance of 68 (<- Thus creating the shortest path)
82+
83+
# Figure out all possible methods of tying the code...
84+
# Then start from the smallest distance score and figure out all the paths
85+
# The distance score of those paths will now need to be minized...
86+
87+
# Optimization whilst calculating the distance paths and possible paths would help prune out the sequence quickly...
88+
shortest = float('inf')
89+
90+
pad1_opt = generate_paths(code, numericPad)
91+
# Now we need to permute through the options and select the best one
92+
for perm1 in product(*pad1_opt):
93+
path_1 = ''.join(perm1)
94+
pad2_opt = generate_paths(path_1, directionPad)
95+
for perm2 in product(*pad2_opt):
96+
path_2 = ''.join(perm2)
97+
# if len(path_2) > shortest: continue
98+
pad3_opt = generate_paths(path_2, directionPad)
99+
for perm3 in product(*pad3_opt):
100+
path_3 = ''.join(perm3)
101+
distribution[-1].append(len(path_3))
102+
if len(path_3) < shortest:
103+
shortest = len(path_3)
104+
complexity += shortest * int(code[:-1])
105+
106+
107+
return complexity
108+
109+
def part2(data):
110+
...

0 commit comments

Comments
 (0)