Init
This commit is contained in:
commit
fc496204e7
24 changed files with 2758 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
*/*_day/data/
|
||||
*/*_day/.idea/
|
||||
*/data/
|
||||
2
AoC_2025/.gitignore
vendored
Normal file
2
AoC_2025/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/target/
|
||||
/input/
|
||||
186
AoC_2025/Cargo.lock
generated
Normal file
186
AoC_2025/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "AoC_2025"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.5.49"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.103"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.111"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
7
AoC_2025/Cargo.toml
Normal file
7
AoC_2025/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "AoC_2025"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.53", features = ["derive"] }
|
||||
5
AoC_2025/README.md
Normal file
5
AoC_2025/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Advent of Code 2025
|
||||
|
||||
As with previous years, you’ll find quick access to the 2025 solutions below:
|
||||
|
||||
- [day 1](./src/days/day01.rs)
|
||||
9
AoC_2025/src/ansi.rs
Normal file
9
AoC_2025/src/ansi.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
pub const RESET: &str = "\x1b[0m";
|
||||
pub const BOLD: &str = "\x1b[1m";
|
||||
pub const RED: &str = "\x1b[31m";
|
||||
pub const GREEN: &str = "\x1b[32m";
|
||||
pub const YELLOW: &str = "\x1b[33m";
|
||||
pub const BLUE: &str = "\x1b[94m";
|
||||
pub const WHITE: &str = "\x1b[97m";
|
||||
pub const HOME: &str = "\x1b[H";
|
||||
pub const CLEAR: &str = "\x1b[J";
|
||||
22
AoC_2025/src/cli.rs
Normal file
22
AoC_2025/src/cli.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use clap::Parser;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
const DAY_RANGE: RangeInclusive<usize> = 1..=25;
|
||||
fn day_in_range(d: &str) -> Result<usize, String> {
|
||||
let day: usize = d.parse().map_err(|_| format!("`{d}` is not a number."))?;
|
||||
if DAY_RANGE.contains(&day) {
|
||||
Ok(day)
|
||||
} else {
|
||||
Err(format!(
|
||||
"`{day}` not in range {}-{}.",
|
||||
DAY_RANGE.start(),
|
||||
DAY_RANGE.end()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
pub struct Cli {
|
||||
#[arg(help = "day to run", value_parser = day_in_range)]
|
||||
pub day: usize,
|
||||
}
|
||||
17
AoC_2025/src/days/day01.rs
Normal file
17
AoC_2025/src/days/day01.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use crate::days::Solution;
|
||||
|
||||
pub struct Day01;
|
||||
|
||||
impl Solution for Day01 {
|
||||
type Input = ();
|
||||
|
||||
fn parse(&self, data: &str) -> Self::Input {}
|
||||
|
||||
fn part1(&self, input: &Self::Input) -> usize {
|
||||
0
|
||||
}
|
||||
|
||||
fn part2(&self, input: &Self::Input) -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
9
AoC_2025/src/days/mod.rs
Normal file
9
AoC_2025/src/days/mod.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
pub mod day01;
|
||||
|
||||
pub trait Solution {
|
||||
type Input;
|
||||
|
||||
fn parse(&self, data: &str) -> Self::Input;
|
||||
fn part1(&self, input: &Self::Input) -> usize;
|
||||
fn part2(&self, input: &Self::Input) -> usize;
|
||||
}
|
||||
3
AoC_2025/src/lib.rs
Normal file
3
AoC_2025/src/lib.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod ansi;
|
||||
pub mod cli;
|
||||
pub mod days;
|
||||
44
AoC_2025/src/main.rs
Normal file
44
AoC_2025/src/main.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use AoC_2025::{
|
||||
ansi::{BOLD, RED, RESET, YELLOW},
|
||||
cli,
|
||||
days::{self, Solution},
|
||||
};
|
||||
use clap::Parser;
|
||||
use std::{fs::read_to_string, process, time::Instant};
|
||||
|
||||
fn run(day: usize, sol: impl Solution) {
|
||||
let path = format!("input/{day:02}_day.txt");
|
||||
if let Ok(data) = read_to_string(&path) {
|
||||
let now = Instant::now();
|
||||
let input = sol.parse(&data);
|
||||
let part1 = sol.part1(&input);
|
||||
let part2 = sol.part2(&input);
|
||||
let elapsed = now.elapsed();
|
||||
|
||||
println!("{YELLOW}------------{RESET}");
|
||||
println!("{BOLD}{YELLOW} Day {day:02}{RESET}");
|
||||
println!("{YELLOW}------------{RESET}");
|
||||
println!("Part1: {part1}");
|
||||
println!("Part2: {part2}");
|
||||
println!("Time: {} ns", elapsed.as_nanos());
|
||||
} else {
|
||||
println!("{RED}------------{RESET}");
|
||||
println!("{BOLD}{RED} Day {day:02}{RESET}");
|
||||
println!("{RED}------------{RESET}");
|
||||
println!("Cannot read input at \"{path}\"");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = cli::Cli::parse();
|
||||
|
||||
let sol = match cli.day {
|
||||
1 => days::day01::Day01,
|
||||
_ => {
|
||||
eprintln!("Day {:02} is not implemented yet!", cli.day);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
run(cli.day, sol);
|
||||
}
|
||||
17
README.md
Normal file
17
README.md
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# Advent of Code 2022
|
||||
|
||||
I might write solutions in different languages but most of them will be in rust or golang.
|
||||
|
||||
You can find the quick access for year 2022 by following this link: [AoC 2022](./AoC_2022/README.md)
|
||||
|
||||
# Advent of Code 2023
|
||||
|
||||
This year, I mainly wrote my solutions using GOlang. Exceptions have been made for the last two days where I went with Python instead.
|
||||
|
||||
You can find the quick access for this year by following this link: [AoC 2023](./AoC_2023/README.md)
|
||||
|
||||
# Advent of Code 2024
|
||||
|
||||
This year, I mainly wrote my solutions using GO.
|
||||
|
||||
You can find a quick access for this year by following this link: [AoC 2024](./AoC_2024/README.md)
|
||||
1
aoc-helper/.gitignore
vendored
Normal file
1
aoc-helper/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target/
|
||||
1944
aoc-helper/Cargo.lock
generated
Normal file
1944
aoc-helper/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
13
aoc-helper/Cargo.toml
Normal file
13
aoc-helper/Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "aoc-helper"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.98"
|
||||
clap = { version = "4.5.41", features = ["derive"] }
|
||||
diesel = { version = "2.2.12", features = ["sqlite"] }
|
||||
glob = "0.3.2"
|
||||
log = "0.4.27"
|
||||
reqwest = { version = "0.12.22", features = ["blocking"] }
|
||||
simplelog = "0.12.2"
|
||||
90
aoc-helper/src/cli.rs
Normal file
90
aoc-helper/src/cli.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
use clap::{Args, Parser, Subcommand, ValueEnum};
|
||||
use std::fmt::Display;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
const YEAR_RANGE: RangeInclusive<usize> = 2015..=2025;
|
||||
const DAY_RANGE: RangeInclusive<usize> = 1..=25;
|
||||
|
||||
fn year_in_range(y: &str) -> Result<usize, String> {
|
||||
let year: usize = y.parse().map_err(|_| format!("`{y}` is not a number."))?;
|
||||
if YEAR_RANGE.contains(&year) {
|
||||
Ok(year)
|
||||
} else {
|
||||
Err(format!(
|
||||
"`{year}` not in range {}-{}.",
|
||||
YEAR_RANGE.start(),
|
||||
YEAR_RANGE.end()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn day_in_range(d: &str) -> Result<usize, String> {
|
||||
let day: usize = d.parse().map_err(|_| format!("`{d}` is not a number."))?;
|
||||
if DAY_RANGE.contains(&day) {
|
||||
Ok(day)
|
||||
} else {
|
||||
Err(format!(
|
||||
"`{day}` not in range {}-{}.",
|
||||
DAY_RANGE.start(),
|
||||
DAY_RANGE.end()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, ValueEnum)]
|
||||
pub enum Part {
|
||||
One,
|
||||
Two,
|
||||
}
|
||||
|
||||
impl Display for Part {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::One => write!(f, "1"),
|
||||
Self::Two => write!(f, "2"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "aoc-helper")]
|
||||
#[command(about = "CLI to help you interact with Advent Of Code.")]
|
||||
pub struct Cli {
|
||||
#[arg(short, long, global = true, help = "debug output")]
|
||||
pub verbose: bool,
|
||||
#[command(subcommand)]
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Command {
|
||||
/// Get an input file
|
||||
Input(InputArgs),
|
||||
/// Submit an answer
|
||||
Answer(AnswerArgs),
|
||||
/// Retrieve cookie session from Firefox
|
||||
GetSession,
|
||||
/// Set given cookie session
|
||||
SetSession { session: String },
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct InputArgs {
|
||||
#[arg(short, long, help = "output filepath [default: <year>_<day>_day.txt]")]
|
||||
pub output: Option<String>,
|
||||
#[arg(short, long, help = "year to use", value_parser = year_in_range)]
|
||||
pub year: usize,
|
||||
#[arg(short, long, help = "day to use", value_parser = day_in_range)]
|
||||
pub day: usize,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct AnswerArgs {
|
||||
#[arg(short, long, help = "year to use for submission", value_parser = year_in_range)]
|
||||
pub year: usize,
|
||||
#[arg(short, long, help = "day to use for submission", value_parser = day_in_range)]
|
||||
pub day: usize,
|
||||
#[arg(short, long, help = "part to use for submission", value_enum)]
|
||||
pub part: Part,
|
||||
pub answer: String,
|
||||
}
|
||||
70
aoc-helper/src/client.rs
Normal file
70
aoc-helper/src/client.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use log::debug;
|
||||
use std::{env, fs};
|
||||
|
||||
use reqwest::{
|
||||
blocking::{self, Response},
|
||||
header,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
cli::Part,
|
||||
cookies::session::{CONFIG_DIR, SESSION_FILE},
|
||||
};
|
||||
|
||||
const BASE_URL: &str = "https://adventofcode.com";
|
||||
|
||||
pub struct AocClient {
|
||||
client: blocking::Client,
|
||||
base_url: String,
|
||||
}
|
||||
|
||||
impl AocClient {
|
||||
pub fn new() -> Result<AocClient, reqwest::Error> {
|
||||
let home_dir = env::home_dir().expect("cannot read HOME var env");
|
||||
let session = fs::read_to_string(home_dir.join(CONFIG_DIR).join(SESSION_FILE))
|
||||
.expect("missing session file. Did you correctly set up the session using get-session or set-session commands ?");
|
||||
|
||||
let mut headers = header::HeaderMap::new();
|
||||
|
||||
let mut session_value =
|
||||
header::HeaderValue::from_str(format!("session={session}").as_str()).unwrap();
|
||||
session_value.set_sensitive(true);
|
||||
|
||||
headers.insert(header::COOKIE, session_value);
|
||||
|
||||
Ok(AocClient {
|
||||
client: blocking::Client::builder()
|
||||
.default_headers(headers)
|
||||
.build()?,
|
||||
base_url: String::from(BASE_URL),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_input_file(&self, year: usize, day: usize) -> Result<Response, reqwest::Error> {
|
||||
let url = format!("{}/{year}/day/{day}/input", self.base_url);
|
||||
|
||||
debug!("preparing: GET {url}");
|
||||
self.client.get(url).send()
|
||||
}
|
||||
|
||||
pub fn post_answer(
|
||||
&self,
|
||||
year: usize,
|
||||
day: usize,
|
||||
part: &Part,
|
||||
answer: &str,
|
||||
) -> Result<Response, reqwest::Error> {
|
||||
let url = format!("{}/{year}/day/{day}/answer", self.base_url);
|
||||
|
||||
let part_str = match part {
|
||||
Part::One => "1",
|
||||
Part::Two => "2",
|
||||
};
|
||||
|
||||
debug!("preparing: POST {url} - level: {part_str}, answer: {answer}");
|
||||
self.client
|
||||
.post(url)
|
||||
.form(&[("level", part_str), ("answer", answer)])
|
||||
.send()
|
||||
}
|
||||
}
|
||||
102
aoc-helper/src/commands.rs
Normal file
102
aoc-helper/src/commands.rs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
use anyhow::{Context, anyhow};
|
||||
use log::{debug, info};
|
||||
use std::{
|
||||
env, fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::cookies::session;
|
||||
use crate::{cli::Part, client::AocClient};
|
||||
|
||||
pub fn cmd_set_session(session: &str) -> anyhow::Result<()> {
|
||||
session::write_session_to_file(session, session::SESSION_FILE)
|
||||
.with_context(|| format!("could not set up session: {session}"))?;
|
||||
|
||||
info!("session set up successfully to: {session}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_get_session() -> anyhow::Result<()> {
|
||||
let home_path = env::home_dir().unwrap();
|
||||
let search_path = home_path.join(".mozilla/firefox/*.default-release/cookies.sqlite");
|
||||
let search_path_str = search_path.to_str().unwrap();
|
||||
|
||||
let mut path = PathBuf::new();
|
||||
for entry in glob::glob(search_path_str)
|
||||
.with_context(|| format!("failed to read glob pattern: {search_path_str}"))?
|
||||
.flatten()
|
||||
{
|
||||
path = entry;
|
||||
}
|
||||
|
||||
debug!("found glob pattern for {search_path_str}");
|
||||
|
||||
let filename = path.file_name().ok_or(anyhow!(
|
||||
"could not find firefox database file: cookies.sqlite"
|
||||
))?;
|
||||
|
||||
// tmp database if firefox open
|
||||
let tmp_path = Path::new("/tmp").join(filename);
|
||||
debug!("tmp_path: {:?}", &tmp_path);
|
||||
fs::copy(path, &tmp_path)?;
|
||||
|
||||
let session = session::retrieve_session(&tmp_path)
|
||||
.map_err(|err| anyhow!(err))
|
||||
.context("could not retrieve session")?;
|
||||
|
||||
debug!("retrieve session ok");
|
||||
|
||||
session::write_session_to_file(&session, session::SESSION_FILE)
|
||||
.context("could not save session")?;
|
||||
fs::remove_file(tmp_path)?;
|
||||
|
||||
info!("session retrieved and saved successfully");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_get_input_file(year: usize, day: usize, output: &str) -> anyhow::Result<()> {
|
||||
let client = AocClient::new().context("could not build aoc client for future requests")?;
|
||||
|
||||
debug!("aoc client ok");
|
||||
|
||||
let response = client
|
||||
.get_input_file(year, day)
|
||||
.context(format!("could not get input file for year: {year} and day: {day:02}."))?
|
||||
.error_for_status()
|
||||
.context(format!("could not get input file for year: {year} and day: {day:02}. Are you sure the session cookie is correctly set up ?"))?;
|
||||
|
||||
debug!("request ok");
|
||||
|
||||
fs::write(output, response.text().unwrap()).context("could not write input data to file")?;
|
||||
|
||||
info!("input data successfully retrieved and saved to '{output}'");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_submit_answer(year: usize, day: usize, part: &Part, answer: &str) -> anyhow::Result<()> {
|
||||
let client = AocClient::new().context("could not build aoc client for future requests")?;
|
||||
|
||||
debug!("aoc client ok");
|
||||
|
||||
let response = client
|
||||
.post_answer(year, day, part, answer)
|
||||
.context(format!(
|
||||
"could not submit '{answer}' for year: {year} and day: {day:02} - part {part}"
|
||||
))?
|
||||
.error_for_status()
|
||||
.context(format!(
|
||||
"could not submit '{answer}' for year: {year} and day: {day:02} - part {part}. Are you sure the session cookie is correctly set up ?"
|
||||
))?;
|
||||
|
||||
debug!("request ok");
|
||||
|
||||
let response_text = response.text()?;
|
||||
|
||||
// Validate answer
|
||||
if response_text.contains("That's the right answer!") {
|
||||
info!("Correct ! That's the right answer.");
|
||||
} else {
|
||||
info!("That is not the right answer...");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
4
aoc-helper/src/cookies/mod.rs
Normal file
4
aoc-helper/src/cookies/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
mod model;
|
||||
mod schema;
|
||||
|
||||
pub mod session;
|
||||
21
aoc-helper/src/cookies/model.rs
Normal file
21
aoc-helper/src/cookies/model.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use diesel::prelude::Queryable;
|
||||
|
||||
#[allow(non_snake_case, unused)]
|
||||
#[derive(Debug, Queryable)]
|
||||
pub struct Cookies {
|
||||
pub id: Option<i32>,
|
||||
pub originAttributes: String,
|
||||
pub name: Option<String>,
|
||||
pub value: Option<String>,
|
||||
pub host: Option<String>,
|
||||
pub path: Option<String>,
|
||||
pub expiry: Option<i32>,
|
||||
pub lastAccessed: Option<i32>,
|
||||
pub creationTime: Option<i32>,
|
||||
pub isSecure: Option<i32>,
|
||||
pub isHttpOnly: Option<i32>,
|
||||
pub inBrowserElement: Option<i32>,
|
||||
pub sameSite: Option<i32>,
|
||||
pub schemeMap: Option<i32>,
|
||||
pub isPartitionedAttributeSet: Option<i32>,
|
||||
}
|
||||
19
aoc-helper/src/cookies/schema.rs
Normal file
19
aoc-helper/src/cookies/schema.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
diesel::table! {
|
||||
moz_cookies (id) {
|
||||
id -> Nullable<Integer>,
|
||||
originAttributes -> Text,
|
||||
name -> Nullable<Text>,
|
||||
value -> Nullable<Text>,
|
||||
host -> Nullable<Text>,
|
||||
path -> Nullable<Text>,
|
||||
expiry -> Nullable<Integer>,
|
||||
lastAccessed -> Nullable<Integer>,
|
||||
creationTime -> Nullable<Integer>,
|
||||
isSecure -> Nullable<Integer>,
|
||||
isHttpOnly -> Nullable<Integer>,
|
||||
inBrowserElement -> Nullable<Integer>,
|
||||
sameSite -> Nullable<Integer>,
|
||||
schemeMap -> Nullable<Integer>,
|
||||
isPartitionedAttributeSet -> Nullable<Integer>,
|
||||
}
|
||||
}
|
||||
78
aoc-helper/src/cookies/session.rs
Normal file
78
aoc-helper/src/cookies/session.rs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
use diesel::prelude::*;
|
||||
use diesel::{ConnectionResult, SqliteConnection};
|
||||
use log::debug;
|
||||
use std::path::Path;
|
||||
use std::{env, fmt, fs, io};
|
||||
|
||||
use super::model::Cookies;
|
||||
|
||||
pub const CONFIG_DIR: &str = ".config/aoc-helper";
|
||||
pub const SESSION_FILE: &str = "session";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SessionError {
|
||||
Connection(diesel::result::ConnectionError),
|
||||
Query(diesel::result::Error),
|
||||
NotFound,
|
||||
}
|
||||
|
||||
impl From<diesel::result::ConnectionError> for SessionError {
|
||||
fn from(value: diesel::result::ConnectionError) -> Self {
|
||||
Self::Connection(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<diesel::result::Error> for SessionError {
|
||||
fn from(value: diesel::result::Error) -> Self {
|
||||
Self::Query(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SessionError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
match self {
|
||||
Self::Connection(e) => write!(f, "connection to database error: {e}"),
|
||||
Self::Query(e) => write!(f, "query database error: {e}"),
|
||||
Self::NotFound => write!(f, "'.adventofcode.com' session not found"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn connect_database<P: AsRef<Path>>(path: P) -> ConnectionResult<SqliteConnection> {
|
||||
let database_url = path.as_ref().to_str().unwrap();
|
||||
SqliteConnection::establish(database_url)
|
||||
}
|
||||
|
||||
pub fn retrieve_session<P: AsRef<Path>>(path: P) -> Result<String, SessionError> {
|
||||
use super::schema::moz_cookies::dsl::{host, moz_cookies, name};
|
||||
|
||||
let mut conn = connect_database(path)?;
|
||||
debug!("connected to database");
|
||||
|
||||
let records = moz_cookies
|
||||
.filter(host.eq(".adventofcode.com"))
|
||||
.filter(name.eq("session"))
|
||||
.limit(1)
|
||||
.load::<Cookies>(&mut conn)?;
|
||||
|
||||
debug!("retrieved: {} record", records.len());
|
||||
if !records.is_empty() {
|
||||
let session = &records[0];
|
||||
session
|
||||
.value
|
||||
.to_owned()
|
||||
.ok_or_else(|| SessionError::NotFound)
|
||||
} else {
|
||||
Err(SessionError::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_session_to_file(session: &str, filename: &str) -> io::Result<()> {
|
||||
let home_path = env::home_dir().unwrap();
|
||||
let config_dir_path = home_path.join(CONFIG_DIR);
|
||||
if !fs::exists(&config_dir_path)? {
|
||||
fs::create_dir(&config_dir_path)?;
|
||||
}
|
||||
fs::write(config_dir_path.join(filename), session)?;
|
||||
Ok(())
|
||||
}
|
||||
35
aoc-helper/src/logging.rs
Normal file
35
aoc-helper/src/logging.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use std::{env::home_dir, fs::File};
|
||||
|
||||
use log::SetLoggerError;
|
||||
use simplelog::{
|
||||
CombinedLogger, ConfigBuilder, SharedLogger, TermLogger, TerminalMode, WriteLogger,
|
||||
};
|
||||
|
||||
use crate::cookies::session::CONFIG_DIR;
|
||||
|
||||
pub fn init_logs(log_level: log::LevelFilter, filename: &str) -> Result<(), SetLoggerError> {
|
||||
let mut filepath = home_dir().unwrap();
|
||||
filepath.push(CONFIG_DIR);
|
||||
filepath.push(filename);
|
||||
|
||||
let logfile = File::create(filepath).unwrap();
|
||||
let config_writelogger = ConfigBuilder::new().set_time_format_rfc2822().build();
|
||||
let config_termlogger = ConfigBuilder::new()
|
||||
.set_time_level(log::LevelFilter::Off)
|
||||
.set_thread_level(log::LevelFilter::Off)
|
||||
.set_target_level(log::LevelFilter::Off)
|
||||
.set_max_level(log::LevelFilter::Debug)
|
||||
.build();
|
||||
|
||||
let loggers: Vec<Box<dyn SharedLogger>> = vec![
|
||||
WriteLogger::new(log::LevelFilter::Debug, config_writelogger, logfile),
|
||||
TermLogger::new(
|
||||
log_level,
|
||||
config_termlogger,
|
||||
TerminalMode::Mixed,
|
||||
simplelog::ColorChoice::Auto,
|
||||
),
|
||||
];
|
||||
|
||||
CombinedLogger::init(loggers)
|
||||
}
|
||||
57
aoc-helper/src/main.rs
Normal file
57
aoc-helper/src/main.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
mod cli;
|
||||
mod client;
|
||||
mod commands;
|
||||
mod cookies;
|
||||
mod logging;
|
||||
|
||||
use clap::Parser;
|
||||
use cli::Command;
|
||||
use commands::{cmd_get_input_file, cmd_get_session, cmd_set_session, cmd_submit_answer};
|
||||
use log::{LevelFilter, error};
|
||||
use std::process;
|
||||
|
||||
const LOG_FILE: &str = "aoc.log";
|
||||
|
||||
fn main() {
|
||||
let cli = cli::Cli::parse();
|
||||
|
||||
let log_level = if cli.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Info
|
||||
};
|
||||
|
||||
if let Err(e) = logging::init_logs(log_level, LOG_FILE) {
|
||||
error!("failed to init logs: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
run(&cli).unwrap_or_else(|err| {
|
||||
error!("{err:?}");
|
||||
process::exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
fn run(opts: &cli::Cli) -> anyhow::Result<()> {
|
||||
match &opts.command {
|
||||
Command::Input(input_args) => {
|
||||
let output = match input_args.output.clone() {
|
||||
Some(o) => o,
|
||||
None => format!("{}_{}_day.txt", input_args.year, input_args.day),
|
||||
};
|
||||
|
||||
cmd_get_input_file(input_args.year, input_args.day, &output)?;
|
||||
}
|
||||
Command::Answer(answer_args) => {
|
||||
cmd_submit_answer(
|
||||
answer_args.year,
|
||||
answer_args.day,
|
||||
&answer_args.part,
|
||||
&answer_args.answer,
|
||||
)?;
|
||||
}
|
||||
Command::GetSession => cmd_get_session()?,
|
||||
Command::SetSession { session } => cmd_set_session(session)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue