Skip to content

Commit

Permalink
day21
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Dec 21, 2024
1 parent 4d31e37 commit 52004a3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 19 deletions.
79 changes: 79 additions & 0 deletions day21_get_direction_keypad_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from itertools import groupby

# < = 60
# > = 62
# A = 65
# ^ = 94
# v = 118

COORDS = {
60: (0, 1),
62: (2, 1),
65: (2, 0),
94: (1, 0),
118: (1, 1),
}

all_moves = {}

for a in [60, 62, 65, 94, 118]:
for b in [60, 62, 65, 94, 118]:
k = a-60 + (b-60)*3

a_coords = COORDS[a]
b_coords = COORDS[b]

dx = b_coords[0] - a_coords[0]
dy = b_coords[1] - a_coords[1]

moves = []
if dx > 0:
moves.append(">" * dx)
elif dx < 0:
moves.append("<" * -dx)

if dy > 0:
moves.append("v" * dy)
elif dy < 0:
moves.append("^" * -dy)

moves = "".join(moves)
moves = list(set([moves, moves[::-1]]))

if a == 60 and b != 60 and len(moves) > 0:
moves = [m for m in moves if m[0] != "^"]

if b == 60 and a != 60 and len(moves) > 0:
moves = [m for m in moves if m[-1] != "v"]

moves = list(sorted([m + "A" for m in moves]))

agg_key = " ".join(["".join(m) for m in moves])
if agg_key not in all_moves:
all_moves[agg_key] = {
"moves": moves,
"keys": [],
"comments": [],
}

all_moves[agg_key]["keys"].append(str(k))
all_moves[agg_key]["comments"].append(chr(a) + chr(b))


print('fn get_direction_keypad_paths(from: u8, to: u8) -> Vec<Vec<u8>> {')
print(' match from - 60 + (to - 60) * 3 {')
for group in all_moves.values():
keys = " | ".join(group["keys"])
moves = ", ".join([
"vec![{}]".format(", ".join([
"b'{}'".format(c)
for c in list(m)
]))
for m in group["moves"]
])
comments = " ".join(group["comments"])
print(" {} => vec![{}], // {}".format(keys, moves, comments))

print(" _ => vec![],")
print(" }")
print("}")
36 changes: 17 additions & 19 deletions src/day21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,23 @@ fn get_numeric_keypad_paths(from: u8, to: u8) -> Vec<Vec<u8>> {
}

fn get_direction_keypad_paths(from: u8, to: u8) -> Vec<Vec<u8>> {
match (from, to) {
(f, t) if f == t => vec![vec![b'A']],
(b'A', b'<') => vec![vec![b'v', b'<', b'<', b'A']],
(b'<', b'A') => vec![vec![b'>', b'>', b'^', b'A']],
(b'A', b'v') => vec![vec![b'v', b'<', b'A'], vec![b'<', b'v', b'A']],
(b'v', b'<') => vec![vec![b'<', b'A']],
(b'A', b'>') => vec![vec![b'v', b'A']],
(b'>', b'^') => vec![vec![b'<', b'^', b'A'], vec![b'^', b'<', b'A']],
(b'^', b'A') => vec![vec![b'>', b'A']],
(b'A', b'^') => vec![vec![b'<', b'A']],
(b'>', b'A') => vec![vec![b'^', b'A']],
(b'^', b'>') => vec![vec![b'v', b'>', b'A'], vec![b'>', b'v', b'A']],
(b'v', b'>') => vec![vec![b'>', b'A']],
(b'v', b'A') => vec![vec![b'>', b'^', b'A'], vec![b'^', b'>', b'A']],
(b'<', b'^') => vec![vec![b'>', b'^', b'A']],
(b'^', b'<') => vec![vec![b'v', b'<', b'A']],
(b'>', b'v') => vec![vec![b'<', b'A']],
(b'<', b'v') => vec![vec![b'>', b'A']],
_ => panic!("Unknown move from {} to {}", from as char, to as char),
match from - 60 + (to - 60) * 3 {
0 | 8 | 20 | 136 | 232 => vec![vec![b'A']], // << >> AA ^^ vv
6 => vec![vec![b'>', b'>', b'A']], // <>
15 => vec![vec![b'>', b'>', b'^', b'A']], // <A
102 => vec![vec![b'>', b'^', b'A']], // <^
174 | 49 | 64 => vec![vec![b'>', b'A']], // <v ^A v>
2 => vec![vec![b'<', b'<', b'A']], // ><
17 | 160 => vec![vec![b'^', b'A']], // >A v^
104 => vec![vec![b'<', b'^', b'A'], vec![b'^', b'<', b'A']], // >^
176 | 107 | 58 => vec![vec![b'<', b'A']], // >v A^ v<
5 => vec![vec![b'v', b'<', b'<', b'A']], // A<
11 | 208 => vec![vec![b'v', b'A']], // A> ^v
179 => vec![vec![b'<', b'v', b'A'], vec![b'v', b'<', b'A']], // Av
34 => vec![vec![b'v', b'<', b'A']], // ^<
40 => vec![vec![b'>', b'v', b'A'], vec![b'v', b'>', b'A']], // ^>
73 => vec![vec![b'>', b'^', b'A'], vec![b'^', b'>', b'A']], // vA
_ => vec![],
}
}

Expand Down

0 comments on commit 52004a3

Please sign in to comment.