@@ -15,48 +15,33 @@ public static string ReadFile(int year, int day, string inputFilePath)
1515 Directory . CreateDirectory ( directory ) ;
1616 }
1717
18- string ? session = AoCClient . GetSessionAsync ( ) . Result ;
18+ string ? session = AoCClient . GetSession ( ) ;
19+ ArgumentNullException . ThrowIfNull ( session ) ;
1920 AoCClient downloader = new AoCClient ( session ) ;
20- string text = downloader . DownloadInputAsync ( year , day ) . Result ;
21+ string text = downloader . DownloadInput ( year , day ) ;
2122 File . WriteAllText ( inputFilePath , text ) ;
2223 return text ;
2324 }
2425 return File . ReadAllText ( inputFilePath ) ;
2526 }
2627
27- public static IEnumerable < string > StreamFile ( string inputFilePath )
28+ public static async Task < string > ReadFileAsync ( int year , int day , string inputFilePath )
2829 {
29- using var fs = new FileStream ( inputFilePath , FileMode . Open , FileAccess . Read ) ;
30- using TextReader sr = new StreamReader ( fs ) ;
31-
32- while ( sr . ReadLine ( ) is string line )
30+ if ( ! File . Exists ( inputFilePath ) )
3331 {
34- yield return line ;
32+ string directory = Path . GetDirectoryName ( inputFilePath ) ! ;
33+ if ( ! Directory . Exists ( directory ) )
34+ {
35+ Directory . CreateDirectory ( directory ) ;
36+ }
37+
38+ string ? session = await AoCClient . GetSessionAsync ( ) ;
39+ ArgumentNullException . ThrowIfNull ( session ) ;
40+ AoCClient downloader = new ( session ) ;
41+ string text = await downloader . DownloadInputAsync ( year , day ) ;
42+ await File . WriteAllTextAsync ( inputFilePath , text ) ;
43+ return text ;
3544 }
36- }
37-
38- private static string ReadFile ( [ CallerFilePath ] string sourceFilePath = "" )
39- {
40- string inputPath = sourceFilePath . Replace ( "Solution" , "input" ) . Replace ( ".cs" , ".txt" ) ;
41- string [ ] split = inputPath . Split ( Path . DirectorySeparatorChar ) ;
42- int year = int . Parse ( split . Single ( s => s . StartsWith ( "Year" ) ) . Substring ( 4 ) ) ;
43- int day = int . Parse ( split . Single ( s => s . StartsWith ( "Day" ) ) . Substring ( 3 ) ) ;
44-
45- return ReadFile ( year , day , inputPath ) ;
46- }
47-
48- private static IEnumerable < string > ReadFileLinesEnumerable ( string path )
49- {
50- return ReadFile ( path ) . Replace ( "\r \n " , "\n " ) . Split ( '\n ' , StringSplitOptions . RemoveEmptyEntries ) ;
51- }
52-
53- private static List < string > ReadFileLines ( [ CallerFilePath ] string path = null )
54- {
55- return ReadFileLinesEnumerable ( path ) . ToList ( ) ;
56- }
57-
58- private static string [ ] ReadFileLinesArray ( [ CallerFilePath ] string path = null )
59- {
60- return ReadFileLinesEnumerable ( path ) . ToArray ( ) ;
45+ return await File . ReadAllTextAsync ( inputFilePath ) ;
6146 }
6247}
0 commit comments