Skip to content

Instantly share code, notes, and snippets.

@NangiDev
Created December 7, 2022 19:03
Show Gist options
  • Save NangiDev/2c6f82d2128fe33c613740ce2e68c97a to your computer and use it in GitHub Desktop.
Save NangiDev/2c6f82d2128fe33c613740ce2e68c97a to your computer and use it in GitHub Desktop.
A test on how to fetch the example input for Advent of Code
[package]
name = "rust_aoc_test_fetch"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = "0.11.13"
tokio = { version = "1.23.0", features = ["full"] }
#[tokio::main]
async fn main() {
// Fetch body from AoC for current day
let aoc_body = reqwest::get("https://adventofcode.com/2022/day/7").await.unwrap().text().await.unwrap();
let phrase = "For example"; // Seems to be the phrase used before every example input <code> tag
let code_after_phrase = aoc_body.split(phrase).collect::<Vec<&str>>()[1]; // The code snippet we want should be second element in vec
let code_tag = "code>";
let code_missing_tag_part = "</";
let code_snippet = &code_after_phrase.split(code_tag) // split on 'code>' helps us keep just the code snippet in same element
.map(|f| f.replace(code_missing_tag_part, "\n")) // replace the '</' left from <code> end tag </code>. Add newline to match how the puzzle input looks
.collect::<Vec<String>>()[1]; // Our desired code snippet should be the second element in vec
println!("'{}'", &code_snippet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment