2023-12-04 23:17:16 -07:00
|
|
|
use scraper::{Html, Selector};
|
|
|
|
|
|
|
|
pub async fn fetch_input(auth: String, day: u8) -> String {
|
2023-12-05 00:03:50 -07:00
|
|
|
let input = reqwest::Client::new()
|
2023-12-04 23:17:16 -07:00
|
|
|
.get(format!("https://adventofcode.com/2023/day/{day}/input"))
|
|
|
|
.header("Cookie", format!("session={auth}"))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("Unable to make input request")
|
|
|
|
.text()
|
|
|
|
.await
|
|
|
|
.expect("Unable to parse string from input response")
|
|
|
|
.trim_end()
|
2023-12-05 00:03:50 -07:00
|
|
|
.to_owned();
|
|
|
|
if input != "Puzzle inputs differ by user. Please log in to get your puzzle input." {
|
|
|
|
input
|
|
|
|
} else {
|
|
|
|
panic!("Invalid auth")
|
|
|
|
}
|
2023-12-04 23:17:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch_example(auth: String, day: u8, part: u8) -> (String, String) {
|
|
|
|
let html = reqwest::Client::new()
|
|
|
|
.get(format!("https://adventofcode.com/2023/day/{day}"))
|
|
|
|
.header("Cookie", format!("session={auth}"))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("Unable to make input request")
|
|
|
|
.text()
|
|
|
|
.await
|
|
|
|
.expect("Unable to parse string from input response");
|
|
|
|
|
|
|
|
let dom = Html::parse_document(&html);
|
|
|
|
let input_selector = Selector::parse("pre > code").unwrap();
|
|
|
|
let solution_selector = Selector::parse(match part {
|
|
|
|
1 => "article:nth-child(1) code > em",
|
|
|
|
2 => "article:nth-child(3) code > em",
|
|
|
|
_ => panic!(),
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let input = dom
|
|
|
|
.select(&input_selector)
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.inner_html()
|
|
|
|
.trim_end()
|
|
|
|
.to_string();
|
|
|
|
let solution = dom
|
|
|
|
.select(&solution_selector)
|
|
|
|
.last()
|
2023-12-05 00:03:50 -07:00
|
|
|
.expect("Unable to find example solution for part, is auth correct?")
|
2023-12-04 23:17:16 -07:00
|
|
|
.inner_html()
|
|
|
|
.trim_end()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
(input, solution)
|
|
|
|
}
|