@@ -10,6 +10,80 @@ type DayInput = string;
1010type DaySolver = ( input : DayInput ) => void ;
1111
1212const daySolvers : { [ key : string ] : DaySolver } = {
13+ '01' : ( input : DayInput ) => {
14+ let zeroCount1 : number = 0 ;
15+ let zeroCount2 : number = 0 ;
16+ let currentPosition : number = 50 ;
17+ let newPosition : number ;
18+ const splitInput = input . split ( "\n" ) . filter ( ( line ) => line . trim ( ) !== '' ) ;
19+
20+ splitInput . forEach ( ( line : string ) => {
21+ const matches = line . match ( / ( [ R | L ] ) ( \d + ) / ) || [ ] ;
22+
23+ if ( matches . length < 3 ) {
24+ console . warn ( `Invalid instruction: ${ line } ` ) ;
25+ return ;
26+ }
27+
28+ const direction = matches [ 1 ] ;
29+ const distance = parseInt ( matches [ 2 ] , 10 ) ;
30+ let revolutions = Math . floor ( distance / 100 ) ;
31+ const remainder = distance % 100 ;
32+
33+ /*
34+ * Part 2 Logic:
35+ *
36+ * We need to track how many times we cross position 0.
37+ * Each full revolution (100 units) guarantees crossing position 0 once.
38+ * Therefore, for every full revolution in the distance, we increment zeroCount2 by 1.
39+ * After accounting for full revolutions, we check the remainder distance to see if it crosses position 0.
40+ *
41+ *
42+ * When moving right (R):
43+ * - If the current position + distance crosses or lands on 0, increment zeroCount2.
44+ *
45+ * When moving left (L):
46+ * - If the current position - distance crosses or lands on 0, increment zeroCount2.
47+ * - Starting position of 1, moving left 1 lands on 0 (crosses once).
48+ * - Starting position of 1, moving left 150 lands on 51, crosses 0 twice (once at 0 and once more after a full revolution).
49+ */
50+
51+ switch ( direction ) {
52+ case 'R' :
53+ newPosition = currentPosition + remainder ;
54+ if ( newPosition === 100 ) {
55+ newPosition = 0 ;
56+ } else if ( newPosition > 100 ) {
57+ newPosition = newPosition % 100 ;
58+ revolutions ++ ;
59+ }
60+ break ;
61+ case 'L' :
62+ newPosition = currentPosition - remainder ;
63+ if ( newPosition < 0 ) {
64+ newPosition += 100 ;
65+ if ( currentPosition !== 0 ) {
66+ revolutions ++ ;
67+ }
68+ }
69+ break ;
70+ default :
71+ throw new Error ( `Unknown direction: ${ direction } ` ) ;
72+ }
73+
74+ zeroCount2 += revolutions ;
75+ currentPosition = newPosition ;
76+
77+ if ( currentPosition === 0 ) {
78+ zeroCount1 ++ ;
79+ zeroCount2 ++ ;
80+ return ;
81+ }
82+ } ) ;
83+
84+ console . log ( `Part 1: Number of times position 0 was reached: ${ zeroCount1 } ` ) ;
85+ console . log ( `Part 2: Number of times position 0 was crossed: ${ zeroCount2 } ` ) ;
86+ } ,
1387} ;
1488
1589// Set up the CLI program.
0 commit comments