aocli 0.0.9

Advent of Code helper CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use std::{fs, path::Path, process};

use serde::{Deserialize, Serialize};

use crate::{
    display,
    error::{AocError, Context, ErrorDisplayer, Result, ToErr},
    file::{FileInfo, PathInfo},
    network,
    run::{self, BuildResult},
    Parts, ROOT,
};

pub fn init(root: &Path) -> Result<()> {
    write_project_file(ROOT, root, "")?;
    write_project_file(
        "Cargo.toml",
        root,
        "[workspace]\nmembers = []\nresolver=\"2\"",
    )?;
    write_project_file(".gitignore", root, "/target\n/.session\n**/[1-2]/out/")?;
    write_project_file(".session", root, "")?;
    write_project_file(
        "README.md",
        root,
        "Solutions to the puzzles at \
    [Advent of Code](https://adventofcode.com) using \
    [aocli](https://github.com/sncxyz/aocli).",
    )?;
    let output = process::Command::new("git")
        .arg("init")
        .current_dir(root)
        .output()
        .context(AocError::GitInit);
    if let Some(output) = output.display_err() {
        if !output.status.success() {
            String::from_utf8_lossy(&output.stderr)
                .error()
                .context(AocError::GitInit)
                .display_err();
        }
    }
    Ok(())
}

fn write_project_file(name: &str, root: &Path, contents: &str) -> Result<()> {
    let path = &root.join(name);
    if path.try_is_file()? {
        display::info!("file `{name}` already exists");
    } else {
        fs::write(path, contents).context(AocError::FileWrite)?;
        display::success!("wrote file `{name}`");
    }
    Ok(())
}

pub fn new_day(path: &Path, year: &str, day: &str) -> Result<()> {
    if path.try_exists().context(AocError::FileRead)? {
        return AocError::PathExists(display::path(path)).err();
    }
    write_day_files(path, year, day).context(AocError::FileWrite)?;
    display::success!("created crate for {year}/{day}");
    add_workspace_member(path, year, day).context(AocError::WorkspaceMember)?;
    display::success!("added {year}/{day} to workspace");
    display::info!("building crate...");
    if run::build(path, false, false).display_err().is_some() {
        display::success!("finished building crate");
    }
    Ok(())
}

fn write_day_files(path: &Path, year: &str, day: &str) -> Result<()> {
    let is_day_25 = day == "25";
    let file_name = format!("{day}.rs");
    fs::create_dir_all(path.join("src"))?;
    fs::write(
        path.join("Cargo.toml"),
        format!(
            "[package]\n\
            name = \"y{year}d{day}\"\n\
            version = \"0.1.0\"\n\
            edition = \"2021\"\n\n\
            [[bin]]\n\
            name = \"y{year}d{day}\"\n\
            path = \"src/{file_name}\"\n\n\
            [dependencies]\n\
            aoclib = \"0.2.1\""
        ),
    )?;
    if is_day_25 {
        fs::write(
            path.join("src").join(&file_name),
            "aoc::parts!(1);\n\n\
            fn part_1(input: aoc::Input) -> impl ToString {\n    0\n}",
        )?;
    } else {
        fs::write(
            path.join("src").join(&file_name),
            "aoc::parts!(1);\n\n\
            fn part_1(input: aoc::Input) -> impl ToString {\n    0\n}\n\n\
            // fn part_2(input: aoc::Input) -> impl ToString {\n//     0\n// }",
        )?;
    }
    let data = path.join("data").join("actual");
    fs::create_dir_all(&data)?;
    fs::write(data.join("input"), "")?;
    fs::create_dir(data.join("1"))?;
    fs::write(data.join("1").join("answer"), "")?;
    if !is_day_25 {
        fs::create_dir(data.join("2"))?;
        fs::write(data.join("2").join("answer"), "")?;
    }
    Ok(())
}

#[derive(Serialize, Deserialize)]
struct CargoToml {
    workspace: Workspace,
}

#[derive(Serialize, Deserialize)]
struct Workspace {
    members: Vec<String>,
    resolver: String,
}

fn add_workspace_member(path: &Path, year: &str, day: &str) -> Result<()> {
    let root = path.parent().unwrap().parent().unwrap();
    let path = root.join("Cargo.toml");
    let file = fs::read_to_string(&path).context(AocError::WorkspaceCargo)?;
    let mut toml: CargoToml = toml::from_str(&file).context(AocError::WorkspaceCargo)?;
    toml.workspace.members.push(format!("{year}/{day}"));
    let file = toml::to_string(&toml).unwrap();
    fs::write(&path, file)?;
    Ok(())
}

