From 9a19ca925105e8e797cb3a3becf650a28bd7621b Mon Sep 17 00:00:00 2001 From: Dory Date: Sat, 21 Dec 2024 19:37:10 -0800 Subject: [PATCH] git add d21p2.swift --- day21/d21p2.swift | 58 ++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/day21/d21p2.swift b/day21/d21p2.swift index 4794db2..2dd431a 100644 --- a/day21/d21p2.swift +++ b/day21/d21p2.swift @@ -56,8 +56,17 @@ struct Dirpad : Pad { nL: (1, 0), nD: (1, 1), nR: (1, 2) ] static var cache: [KeyPair: Set<[Key]>] = [ - // >^ >A >< >v (>>) - // v< v> v^ vA? (vv) + // <<0 - <^. - v^- vA + // ><- >v- >>0 >^ >A- + // ^<. ^v- ^> ^^0 ^A- + // A<. Av A>- A^- AA0 + // Noops + KeyPair(a: nL, b: nL): [[]], + KeyPair(a: nR, b: nR): [[]], + KeyPair(a: nU, b: nU): [[]], + KeyPair(a: nD, b: nD): [[]], + KeyPair(a: nA, b: nA): [[]], // Only solutions due to gap KeyPair(a: nL, b: nU): [[nR, nU]], KeyPair(a: nL, b: nA): [[nR, nR, nU]], @@ -76,8 +85,9 @@ struct Dirpad : Pad { KeyPair(a: nR, b: nA): [[nU]], KeyPair(a: nU, b: nD): [[nD]], KeyPair(a: nD, b: nU): [[nU]], - // Known short ones + // Known short ones. TODO: proper DP this part instead of by hand KeyPair(a: nA, b: nD): [[nL, nD]], + KeyPair(a: nD, b: nA): [[nU, nR]], KeyPair(a: nR, b: nU): [[nL, nU]], KeyPair(a: nU, b: nR): [[nD, nR]] ] @@ -143,22 +153,25 @@ Dirpad.press(nA, "v<^AA>A".map(String.init)[...]) //let answer = try readInput(CommandLine.arguments[1]).map { passwd in // Numpad.press(nA, passwd[...]).flatMap { dirSeq1 in -let _ = [[">", "A"]].map { passwd in - Dirpad.press(nA, passwd[...]).flatMap { dirSeq1 in - Dirpad.press(nA, dirSeq1[...]).flatMap { dirSeq2 in - Dirpad.press(nA, dirSeq2[...]).flatMap { dirSeq3 in - Dirpad.press(nA, dirSeq3[...]).flatMap { dirSeq4 in - Dirpad.press(nA, dirSeq4[...]).flatMap { dirSeq5 in +let _ = [["v", "A"]].map { passwd in + Dirpad.press(nA, passwd[...]).forEach { dirSeq1 in + Dirpad.press(nA, dirSeq1[...]).forEach { dirSeq2 in + Dirpad.press(nA, dirSeq2[...]).forEach { dirSeq3 in + Dirpad.press(nA, dirSeq3[...]).forEach { dirSeq4 in + Dirpad.press(nA, dirSeq4[...]).forEach { dirSeq5 in + //Dirpad.press(nA, dirSeq4[...]).forEach { dirSeq6 in let out: String = ( - "\(passwd.joined()) " + - "\(dirSeq1.joined()) " + - "\(dirSeq2.joined()) " + - "\(dirSeq3.joined()) " + - "\(dirSeq4.joined()) " + - "\(dirSeq5.joined()) " + - "" - ) - print(out.replacingOccurrences(of: "A", with: "|")) + "\(passwd.joined()) " + + "\(dirSeq1.joined()) " + + "\(dirSeq2.joined()) " + + "\(dirSeq3.joined()) " + + "\(dirSeq4.joined()) " + + "\(dirSeq5.count) " + + // "\(dirSeq6.count) " + + "" + ).replacingOccurrences(of: "A", with: "|") + print("\(dirSeq5.count)" + out) + //} } } } @@ -166,21 +179,20 @@ let _ = [[">", "A"]].map { passwd in } } - var answer = 0 for passwd in try readInput(CommandLine.arguments[1]) { var seq = Numpad.press(nA, passwd[...]) seq.map { $0.joined() }.forEach { print("\($0.count): \($0)") } print() - for _ in 0..<2 { + for _ in 0..<25 { let tmpSeq = seq.flatMap { seq in Dirpad.press(nA, seq[...]) } let minLen = seq.map { $0.count }.min()! seq = tmpSeq.filter { $0.count > minLen } - seq.map { $0.joined() }.forEach { print("\($0.count): \($0)") } - print() + //seq.map { $0.joined() }.forEach { print("\($0.count): \($0)") } + //print() } let r = seq.map { $0.count }.min()! * Int(passwd.joined().dropLast())! - print("\(passwd.joined()): \(r)") + //print("\(passwd.joined()): \(r)") answer += r } print(answer)