1+ package aminetti .adventofcode2024 ;
2+
3+ import org .apache .commons .io .IOUtils ;
4+ import org .slf4j .Logger ;
5+ import org .slf4j .LoggerFactory ;
6+
7+ import java .io .IOException ;
8+ import java .net .CookieHandler ;
9+ import java .net .CookieManager ;
10+ import java .net .HttpCookie ;
11+ import java .net .URI ;
12+ import java .net .http .HttpClient ;
13+ import java .net .http .HttpRequest ;
14+ import java .net .http .HttpResponse ;
15+ import java .nio .file .Files ;
16+ import java .nio .file .Path ;
17+ import java .nio .file .Paths ;
18+ import java .time .Duration ;
19+ import java .time .LocalDate ;
20+ import java .time .Month ;
21+ import java .util .regex .Matcher ;
22+ import java .util .regex .Pattern ;
23+
24+ import static java .nio .charset .StandardCharsets .UTF_8 ;
25+ import static java .nio .file .StandardOpenOption .*;
26+
27+ public class DaySetup {
28+ private static final Logger LOGGER = LoggerFactory .getLogger (DaySetup .class );
29+
30+ public static void main (String ... args ) throws IOException , InterruptedException {
31+ prepareCookie ();
32+
33+ int year = LocalDate .now ().getYear ();
34+ int day = getCurrentDay ();
35+
36+ cleanCodeAndTestAndInputForDay (year , day );
37+
38+ downloadDayInput (year , day );
39+ prepareCodeForDay (year , day );
40+ prepareTestsForDay (year , day );
41+
42+ }
43+
44+ static void cleanCodeAndTestAndInputForDay (int year , int day ) throws IOException , InterruptedException {
45+ String basePackageName = String .format ("adventofcode%d" , year );
46+ String packageName = String .format ("day%02d" , day );
47+
48+ String testClassName = String .format ("Day%02dTest" , day );
49+ Path testClassPath = Paths .get ("src" , "test" , "java" , "aminetti" , basePackageName , packageName , testClassName + ".java" );
50+ Files .deleteIfExists (testClassPath );
51+
52+
53+ String className = String .format ("Day%02d" , day );
54+ Path classPath = Paths .get ("src" , "test" , "java" , "aminetti" , basePackageName , packageName , className + ".java" );
55+ Files .deleteIfExists (classPath );
56+
57+ String folderName = String .format ("day%02d" , day );
58+ String fileName = String .format ("day%02d_input.txt" , day );
59+ Path inputFile = Paths .get ("src" , "test" , "resources" , folderName , fileName );
60+ Files .deleteIfExists (inputFile );
61+
62+ String testFileName = String .format ("day%02d_input_test.txt" , day );
63+ Path testInputFile = Paths .get ("src" , "test" , "resources" , folderName , testFileName );
64+ Files .deleteIfExists (testInputFile );
65+
66+ LOGGER .info ("All the files related to day {} has been deleted." , day );
67+
68+ }
69+
70+ static String getInput (int year , int day ) throws IOException , InterruptedException {
71+ URI uri = URI .create (String .format ("https://adventofcode.com/%d/day/%d/input" , year , day ));
72+
73+ HttpRequest req = HttpRequest .newBuilder ()
74+ .uri (uri )
75+ .header ("User-Agent" , "github.com/albertominetti/" )
76+ .GET ().build ();
77+
78+ try (HttpClient client = HttpClient .newBuilder ()
79+ .cookieHandler (CookieHandler .getDefault ())
80+ .version (HttpClient .Version .HTTP_2 )
81+ .connectTimeout (Duration .ofSeconds (10 ))
82+ .build ()) {
83+ return client .send (req , HttpResponse .BodyHandlers .ofString ()).body ();
84+ }
85+ }
86+
87+ static void prepareCookie () throws IOException {
88+ CookieManager cookieManager = new CookieManager ();
89+ String textCookie = IOUtils .resourceToString ("/cookie.txt" , UTF_8 );
90+ if (textCookie == null ) {
91+ throw new IllegalArgumentException ("Missing cookie file, please check your cookie.txt file in the resource directory." );
92+ }
93+ Pattern patternForCookie = Pattern .compile ("session=([a-f0-9]+)" );
94+ Matcher matcher = patternForCookie .matcher (textCookie );
95+ if (matcher .matches ()) {
96+ HttpCookie sessionCookie = new HttpCookie ("session" , matcher .group (1 ));
97+ sessionCookie .setPath ("/" );
98+ sessionCookie .setVersion (0 );
99+ cookieManager .getCookieStore ()
100+ .add (URI .create ("https://adventofcode.com" ), sessionCookie );
101+ } else {
102+ throw new IllegalArgumentException ("Invalid cookie format, please check your cookie.txt file." );
103+ }
104+
105+ CookieHandler .setDefault (cookieManager );
106+ }
107+
108+
109+ static void prepareTestsForDay (int year , int day ) throws IOException , InterruptedException {
110+ String basePackageName = String .format ("adventofcode%d" , year );
111+ String packageName = String .format ("day%02d" , day );
112+ String testClassName = String .format ("Day%02dTest" , day );
113+
114+ Path testClassPath = Paths .get ("src" , "test" , "java" , "aminetti" , basePackageName , packageName , testClassName + ".java" );
115+ Path templateClassPath = Paths .get ("src" , "test" , "java" , "aminetti" , "adventofcode2024" , "dayXX" , "DayXXTest.java" );
116+
117+ if (Files .exists (testClassPath )) {
118+ LOGGER .warn ("The test class file {} is already existent." , testClassPath .toAbsolutePath ());
119+ return ;
120+ }
121+
122+ Files .createDirectories (testClassPath .getParent ());
123+ String templateClassContent = Files .readString (templateClassPath );
124+
125+
126+ String className = String .format ("Day%02d" , day );
127+ String classContent = templateClassContent
128+ .replaceAll ("adventofcode[0-9]+" , basePackageName )
129+ .replace ("DayXX" , className )
130+ .replace ("DayXXTest" , testClassName )
131+ .replaceAll ("dayXX" , packageName );
132+ Files .writeString (testClassPath , classContent , WRITE , TRUNCATE_EXISTING , CREATE );
133+
134+ LOGGER .info ("The test java source file {} has been created." , testClassPath .toAbsolutePath ());
135+
136+ }
137+
138+ static void prepareCodeForDay (int year , int day ) throws IOException , InterruptedException {
139+ String basePackageName = String .format ("adventofcode%d" , year );
140+ String packageName = String .format ("day%02d" , day );
141+ String className = String .format ("Day%02d" , day );
142+
143+ Path classPath = Paths .get ("src" , "main" , "java" , "aminetti" , basePackageName , packageName , className + ".java" );
144+ Path templateClassPath = Paths .get ("src" , "main" , "java" , "aminetti" , "adventofcode2024" , "dayXX" , "DayXX.java" );
145+
146+ if (Files .exists (classPath )) {
147+ LOGGER .warn ("The class file {} is already existent." , classPath .toAbsolutePath ());
148+ return ;
149+ }
150+
151+ Files .createDirectories (classPath .getParent ());
152+ String templateClassContent = Files .readString (templateClassPath );
153+ String classContent = templateClassContent
154+ .replaceAll ("adventofcode[0-9]+" , basePackageName )
155+ .replace ("DayXX" , className )
156+ .replaceAll ("dayXX" , packageName );
157+ Files .writeString (classPath , classContent , WRITE , TRUNCATE_EXISTING , CREATE );
158+
159+ LOGGER .info ("The java source file {} has been created." , classPath .toAbsolutePath ());
160+
161+ }
162+
163+
164+ static void downloadDayInput (int year , int day ) throws IOException , InterruptedException {
165+ String folderName = String .format ("day%02d" , day );
166+ String fileName = String .format ("day%02d_input.txt" , day );
167+ Path path = Paths .get ("src" , "test" , "resources" , folderName , fileName );
168+
169+ if (Files .exists (path )) {
170+ LOGGER .warn ("The input file {} is already existent." , path .toAbsolutePath ());
171+ return ;
172+ }
173+
174+ Files .createDirectories (path .getParent ());
175+ Path testInput = path .getParent ().resolve (String .format ("day%02d_input_test.txt" , day ));
176+ Files .createFile (testInput );
177+ LOGGER .info ("The input file {} has been created." , testInput .toAbsolutePath ());
178+
179+ String dayInput = getInput (year , day );
180+ Files .writeString (path , dayInput , WRITE , TRUNCATE_EXISTING , CREATE );
181+ LOGGER .info ("The input file {} has been created." , path .toAbsolutePath ());
182+ }
183+
184+ static int getCurrentDay () {
185+ LocalDate now = LocalDate .now ();
186+ if (now .getMonth () != Month .DECEMBER ) {
187+ throw new IllegalArgumentException ("It is still not December." );
188+ }
189+
190+ return now .getDayOfMonth ();
191+ }
192+ }
0 commit comments