File tree Expand file tree Collapse file tree 3 files changed +4760
-0
lines changed Expand file tree Collapse file tree 3 files changed +4760
-0
lines changed Original file line number Diff line number Diff line change 1+ export function part1 ( input ) {
2+ let password = 0 ;
3+ input
4+ . split ( "\n" )
5+ . map ( x => {
6+ let [ , direction , count ] = x . match ( / ( L | R ) ( \d + ) / ) ;
7+ return { direction, count : + count } ;
8+ } )
9+ . reduce ( ( prev , curr ) => {
10+ if ( curr . direction === "L" ) prev -= curr . count ;
11+ if ( curr . direction === "R" ) prev += curr . count ;
12+ while ( prev < 0 ) prev += 100 ;
13+ while ( prev >= 100 ) prev -= 100 ;
14+ if ( prev === 0 ) password ++ ;
15+ return prev ;
16+ } , 50 ) ;
17+
18+ return password ;
19+ }
20+
21+ export function part2 ( input ) {
22+ let password = 0 ;
23+ input
24+ . split ( "\n" )
25+ . map ( x => {
26+ let [ , direction , count ] = x . match ( / ( L | R ) ( \d + ) / ) ;
27+ return { direction, count : + count } ;
28+ } )
29+ . reduce ( ( prev , curr ) => {
30+ if ( curr . direction === "L" ) {
31+ for ( let i = 0 ; i < curr . count ; i ++ ) {
32+ prev -- ;
33+ if ( prev === 0 ) password ++ ;
34+ if ( prev === - 1 ) prev = 99 ;
35+ }
36+ }
37+ if ( curr . direction === "R" ) {
38+ for ( let i = 0 ; i < curr . count ; i ++ ) {
39+ prev ++ ;
40+ if ( prev === 100 ) prev = 0 ;
41+ if ( prev === 0 ) password ++ ;
42+ }
43+ }
44+ return prev ;
45+ } , 50 ) ;
46+
47+ return password ;
48+ }
Original file line number Diff line number Diff line change 1+ import { part1 , part2 } from "./day01.js" ;
2+ import { describe , test , expect } from "vitest" ;
3+ import readInput from "../utils/read-input.js" ;
4+
5+ let input = readInput ( import . meta. url ) ;
6+
7+ describe ( "day01 2025" , ( ) => {
8+ describe ( "part1" , ( ) => {
9+ test ( "it should work for part 1 examples" , ( ) => {
10+ expect (
11+ part1 (
12+ [
13+ "L68" ,
14+ "L30" ,
15+ "R48" ,
16+ "L5" ,
17+ "R60" ,
18+ "L55" ,
19+ "L1" ,
20+ "L99" ,
21+ "R14" ,
22+ "L82" ,
23+ ] . join ( "\n" ) ,
24+ ) ,
25+ ) . toEqual ( 3 ) ;
26+ } ) ;
27+
28+ test ( "it should work for part 1 input" , ( ) => {
29+ expect ( part1 ( input ) ) . toEqual ( 1150 ) ;
30+ } ) ;
31+ } ) ;
32+
33+ describe ( "part2" , ( ) => {
34+ test ( "it should work for part 2 examples" , ( ) => {
35+ expect (
36+ part2 (
37+ [
38+ "L68" ,
39+ "L30" ,
40+ "R48" ,
41+ "L5" ,
42+ "R60" ,
43+ "L55" ,
44+ "L1" ,
45+ "L99" ,
46+ "R14" ,
47+ "L82" ,
48+ ] . join ( "\n" ) ,
49+ ) ,
50+ ) . toEqual ( 6 ) ;
51+ } ) ;
52+
53+ test ( "it should work for part 2 input" , ( ) => {
54+ expect ( part2 ( input ) ) . toEqual ( 6738 ) ;
55+ } ) ;
56+ } ) ;
57+ } ) ;
You can’t perform that action at this time.
0 commit comments