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

  • madmo@programming.dev
    link
    fedilink
    arrow-up
    3
    ·
    26 days ago

    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)
    }