Skip to content

Commit 761f654

Browse files
committed
Fix get input script
1 parent 7b40d65 commit 761f654

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/constants.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
export const VALID_YEARS = ['2015','2016','2017','2018','2019','2020','2021','2022'];
1+
export const VALID_YEARS = [
2+
'2015',
3+
'2016',
4+
'2017',
5+
'2018',
6+
'2019',
7+
'2020',
8+
'2021',
9+
'2022',
10+
'2023',
11+
'2024'
12+
];
213

314
export const HOST = 'https://adventofcode.com';

src/hooks/useInputFile.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ import fs from 'fs';
33
import { useEffect, useState } from "react";
44
import { HOST, VALID_YEARS } from '../constants.js';
55

6-
const SAMPLE_REGEX = /\(your puzzle input\)[\s\S]*?<code>([\s\S]+?)<\/code>/;
7-
86
const decode = (str: string) => {
97
return str
108
.replaceAll('&gt;', '>')
11-
.replaceAll('&lt;', '<');
9+
.replaceAll('&lt;', '<')
10+
.replaceAll('<code>', '')
11+
.replaceAll('</code>', '')
12+
.replaceAll('<em>', '')
13+
.replaceAll('</em>', '')
14+
;
1215
};
1316

17+
const findLongest = (str: string[]) => {
18+
let longest = ''
19+
str.forEach((s: string) => {
20+
if (s.length > longest.length) {
21+
longest = s
22+
}
23+
})
24+
return longest
25+
}
26+
1427
export const useInputFile = (year: string, day: string, inp: string, ts: number): AppFile => {
1528
const [name, setName] = useState<string>('')
1629
const [size, setSize] = useState<number>(0)
@@ -56,9 +69,10 @@ export const useInputFile = (year: string, day: string, inp: string, ts: number)
5669
}
5770
}).then(res => {
5871
if (res.data) {
72+
const SAMPLE_REGEX = /<code>(<em>)?([\s\S]+?)(<\/em>)?<\/code>/g;
5973
const matches = res.data.match(SAMPLE_REGEX);
6074
if (matches) {
61-
fs.writeFileSync(file, decode(matches[1]).trim());
75+
fs.writeFileSync(file, decode(findLongest(matches)).trim());
6276
const stats = fs.statSync(file)
6377
setSize(stats.size);
6478
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy