Welcome everyone to the 2023 advent of code! Thank you all for stopping by and participating in it in programming.dev whether youre new to the event or doing it again.

This is an unofficial community for the event as no official spot exists on lemmy but ill be running it as best I can with Sigmatics modding as well. Ill be running a solution megathread every day where you can share solutions with other participants to compare your answers and to see the things other people come up with


Day 1: Trebuchet?!


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)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

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

🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Edit: Post has been unlocked after 6 minutes

  • CannotSleep420@lemmygrad.ml
    ·
    edit-2
    1 year ago

    I finally got my solutions done. I used rust. I feel like 114 lines (not including empty lines or driver code) for both solutions is pretty decent. If lemmy's code blocks are hard to read, I also put my solutions on github.

    use std::{
        cell::OnceCell,
        collections::{HashMap, VecDeque},
        ops::ControlFlow::{Break, Continue},
    };
    
    use crate::utils::read_lines;
    
    #[derive(Clone, Copy, PartialEq, Eq)]
    enum NumType {
        Digit,
        DigitOrWord,
    }
    
    #[derive(Clone, Copy, PartialEq, Eq)]
    enum FromDirection {
        Left,
        Right,
    }
    
    const WORD_NUM_MAP: OnceCell> = OnceCell::new();
    
    fn init_num_map() -> HashMap<&'static str, u8> {
        HashMap::from([
            ("one", b'1'),
            ("two", b'2'),
            ("three", b'3'),
            ("four", b'4'),
            ("five", b'5'),
            ("six", b'6'),
            ("seven", b'7'),
            ("eight", b'8'),
            ("nine", b'9'),
        ])
    }
    
    const MAX_WORD_LEN: usize = 5;
    
    fn get_digit<i>(mut bytes: I, num_type: NumType, from_direction: FromDirection) -> Option
    where
        I: Iterator,
    {
        let digit = bytes.try_fold(VecDeque::new(), |mut byte_queue, byte| {
            if byte.is_ascii_digit() {
                Break(byte)
            } else if num_type == NumType::DigitOrWord {
                if from_direction == FromDirection::Left {
                    byte_queue.push_back(byte);
                } else {
                    byte_queue.push_front(byte);
                }
    
                let word = byte_queue
                    .iter()
                    .map(|&amp;byte| byte as char)
                    .collect::();
    
                for &amp;key in WORD_NUM_MAP
                    .get_or_init(init_num_map)
                    .keys()
                    .filter(|k| k.len() &lt;= byte_queue.len())
                {
                    if word.contains(key) {
                        return Break(*WORD_NUM_MAP.get_or_init(init_num_map).get(key).unwrap());
                    }
                }
    
                if byte_queue.len() == MAX_WORD_LEN {
                    if from_direction == FromDirection::Left {
                        byte_queue.pop_front();
                    } else {
                        byte_queue.pop_back();
                    }
                }
    
                Continue(byte_queue)
            } else {
                Continue(byte_queue)
            }
        });
    
        if let Break(byte) = digit {
            Some(byte)
        } else {
            None
        }
    }
    
    fn process_digits(x: u8, y: u8) -> u16 {
        ((10 * (x - b'0')) + (y - b'0')).into()
    }
    
    fn solution(num_type: NumType) {
        if let Ok(lines) = read_lines("src/day_1/input.txt") {
            let sum = lines.fold(0_u16, |acc, line| {
                let line = line.unwrap_or_else(|_| String::new());
                let bytes = line.bytes();
                let left = get_digit(bytes.clone(), num_type, FromDirection::Left).unwrap_or(b'0');
                let right = get_digit(bytes.rev(), num_type, FromDirection::Right).unwrap_or(left);
    
                acc + process_digits(left, right)
            });
    
            println!("{sum}");
        }
    }
    
    pub fn solution_1() {
        solution(NumType::Digit);
    }
    
    pub fn solution_2() {
        solution(NumType::DigitOrWord);
    }
    ```</i>