this post was submitted on 03 Dec 2024
24 points (100.0% liked)

Advent Of Code

920 readers
2 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 3: Mull It Over

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 3 points 1 month ago (1 children)

Rust with nom parser

Decided to give it a go with the nom parser (first time using this crate). Turned out quite nicely. Had some issues with the alt combinator: All alternatives have to return the same type, using a enum to wrap all options did the trick.

use memmap2::Mmap;
use nom::{
    branch::alt, bytes::complete::*, character::complete::*, combinator::map, multi::many_till,
    sequence::tuple, AsBytes, IResult,
};

#[derive(Debug)]
enum Token {
    Do,
    Dont,
    Mul(u64, u64),
}

fn main() -> anyhow::Result<()> {
    let file = std::fs::File::open("input.txt")?;
    let mmap = unsafe { Mmap::map(&file)? };

    let mut sum_part1 = 0;
    let mut sum_part2 = 0;
    let mut enabled = true;

    let mut cursor = mmap.as_bytes();
    while let Ok(token) = parse(cursor) {
        match token.1 .1 {
            Token::Do => enabled = true,
            Token::Dont => enabled = false,
            Token::Mul(left, right) => {
                let prod = left * right;
                sum_part1 += prod;
                if enabled {
                    sum_part2 += prod;
                }
            }
        }

        cursor = token.0;
    }

    println!("part1: {} part2: {}", sum_part1, sum_part2);

    Ok(())
}

type ParseResult<'a> =
    Result<(&'a [u8], (Vec<char>, Token)), nom::Err<nom::error::Error<&'a [u8]>>>;

fn parse(input: &[u8]) -> ParseResult {
    many_till(
        anychar,
        alt((
            map(doit, |_| Token::Do),
            map(dont, |_| Token::Dont),
            map(mul, |el| Token::Mul(el.2, el.4)),
        )),
    )(input)
}

fn doit(input: &[u8]) -> IResult<&[u8], &[u8]> {
    tag("do()")(input)
}

fn dont(input: &[u8]) -> IResult<&[u8], &[u8]> {
    tag("don't()")(input)
}

type ParsedMulResult<'a> = (&'a [u8], &'a [u8], u64, &'a [u8], u64, &'a [u8]);

fn mul(input: &[u8]) -> IResult<&[u8], ParsedMulResult> {
    tuple((tag("mul"), tag("("), u64, tag(","), u64, tag(")")))(input)
}
[โ€“] [email protected] 1 points 1 month ago

doit

I had to check if do was reserved since I couldn't remember seeing it in the language, and it's in the reserved for future use section.