@@ -331,249 +331,5 @@ static void BuildPath(
331331}
332332}
333333}
334-
335- #ifGARBAGE
336- private struct State
337- {
338- internal char KeypadRobotPos ;
339- internal char DPadRobot1Pos ;
340- internal char DPadRobot2Pos ;
341-
342- internal static State New ( )
343- {
344- State ret = default ;
345- ret . KeypadRobotPos = ret . DPadRobot1Pos = ret . DPadRobot2Pos = 'A' ;
346- return ret ;
347- }
348- }
349-
350- internal static void Problem1x ( )
351- {
352- char [ ] dpad = [ '<' , 'v' , '>' , 'A' , '^' ] ;
353- char [ ] keypad = "0123456789A" . ToCharArray ( ) ;
354-
355- Console . WriteLine ( "Keypad" ) ;
356- foreach ( char one in keypad )
357- {
358- foreach ( char two in keypad )
359- {
360- Console . WriteLine ( $ "From{ one } to{ two } :{ new string ( KeypadMoves ( one , two ) . ToArray ( ) ) } ") ;
361- }
362- }
363-
364- Console . WriteLine ( "dpad" ) ;
365- foreach ( char one in dpad )
366- {
367- foreach ( char two in dpad )
368- {
369- Console . WriteLine ( $ "From{ one } to{ two } :{ new string ( DPadMoves ( one , two ) . ToArray ( ) ) } ") ;
370- }
371- }
372-
373- }
374-
375- internal static void Problem1 ( )
376- {
377- List < string > codes = Load ( ) ;
378- //List<string> codes = ["37"];
379- long ret = 0 ;
380-
381- foreach ( string code in codes )
382- {
383- int numberPart = int . Parse ( code . AsSpan ( 0 , code . Length - 1 ) ) ;
384- StringBuilder humanSeq = MoveKeypad ( code , State . New ( ) ) ;
385-
386- Console . WriteLine ( $ "Code={ code } , Number={ numberPart } , CodeLen={ humanSeq . Length } , Ret+={ humanSeq . Length * numberPart } ") ;
387- Console . WriteLine ( humanSeq ) ;
388- Console . WriteLine ( ) ;
389-
390- ret += ( long ) numberPart * humanSeq . Length ;
391- }
392-
393- Console . WriteLine ( ret ) ;
394- }
395-
396- internal static void Problem2x ( )
397- {
398- }
399-
400- private static StringBuilder MoveKeypad ( ReadOnlySpan < char > code , State currentState )
401- {
402- StringBuilder keypadRobot = new ( ) ;
403- StringBuilder dpad1 = new ( ) ;
404- StringBuilder dpad2 = new ( ) ;
405- StringBuilder human = new ( ) ;
406- StringBuilder debug = new ( ) ;
407-
408- foreach ( char codeChar in code )
409- {
410- keypadRobot . Append ( codeChar ) ;
411-
412- //Console.WriteLine($"Keypad robot is at {currentState.KeypadRobotPos} and needs to be at {codeChar}");
413- IEnumerable < char > keypadSequence = KeypadMoves ( currentState . KeypadRobotPos , codeChar ) ;
414-
415- foreach ( char keypadSeqChar in keypadSequence )
416- {
417- dpad1 . Append ( keypadSeqChar ) ;
418- //Console.WriteLine($"DPad1 is at {currentState.DPadRobot1Pos} and needs to be at {keypadSeqChar}");
419-
420- IEnumerable < char > keypadDPadSequence = DPadMoves ( currentState . DPadRobot1Pos , keypadSeqChar ) ;
421-
422- foreach ( char dpad1Char in keypadDPadSequence )
423- {
424- //Console.WriteLine($"DPad2 is at {currentState.DPadRobot2Pos} and needs to be at {dpad1Char}");
425-
426- dpad2 . Append ( dpad1Char ) ;
427-
428- IEnumerable < char > dpadDpadSequence = DPadMoves ( currentState . DPadRobot2Pos , dpad1Char ) ;
429- debug . Clear ( ) ;
430-
431- foreach ( char dpad2Char in dpadDpadSequence )
432- {
433- human . Append ( dpad2Char ) ;
434- debug . Append ( dpad2Char ) ;
435- }
436-
437- string debugStr = debug . ToString ( ) ;
438- if ( ( debugStr . Contains ( '^' ) && debugStr . Contains ( 'v' ) ) ||
439- ( debugStr . Contains ( '>' ) && debugStr . Contains ( '<' ) ) )
440- {
441- throw new InvalidDataException (
442- $ "Garbage produced for{ currentState . DPadRobot2Pos } =>{ dpad1Char } :{ debugStr } ") ;
443- }
444-
445- currentState . DPadRobot2Pos = dpad1Char ;
446- //human.Append(" ");
447- }
448-
449- currentState . DPadRobot1Pos = keypadSeqChar ;
450- //dpad2.Append(" ");
451- }
452-
453- //dpad2.Append(" ");
454- currentState . KeypadRobotPos = codeChar ;
455- }
456-
457- Console . WriteLine ( human ) ;
458- Console . WriteLine ( dpad2 ) ;
459- Console . WriteLine ( dpad1 ) ;
460- Console . WriteLine ( keypadRobot ) ;
461-
462- return human ;
463- }
464-
465- // x^A
466- // <v>
467- private static IEnumerable < char > DPadMoves ( char from , char to )
468- {
469- #ifDEBUG
470- char origFrom = from ;
471- char origTo = to ;
472- #endif
473-
474- int fromRow = Row ( from ) ;
475- int fromCol = Col ( from ) ;
476- int toRow = Row ( to ) ;
477- int toCol = Col ( to ) ;
478-
479- while ( fromCol < toCol )
480- {
481- yield return Right ;
482- fromCol ++ ;
483- }
484-
485- while ( fromRow < toRow )
486- {
487- yield return Up ;
488- fromRow ++ ;
489- }
490-
491- while ( fromRow > toRow )
492- {
493- yield return Down ;
494- fromRow -- ;
495- }
496-
497- while ( fromCol > toCol )
498- {
499- yield return Left ;
500- fromCol -- ;
501- }
502-
503-
504- yield return Submit ;
505-
506- static int Row ( char c ) => c switch
507- {
508- '^' or'A' => 1 ,
509- '<' or'v' or'>' => 0 ,
510- } ;
511-
512- static int Col ( char c ) => c switch
513- {
514- '<' or'4' or'1' => 0 ,
515- '^' or'v' => 1 ,
516- 'A' or'>' => 2 ,
517- } ;
518- }
519-
520- // 789
521- // 456
522- // 123
523- // x0A
524- private static IEnumerable < char > KeypadMoves ( char from , char to )
525- {
526- #ifDEBUG
527- char origFrom = from ;
528- char origTo = to ;
529- #endif
530-
531- int fromRow = Row ( from ) ;
532- int fromCol = Col ( from ) ;
533- int toRow = Row ( to ) ;
534- int toCol = Col ( to ) ;
535-
536- while ( fromCol < toCol )
537- {
538- yield return Right ;
539- fromCol ++ ;
540- }
541-
542- while ( fromRow < toRow )
543- {
544- yield return Up ;
545- fromRow ++ ;
546- }
547-
548- while ( fromCol > toCol )
549- {
550- yield return Left ;
551- fromCol -- ;
552- }
553-
554- while ( fromRow > toRow )
555- {
556- yield return Down ;
557- fromRow -- ;
558- }
559-
560- yield return Submit ;
561-
562- static int Row ( char c ) => c switch
563- {
564- '7' or'8' or'9' => 3 ,
565- '4' or'5' or'6' => 2 ,
566- '1' or'2' or'3' => 1 ,
567- '0' or'A' => 0 ,
568- } ;
569-
570- static int Col ( char c ) => c switch
571- {
572- '7' or'4' or'1' => 0 ,
573- '8' or'5' or'2' or'0' => 1 ,
574- '9' or'6' or'3' or'A' => 2 ,
575- } ;
576- }
577- #endif
578334}
579335}