File tree Expand file tree Collapse file tree 1 file changed +7
-13
lines changed Expand file tree Collapse file tree 1 file changed +7
-13
lines changed Original file line number Diff line number Diff line change 1- export function part1 ( input ) {
1+ function invalidSum ( input , regex ) {
22 let ranges = input . split ( "," ) . map ( range => range . split ( "-" ) . map ( Number ) ) ;
33 let sum = 0 ;
44 for ( let range of ranges ) {
55 for ( let i = range [ 0 ] ; i <= range [ 1 ] ; i ++ ) {
66 let str = i . toString ( ) ;
7- if ( str . match ( / ^ ( \d + ) \1 $ / ) ) {
7+ if ( str . match ( regex ) ) {
88 sum += i ;
99 }
1010 }
1111 }
1212 return sum ;
1313}
1414
15+ export function part1 ( input ) {
16+ return invalidSum ( input , / ^ ( \d + ) \1$ / ) ;
17+ }
18+
1519export function part2 ( input ) {
16- let ranges = input . split ( "," ) . map ( range => range . split ( "-" ) . map ( Number ) ) ;
17- let sum = 0 ;
18- for ( let range of ranges ) {
19- for ( let i = range [ 0 ] ; i <= range [ 1 ] ; i ++ ) {
20- let str = i . toString ( ) ;
21- if ( str . match ( / ^ ( \d + ) \1+ $ / ) ) {
22- sum += i ;
23- }
24- }
25- }
26- return sum ;
20+ return invalidSum ( input , / ^ ( \d + ) \1+ $ / ) ;
2721}
You can’t perform that action at this time.
0 commit comments