Spyke

Syndicated from the fediverse. Read and engage on the original instance.

View original on programming.dev
advent_of_codeยทAdvent Of CodebyCameronDev

Congratulations All - Leaderboard

Congrats to everyone who took part, and thank you to everyone who contributed here, in the solutions and visualization threads.

If anyone knows of a similar event that runs during the year, please do reach out, I would be happy to run more events like this.

Happy new year!

View original on programming.dev
35

4 replies

For a final bit of code to roast, here is the bot that posted the daily threads:

::: spoiler Bot code

use std::process::exit;
use chrono::{Datelike, Timelike};
use lemmy_client::lemmy_api_common::community::GetCommunity;
use lemmy_client::lemmy_api_common::person::Login;
use lemmy_client::lemmy_api_common::post::CreatePost;
use lemmy_client::{ClientOptions, LemmyClient};
use rand::seq::IndexedRandom;
use regex::Regex;

static TARGET_COMMUNITY: &str = "[email protected]";

async fn get_advent_emoji() -> String {
    let choices = ['๐ŸŽ„','๐ŸŽ','๐Ÿงฆ','๐Ÿ•ฏ','๐ŸŒŸ','โœจ','๐Ÿงธ','โ›„','โ˜ƒ','โ„','๐Ÿ”','๐ŸŽ…','๐Ÿคถ','๐Ÿง‘','๐ŸฆŒ','๐Ÿ‘ผ','๐Ÿ‘ช','๐Ÿฝ','๐Ÿฅ›','๐Ÿท','๐Ÿ—','๐Ÿฅง','๐Ÿฌ','๐Ÿซ','๐ŸŽถ','๐ŸŽถ','๐Ÿ””','๐ŸŽŠ','๐ŸŽ‰','๐Ÿ’ƒ'];
    choices.choose(&mut rand::rng()).unwrap().to_string()
}

async fn get_advent_title(year: i32, day: u32) -> String {
    let url = format!("https://adventofcode.com/%7B%7D/day/%7B%7D", year, day);

    let resp = reqwest::get(url).await.unwrap();
    let body = String::from_utf8(resp.bytes().await.unwrap().to_vec()).unwrap();

    let re = Regex::new(r"---(.*)---").unwrap();

    for (_, [line]) in re.captures_iter(&body).map(|c| c.extract()) {
        return line.trim().to_string();
    }

    panic!("could not find advent title - {body}");
}

#[tokio::main]
async fn main() {
    let time = chrono::Utc::now();
    let year = time.year();
    let day = time.day();

    if time.hour() != 5 {
        println!("Wrong time: {time}");
        exit(0);
    }

    let title = get_advent_title(year, day).await;
    println!("Title: {:#?}", title);

    post_to_lemmy(year, day, title).await;
}

async fn post_to_lemmy(year: i32, day:u32, challenge_title: String) {
    let emoji = get_advent_emoji().await;
    let post_title = format!("{emoji} - {year} DAY {day} SOLUTIONS - {emoji}");
    let post_body = format!("# {challenge_title}

## Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

## FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465");

    let mut client = LemmyClient::new(ClientOptions {
        domain: "programming.dev".to_string(),
        secure: true,
    });

    let auth = client.login(Login {
        username_or_email: "HARDCODED".to_string().into(),
        password: "HARDCODED".to_string().into(),
        totp_2fa_token: None,
    }).await.unwrap();

    client.headers_mut().insert("Authorization".to_string(),
                                format!("Bearer {}", auth.jwt.unwrap().to_string()));

    let community = client.get_community(GetCommunity {
        id: None,
        name: Some(TARGET_COMMUNITY.to_string()),
    }).await.unwrap();
    let community_id = community.community_view.community.id;

    client.create_post(CreatePost {
        name: post_title,
        community_id,
        url: None,
        body: Some(post_body),
        alt_text: None,
        honeypot: None,
        nsfw: None,
        language_id: None,
        custom_thumbnail: None,
    }).await.unwrap();

}

:::

5

I still have day 12 to do. Got stuck on day 10, refused to use third party solver and persevered. Day 11 was quite basic compared with 10.

3
Pyro
programming.dev

Even though it wasn't remotely in my control, I love my nice even score this year.

2

You reached the end

Congratulations All - Leaderboard | Spyke