pub fn add_input(path: &Path, data: &str) -> Result<()> {
    let mut data_path = path.join("data");
    if !data_path.try_is_dir()? {
        fs::create_dir(&data_path).context(AocError::FileWrite)?;
    }
    data_path.push(data);
    let data_path = &data_path;
    if data_path
        .try_is_dir()
        .map_err(|_| AocError::InputNameFormat)
        .context(AocError::InputName)?
    {
        return AocError::PathExists(display::path(data_path)).err();
    }
    write_data_files(data_path).context(AocError::FileWrite)?;
    display::success!("created input `{}` at {}", data, display::path(data_path));
    Ok(())
}

fn write_data_files(path: &Path) -> Result<()> {
    fs::create_dir(path)?;
    fs::write(path.join("input"), "")?;
    fs::create_dir(path.join("1"))?;
    fs::write(path.join("1").join("answer"), "")?;
    fs::create_dir(path.join("2"))?;
    fs::write(path.join("2").join("answer"), "")?;
    Ok(())
}

pub fn run_day(
    path: &Path,
    year: &str,
    day: &str,
    input: &str,
    parts: Parts,
    debug: bool,
) -> Result<()> {
    let data_path = &path.join("data").join(input);
    data_path
        .join("input")
        .read_file()?
        .try_contents()
        .context(AocError::NoInput)?;
    if !run::build(path, debug, true)?.success() {
        return Ok(());
    }
    match &parts {
        Parts::Default => {
            let mut both_unimplemented = true;
            for part in ["1", "2"] {
                match run::run(path, year, day, input, part, debug, true)? {
                    run::RunResult::Success { answer, time } => {
                        let correct = get_correct(data_path, part)?;
                        both_unimplemented = false;
                        display::answer_full(year, day, part, &answer, correct.as_deref(), time);
                    }
                    run::RunResult::Panic => {
                        both_unimplemented = false;
                        display::day_part(year, day, part);
                        display::panic();
                    }
                    _ => (),
                }
            }
            if both_unimplemented {
                display::info!("both parts unimplemented");
            }
        }
        Parts::Part(part) => match run::run(path, year, day, input, part, debug, true)? {
            run::RunResult::Success { answer, time } => {
                let correct = get_correct(data_path, part)?;
                display::answer_full(year, day, part, &answer, correct.as_deref(), time);
            }
            run::RunResult::Unimplemented => {
                display::day_part(year, day, part);
                display::unimplemented();
            }
            run::RunResult::Panic => {
                display::day_part(year, day, part);
                display::panic();
            }
        },
    }
    Ok(())
}

pub fn run_days(path: &Path, year: &str, days: impl IntoIterator<Item = u8>) -> Result<()> {
    let mut total_time = 0;
    let mut num_parts = 0;
    let mut total_days = 0;
    for day_number in days {
        total_days += 1;
        let day = &format!("{day_number:02}");
        let path = &path.join(day);
        if !path.try_is_dir()? {
            continue;
        }
        if !path
            .join("data")
            .join("actual")
            .join("input")
            .read_file()?
            .has_contents()
        {
            display::day(year, day);
            display::no_input();
            continue;
        }
        display::day(year, day);
        match run::build(path, false, false) {
            Ok(BuildResult::Failure) => {
                display::build_error();
                continue;
            }
            Err(e) => {
                display::build_error();
                return Err(e);
            }
            _ => (),
        }
        display::part("1");
        if let Err(e) = run_part(path, year, day, "1", &mut total_time, &mut num_parts) {
            display::run_error();
            return Err(e);
        }
        if day_number < 25 {
            display::day_part(year, day, "2");
            if let Err(e) = run_part(path, year, day, "2", &mut total_time, &mut num_parts) {
                display::run_error();
                return Err(e);
            }
        }
    }
    if total_days == 0 {
        return AocError::NoDays.err();
    }
    display::stats(total_time, num_parts);
    Ok(())
}

fn run_part(
    path: &Path,
    year: &str,
    day: &str,
    part: &str,
    total_time: &mut u64,
    num_parts: &mut u8,
) -> Result<()> {
    match run::run(path, year, day, "actual", part, false, false)? {
        run::RunResult::Panic => display::panic(),
        run::RunResult::Unimplemented => display::unimplemented(),
        run::RunResult::Success { answer, time } => {
            let correct = get_correct(&path.join("data").join("actual"), part)?;
            display::answer(&answer, correct.as_deref(), time);
            println!();
            *total_time += time;
            *num_parts += 1;
        }
    };
    Ok(())
}

pub fn test_day(path: &Path, year: &str, day: &str, parts: Parts) -> Result<()> {
    if !run::build(path, false, true)?.success() {
        return Ok(());
    }
    let parts = &match parts {
        Parts::Default => vec!["1", "2"],
        Parts::Part(ref part) => vec![part.as_ref()],
    }[..];
    if test_parts(path, year, day, parts)? {
        display::info!("nothing to test");
    }
    Ok(())
}

fn test_parts(path: &Path, year: &str, day: &str, parts: &[&str]) -> Result<bool> {
    let mut implemented = [true, true];
    let mut empty = true;
    for dir in path.join("data").read_dir().context(AocError::FileRead)? {
        let dir = &dir.context(AocError::FileRead)?;
        let input = dir.file_name();
        let Some(input) = input.to_str() else {
            continue;
        };
        let data_path = &dir.path();
        if !data_path.join("input").read_file()?.has_contents() {
            continue;
        }
        for (i, &part) in parts.iter().enumerate() {
            if !implemented[i] {
                continue;
            }
            let correct = get_correct(data_path, part)?;
            if correct.is_none() {
                continue;
            }
            empty = false;
            display::day_part(year, day, part);
            let result = run::run(path, year, day, input, part, false, false);
            if result.is_err() {
                display::run_error();
            }
            match result? {
                run::RunResult::Panic => display::panic_input(input),
                run::RunResult::Unimplemented => {
                    display::unimplemented();
                    implemented[i] = false;
                }
                run::RunResult::Success { answer, time } => {
                    display::answer(&answer, correct.as_deref(), time);
                    println!("  ({input})");
                }
            }
        }
    }
    Ok(empty)
}

pub fn test_days(path: &Path, year: &str, days: impl IntoIterator<Item = u8>) -> Result<()> {
    let mut empty = true;
    for day_number in days {
        let day = &format!("{day_number:02}");
        let path = &path.join(day);
        if !path.try_is_dir()? || !run::build(path, false, false)?.success() {
            continue;
        }
        if !test_parts(path, year, day, &["1", "2"])? {
            empty = false;
        }
    }
    if empty {
        display::info!("nothing to test");
    }
    Ok(())
}

pub fn get(path: &Path, year: &str, day: &str) -> Result<()> {
    const PARTS: [&str; 2] = ["1", "2"];
    let data_path = &path.join("data").join("actual");
    let input_path = &data_path.join("input");
    let answer_paths = &PARTS.map(|part| data_path.join(part).join("answer"));

    let update_input = !input_path.read_file()?.has_contents();
    let update_answers = [
        !answer_paths[0].read_file()?.has_contents(),
        day != "25" && !answer_paths[1].read_file()?.has_contents(),
    ];

    if update_input || update_answers[0] || update_answers[1] {
        let session = &get_session(path.parent().unwrap().parent().unwrap())?;
        if !data_path.try_is_dir()? {
            fs::create_dir_all(data_path).context(AocError::FileWrite)?;
        }
        if update_input {
            display::info!("downloading puzzle input...");
            let input = network::get_input(year, day, session)?;
            fs::write(input_path, input).context(AocError::FileWrite)?;
            display::success!("input file written to {}", display::path(input_path));
        }
        if update_answers[0] || update_answers[1] {
            display::info!("downloading puzzle answers...");
            let progress = network::get_progress(year, day, session)?;
            let answers = [progress.part_1, progress.part_2];
            for i in 0..2 {
                if !update_answers[i] {
                    continue;
                }
                let part = PARTS[i];
                if let Some(answer) = &answers[i] {
                    let part_path = answer_paths[i].parent().unwrap();
                    if !part_path.try_is_dir()? {
                        fs::create_dir(part_path).context(AocError::FileWrite)?;
                    }
                    fs::write(&answer_paths[i], answer).context(AocError::FileWrite)?;
                    display::success!(
                        "answer to part {part} written to {}",
                        display::path(&answer_paths[i])
                    );
                } else {
                    display::info!("no answer to part {part} found");
                }
            }
        }
    } else {
        display::info!("nothing to update");
    }

    Ok(())
}

pub fn submit(path: &Path, year: &str, day: &str, answer: Option<&str>) -> Result<()> {
    let session = &get_session(path.parent().unwrap().parent().unwrap())?;
    display::info!("getting progress");
    let progress = network::get_progress(year, day, session)?;
    if let Some(part) = &progress.next {
        let answer_path = &path.join("data").join("actual").join(part);
        let answer = &if let Some(answer) = answer {
            answer.to_string()
        } else {
            answer_path
                .join("out")
                .join("answer")
                .read_file()?
                .try_contents()
                .map_err(|_| "no answer to submit")?
        };
        if !answer_path.try_is_dir()? {
            fs::create_dir_all(answer_path).context(AocError::FileWrite)?;
        }
        display::day_part(year, day, part);
        let result = network::submit(year, day, part, answer, session);
        if result.is_err() {
            display::submit_error();
        }
        match result? {
            network::SubmissionResult::Correct => {
                display::just_answer(answer, true);
                fs::write(answer_path.join("answer"), answer).context(AocError::FileWrite)?;
            }
            network::SubmissionResult::Wait => {
                display::wait();
            }
            network::SubmissionResult::Incorrect => {
                display::just_answer(answer, false);
            }
        }
    } else {
        display::info!("no part left to submit");
    }
    Ok(())
}

pub fn open_year(year: &str) -> Result<()> {
    webbrowser::open(&format!("https://adventofcode.com/{year}")).context(AocError::Browser)
}

pub fn open_day(year: &str, day: &str) -> Result<()> {
    let day = &day.parse::<u8>().unwrap().to_string();
    webbrowser::open(&format!("https://adventofcode.com/{year}/day/{day}"))
        .context(AocError::Browser)
}

pub fn all_progress(path: &Path) -> Result<()> {
    let session = &get_session(path)?;
    let year_completion = network::get_year_completion("2015", session)?;
    println!();
    display::completion_header();
    display::year_completion("2015", year_completion);
    let mut year = 2016;
    let mut year_string = "2016".to_string();
    while let Ok(year_completion) = network::get_year_completion(&year_string, session) {
        display::year_completion(&year_string, year_completion);
        year += 1;
        year_string = year.to_string();
    }
    println!();
    Ok(())
}

pub fn year_progress(path: &Path, year: &str) -> Result<()> {
    let session = &get_session(path)?;
    let year_completion = network::get_year_completion(year, session)?;
    println!();
    display::completion_header();
    display::year_completion(year, year_completion);
    println!();
    Ok(())
}

pub fn day_progress(path: &Path, year: &str, day: &str) -> Result<()> {
    let session = &get_session(path)?;
    let progress = network::get_progress(year, day, session)?;
    display::day_part(year, day, "1");
    if let Some(answer) = &progress.part_1 {
        display::just_answer(answer, true);
    } else {
        display::incomplete();
    }
    if let Some(answer) = &progress.part_2 {
        display::day_part(year, day, "2");
        display::just_answer(answer, true);
    }
    Ok(())
}

pub fn clean_year(path: &Path) -> Result<()> {
    let mut empty = true;
    for day in 1..=25 {
        let day = &format!("{day:02}");
        let path = &path.join(day);
        if path.try_is_dir()? {
            clean_day(path, true)?;
            empty = false;
        }
    }
    if empty {
        display::info!("no day directories found");
    } else {
        display::success!("cleaned input and answer files for all days");
    }
    Ok(())
}

pub fn clean_day(path: &Path, silent: bool) -> Result<()> {
    let data_path = &path.join("data").join("actual");
    let input_path = &data_path.join("input");
    if input_path.try_is_file()? {
        fs::write(input_path, "").context(AocError::FileWrite)?;
        if !silent {
            display::success!("reset input file to empty");
        }
    } else if !silent {
        display::info!("no input file found");
    }
    for part in ["1", "2"] {
        let part_path = data_path.join(part).join("answer");
        if part_path.try_is_file()? {
            fs::write(part_path, "").context(AocError::FileWrite)?;
            if !silent {
                display::success!("reset part {part} answer file to empty");
            }
        } else if !silent {
            display::info!("no part {part} answer file found");
        }
    }
    Ok(())
}

pub fn help() -> Result<()> {
    webbrowser::open("https://github.com/sncxyz/aocli/blob/master/README.md#commands")
        .context(AocError::Browser)
}

fn get_session(root: &Path) -> Result<String> {
    root.join(".session")
        .read_file()
        .and_then(FileInfo::try_contents)
        .map(|contents| {
            let trimmed = contents.trim();
            if trimmed.starts_with("session=") {
                trimmed.to_owned()
            } else {
                format!("session={trimmed}")
            }
        })
        .context("failed to get session cookie")
}

fn get_correct(data_path: &Path, part: &str) -> Result<Option<String>> {
    Ok(data_path
        .join(part)
        .join("answer")
        .read_file()?
        .get_contents())
}