Skip Navigation

🍷 - 2024 DAY 3 SOLUTIONS -🍷

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

54 comments
  • I started poking at doing a proper lexer/parser, but then I thought about how early in AoC it is and how low the chance is that the second part will require proper parsing.

    So therefore; hello regex my old friend, I've come to talk with you again.

  • Uiua

    Uses experimental feature of fold to track the running state of do/don't.

    [edit] Slightly re-written to make it less painful :-) Try it online!

     undefined
        
    # Experimental!
    DataP₁       ← $ xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
    DataP₂       ← $ xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
    GetMul       ← $ mul\((\d{1,3}),(\d{1,3})\)
    GetMulDoDont ← $ mul\(\d{1,3},\d{1,3}\)|do\(\)|don\'t\(\)
    
    &p/+≡(/×≡⋕↘1)regexGetMul DataP₁ # Part 1
    
    # Build an accumulator to track running state of do/don't
    Filter ← ↘1⊂:∧(⍣(0 °"don"|1 °"do("|.◌)) :1≡(↙3°□)
    ≡⊢ regex GetMulDoDont DataP₂
    ▽⊸≡◇(≍"mul"↙3)▽⊸Filter      # Apply Filter, remove the spare 'do's
    &p/+≡◇(/×≡◇⋕↘1⊢regexGetMul) # Get the digits and multiply, sum.
    
      
  • Factor

     undefined
        
    : get-input ( -- corrupted-input )
      "vocab:aoc-2024/03/input.txt" utf8 file-contents ;
    
    : get-muls ( corrupted-input -- instructions )
      R/ mul\(\d+,\d+\)/ all-matching-subseqs ;
    
    : process-mul ( instruction -- n )
      R/ \d+/ all-matching-subseqs
      [ string>number ] map-product ;
    
    : solve ( corrupted-input -- n )
      get-muls [ process-mul ] map-sum ;
    
    : part1 ( -- n )
      get-input solve ;
    
    : part2 ( -- n )
      get-input
      R/ don't\(\)(.|\n)*?do\(\)/ split concat
      R/ don't\(\)(.|\n)*/ "" re-replace
      solve ;
    
      
  • J

    We can take advantage of the manageable size of the input to avoid explicit looping and mutable state; instead, construct vectors which give, for each character position in the input, the position of the most recent do() and most recent don't(); for part 2 a multiplication is enabled if the position of the most recent do() (counting start of input as 0) is greater than that of the most recent don't() (counting start of input as minus infinity).

     undefined
        
    load 'regex'
    
    raw =: fread '3.data'
    mul_matches =: 'mul\(([[:digit:]]{1,3}),([[:digit:]]{1,3})\)' rxmatches raw
    
    NB. a b sublist y gives elements [a..b) of y
    sublist =: ({~(+i.)/)~"1 _
    
    NB. ". is number parsing
    mul_results =: */"1 ". (}."2 mul_matches) sublist raw
    result1 =: +/ mul_results
    
    do_matches =: 'do\(\)' rxmatches raw
    dont_matches =: 'don''t\(\)' rxmatches raw
    match_indices =: (<0 0) & {"2
    do_indices =: 0 , match_indices do_matches  NB. start in do mode
    dont_indices =: match_indices dont_matches
    NB. take successive diffs, then append length from last index to end of string
    run_lengths =: (}. - }:) , (((#raw) & -) @: {:)
    do_map =: (run_lengths do_indices) # do_indices
    dont_map =: (({. , run_lengths) dont_indices) # __ , dont_indices
    enabled =: do_map > dont_map
    result2 =: +/ ((match_indices mul_matches) { enabled) * mul_results
    
      
  • 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.

     undefined
        
    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)
    }
    
      
  • Gleam

    Struggled with the second part as I am still very new to this very cool language, but got there after scrolling for some inspiration.

     gleam
        
    import gleam/int
    import gleam/io
    import gleam/list
    import gleam/regex
    import gleam/result
    import gleam/string
    import simplifile
    
    pub fn main() {
      let assert Ok(data) = simplifile.read("input.in")
      part_one(data) |> io.debug
      part_two(data) |> io.debug
    }
    
    fn part_one(data) {
      let assert Ok(multiplication_pattern) =
        regex.from_string("mul\\(\\d{1,3},\\d{1,3}\\)")
      let assert Ok(digit_pattern) = regex.from_string("\\d{1,3},\\d{1,3}")
      let multiplications =
        regex.scan(multiplication_pattern, data)
        |> list.flat_map(fn(reg) {
          regex.scan(digit_pattern, reg.content)
          |> list.map(fn(digits) {
            digits.content
            |> string.split(",")
            |> list.map(fn(x) { x |> int.parse |> result.unwrap(0) })
            |> list.reduce(fn(a, b) { a * b })
            |> result.unwrap(0)
          })
        })
        |> list.reduce(fn(a, b) { a + b })
        |> result.unwrap(0)
    }
    
    fn part_two(data) {
      let data = "do()" <> string.replace(data, "\n", "") <> "don't()"
      let assert Ok(pattern) = regex.from_string("do\\(\\).*?don't\\(\\)")
      regex.scan(pattern, data)
      |> list.map(fn(input) { input.content |> part_one })
      |> list.reduce(fn(a, b) { a + b })
    }
    
      
  • Python

    After a bunch of fiddling yesterday and today I finally managed to arrive at a regex-only solution for part 2. That re.DOTALL is crucial here.

     python
        
    import re
    from pathlib import Path
    
    
    def parse_input_one(input: str) -> list[tuple[int]]:
        p = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)")
        return [(int(m[0]), int(m[1])) for m in p.findall(input)]
    
    
    def parse_input_two(input: str) -> list[tuple[int]]:
        p = re.compile(r"don't\(\).*?do\(\)|mul\((\d{1,3}),(\d{1,3})\)", re.DOTALL)
        return [(int(m[0]), int(m[1])) for m in p.findall(input) if m[0] and m[1]]
    
    
    def part_one(input: str) -> int:
        pairs = parse_input_one(input)
        return sum(map(lambda v: v[0] * v[1], pairs))
    
    
    def part_two(input: str) -> int:
        pairs = parse_input_two(input)
        return sum(map(lambda v: v[0] * v[1], pairs))
    
    
    if __name__ == "__main__":
        input = Path("input").read_text("utf-8")
        print(part_one(input))
        print(part_two(input))
    
      
  • Nim

     nim
        
    import ../aoc, re, sequtils, strutils, math
    
    proc mulsum*(line:string):int=
      let matches = line.findAll(re"mul\([0-9]{1,3},[0-9]{1,3}\)")
      let pairs = matches.mapIt(it[4..^2].split(',').map(parseInt))
      pairs.mapIt(it[0]*it[1]).sum
    
    proc filter*(line:string):int=
      var state = true;
      var i=0
      while i < line.len:
        if state:
          let off = line.find("don't()", i)
          if off == -1:
            break
          result += line[i..<off].mulsum
          i = off+6
          state = false
        else:
          let on = line.find("do()", i)
          if on == -1:
            break
          i = on+4
          state = true
          
      if state:
        result += line[i..^1].mulsum
    
    proc solve*(input:string): array[2,int] =
      #part 1&2
      result = [input.mulsum, input.filter]
    
    
      

    I had a nicer solution in mind for part 2, but for some reason nre didn't want to work for me, and re couldn't give me the start/end or all results, so I ended up doing this skip/toggle approach.

    Also initially I was doing it line by line out of habit from other puzzles, but then ofc the don't()s didn't propagate to the next line.

  • Rust

     rust
        
    use crate::utils::read_lines;
    
    pub fn solution1() {
        let lines = read_lines("src/day3/input.txt");
        let sum = lines
            .map(|line| {
                let mut sum = 0;
                let mut command_bytes = Vec::new();
                for byte in line.bytes() {
                    match (byte, command_bytes.as_slice()) {
                        (b')', [.., b'0'..=b'9']) => {
                            handle_mul(&mut command_bytes, &mut sum);
                        }
                        _ if matches_mul(byte, &command_bytes) => {
                            command_bytes.push(byte);
                        }
                        _ => {
                            command_bytes.clear();
                        }
                    }
                }
    
                sum
            })
            .sum::<usize>();
    
        println!("Sum of multiplication results = {sum}");
    }
    
    pub fn solution2() {
        let lines = read_lines("src/day3/input.txt");
    
        let mut can_mul = true;
        let sum = lines
            .map(|line| {
                let mut sum = 0;
                let mut command_bytes = Vec::new();
                for byte in line.bytes() {
                    match (byte, command_bytes.as_slice()) {
                        (b')', [.., b'0'..=b'9']) if can_mul => {
                            handle_mul(&mut command_bytes, &mut sum);
                        }
                        (b')', [b'd', b'o', b'(']) => {
                            can_mul = true;
                            command_bytes.clear();
                        }
                        (b')', [.., b't', b'(']) => {
                            can_mul = false;
                            command_bytes.clear();
                        }
                        _ if matches_do_or_dont(byte, &command_bytes)
                            || matches_mul(byte, &command_bytes) =>
                        {
                            command_bytes.push(byte);
                        }
                        _ => {
                            command_bytes.clear();
                        }
                    }
                }
    
                sum
            })
            .sum::<usize>();
    
        println!("Sum of enabled multiplication results = {sum}");
    }
    
    fn matches_mul(byte: u8, command_bytes: &[u8]) -> bool {
        matches!(
            (byte, command_bytes),
            (b'm', [])
                | (b'u', [.., b'm'])
                | (b'l', [.., b'u'])
                | (b'(', [.., b'l'])
                | (b'0'..=b'9', [.., b'(' | b'0'..=b'9' | b','])
                | (b',', [.., b'0'..=b'9'])
        )
    }
    
    fn matches_do_or_dont(byte: u8, command_bytes: &[u8]) -> bool {
        matches!(
            (byte, command_bytes),
            (b'd', [])
                | (b'o', [.., b'd'])
                | (b'n', [.., b'o'])
                | (b'\'', [.., b'n'])
                | (b'(', [.., b'o' | b't'])
                | (b't', [.., b'\''])
        )
    }
    
    fn handle_mul(command_bytes: &mut Vec<u8>, sum: &mut usize) {
        let first_num_index = command_bytes
            .iter()
            .position(u8::is_ascii_digit)
            .expect("Guarunteed to be there");
        let comma_index = command_bytes
            .iter()
            .position(|&c| c == b',')
            .expect("Guarunteed to be there.");
    
        let num1 = bytes_to_num(&command_bytes[first_num_index..comma_index]);
        let num2 = bytes_to_num(&command_bytes[comma_index + 1..]);
    
        *sum += num1 * num2;
        command_bytes.clear();
    }
    
    fn bytes_to_num(bytes: &[u8]) -> usize {
        bytes
            .iter()
            .rev()
            .enumerate()
            .map(|(i, digit)| (*digit - b'0') as usize * 10usize.pow(i as u32))
            .sum::<usize>()
    }
    
    
      

    Definitely not my prettiest code ever. It would probably look nicer if I used regex or some parsing library, but I took on the self-imposed challenge of not using third party libraries. Also, this is already further than I made it last year!

  • Raku

     undefined
        
    sub MAIN($input) {
        grammar Muls {
            token TOP { .*? <mul>+%.*? .* }
            token mul { "mul(" <number> "," <number> ")" }
            token number { \d+ }
        }
    
        my $parsedMuls = Muls.parsefile($input);
        my @muls = $parsedMuls<mul>.map({.<number>».Int});
        my $part-one-solution = @muls.map({[*] $_.List}).sum;
        say "part 1: $part-one-solution";
    
        grammar EnabledMuls {
            token TOP { .*? [<.disabled> || <mul>]+%.*? .* }
            token mul { "mul(" <number> "," <number> ")" }
            token number { \d+ }
            token disabled { "don't()" .*? ["do()" || $] }
        }
    
        my $parsedEnabledMuls = EnabledMuls.parsefile($input);
        my @enabledMuls = $parsedEnabledMuls<mul>.map({.<number>».Int});
        my $part-two-solution = @enabledMuls.map({[*] $_.List}).sum;
        say "part 2: $part-two-solution";
    }
    
    
      

    github

  • Julia

    I did not try to make my solution concise and kept separate code for part 1 and part 2 with test cases for both to check if I broke anything. But after struggling with Day 2 I am quite pleased to have solved Day 3 with only a little bugfixing.

     undefined
        
    function calcLineResult(line::String)
        lineResult::Int = 0
        enabled::Bool = true
        for i=1 : length(line)
            line[i]!='m' ? continue : (i<length(line) ? i+=1 : continue)
            line[i]!='u' ? continue : (i<length(line) ? i+=1 : continue)
            line[i]!='l' ? continue : (i<length(line) ? i+=1 : continue)
            line[i]!='(' ? continue : (i<length(line) ? i+=1 : continue)
            num1Str::String = ""
            while line[i] in ['0','1','2','3','4','5','6','7','8','9'] #should check for length of digits < 3, but works without
                num1Str = num1Str*line[i]; (i<length(line) ? i+=1 : continue)
            end
            line[i]!=',' ? continue : (i<length(line) ? i+=1 : continue)
            num2Str::String = ""
            while line[i] in ['0','1','2','3','4','5','6','7','8','9'] #should check for length of digits < 3, but works without
                num2Str = num2Str*line[i]; (i<length(line) ? i+=1 : continue)
            end
            line[i]==')' ? lineResult+=parse(Int,num1Str)*parse(Int,num2Str) : continue
        end
        return lineResult
    end
    
    function calcLineResultWithEnabling(line::String,enabled::Bool)
        lineResult::Int = 0
        for i=1 : length(line)
            if enabled && line[i] == 'm'
                i<length(line) ? i += 1 : continue
                line[i]!='u' ? continue : (i<length(line) ? i+=1 : continue)
                line[i]!='l' ? continue : (i<length(line) ? i+=1 : continue)
                line[i]!='(' ? continue : (i<length(line) ? i+=1 : continue)
                num1Str::String = ""
                while line[i] in ['0','1','2','3','4','5','6','7','8','9']
                    num1Str = num1Str*line[i]; (i<length(line) ? i+=1 : continue)
                end
                line[i]!=',' ? continue : (i<length(line) ? i+=1 : continue)
                num2Str::String = ""
                while line[i] in ['0','1','2','3','4','5','6','7','8','9']
                    num2Str = num2Str*line[i]; (i<length(line) ? i+=1 : continue)
                end
                line[i]==')' ? lineResult+=parse(Int,num1Str)*parse(Int,num2Str) : continue
            elseif line[i] == 'd'
                i<length(line) ? i += 1 : continue
                line[i]!='o' ? continue : (i<length(line) ? i+=1 : continue)
                if line[i] == '('
                    i<length(line) ? i += 1 : continue
                    line[i]==')' ? enabled=true : continue
                    #@info i,line[i-3:i]
                elseif line[i] == 'n'
                    i<length(line) ? i += 1 : continue
                    line[i]!=''' ? continue : (i<length(line) ? i+=1 : continue)
                    line[i]!='t' ? continue : (i<length(line) ? i+=1 : continue)
                    line[i]!='(' ? continue : (i<length(line) ? i+=1 : continue)
                    line[i]==')' ? enabled=false : continue
                    #@info i,line[i-6:i]
                else
                    nothing
                end
            end
        end
        return lineResult,enabled
    end
    
    function calcMemoryResult(inputFile::String,useEnabling::Bool)
        memoryRes::Int = 0
        f = open(inputFile,"r")
        lines = readlines(f)
        close(f)
        enabled::Bool = true
        for line in lines
            if useEnabling
                lineRes::Int,enabled = calcLineResultWithEnabling(line,enabled)
                memoryRes += lineRes
            else
                memoryRes += calcLineResult(line)
            end
        end
        return memoryRes
    end
    
    if abspath(PROGRAM_FILE) == @__FILE__
        @info "Part 1"
        @debug "checking test input"
        inputFile::String = "day03InputTest"
        memoryRes::Int = calcMemoryResult(inputFile,false)
        try
            @assert memoryRes==161
        catch e
            throw(ErrorException("$e memoryRes=$memoryRes"))
        end
        @debug "test input ok"
        @debug "running real input"
        inputFile::String = "day03Input"
        memoryRes::Int = calcMemoryResult(inputFile,false)
        try
            @assert memoryRes==153469856
        catch e
            throw(ErrorException("$e memoryRes=$memoryRes"))
        end
        println("memory result: $memoryRes")
        @debug "real input ok"
    
        @info "Part 2"
        @debug "checking test input"
        inputFile::String = "day03InputTest"
        memoryRes::Int = calcMemoryResult(inputFile,true)
        try
            @assert memoryRes==48
        catch e
            throw(ErrorException("$e memoryRes=$memoryRes"))
        end
        @debug "test input ok"
        @debug "running real input"
        inputFile::String = "day03Input"
        memoryRes::Int = calcMemoryResult(inputFile,true)
        try
            @assert memoryRes==77055967
        catch e
            throw(ErrorException("$e memoryRes=$memoryRes"))
        end
        println("memory result: $memoryRes")
        @debug "real input ok"
    
    end
    
    
      
  • Rust feat. pest

    No Zalgo here! I wasted a huge amount of time by not noticing that the second part's example input was different - my code worked fine but my test failed 🤦‍♂️

    pest.rs is lovely, although part two made my PEG a bit ugly.

     pest
        
    part1    =  { SOI ~ (mul_expr | junk)+ ~ EOI }
    part2    =  { (enabled | disabled)+ ~ EOI }
    mul_expr =  { "mul(" ~ number ~ "," ~ number ~ ")" }
    number   =  { ASCII_DIGIT{1,3} }
    junk     = _{ ASCII }
    on       = _{ "do()" }
    off      = _{ "don't()" }
    enabled  = _{ (SOI | on) ~ (!(off) ~ (mul_expr | junk))+ }
    disabled = _{ off ~ (!(on) ~ junk)+ }
    
      
     rust
        
    use std::fs;
    
    use color_eyre::eyre;
    use pest::Parser;
    use pest_derive::Parser;
    
    #[derive(Parser)]
    #[grammar = "memory.pest"]
    pub struct MemoryParser;
    
    fn parse(input: &str, rule: Rule) -> eyre::Result<usize> {
        let sum = MemoryParser::parse(rule, input)?
            .next()
            .expect("input must be ASCII")
            .into_inner()
            .filter(|pair| pair.as_rule() == Rule::mul_expr)
            .map(|pair| {
                pair.into_inner()
                    .map(|num| num.as_str().parse::<usize>().unwrap())
                    .product::<usize>()
            })
            .sum();
        Ok(sum)
    }
    
    fn part1(filepath: &str) -> eyre::Result<usize> {
        let input = fs::read_to_string(filepath)?;
        parse(&input, Rule::part1)
    }
    
    fn part2(filepath: &str) -> eyre::Result<usize> {
        let input = fs::read_to_string(filepath)?;
        parse(&input, Rule::part2)
    }
    
    fn main() -> eyre::Result<()> {
        color_eyre::install()?;
    
        let part1 = part1("d03/input.txt")?;
        let part2 = part2("d03/input.txt")?;
        println!("Part 1: {part1}\nPart 2: {part2}");
        Ok(())
    }
    
      
  • I did part 2 live with the python interactive shell. I deleted all the stuff where I was just exploring ideas.

    part 1:

     undefined
        
    import re
    
    def multiply_and_add(data: "str") -> int:
        digit_matches = re.findall(r"mul\(\d{0,3},\d{0,3}\)", data)
        result = 0
        for _ in digit_matches:
            first = _.split("(")[1].split(")")[0].split(",")[0]
            second = _.split("(")[1].split(")")[0].split(",")[1]
            result += int(first) * int(second)
    
        return result
    
    with open("input") as file:
        data = file.read()
    
    
    answer = multiply_and_add(data)
    print(answer)
    
      

    part 2:

     undefined
        
    Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import solution2
    <re.Match object; span=(647, 651), match='do()'>
    >>> from solution2 import *
    >>> split_on_dont = data.split("don't()")
    >>> valid = []
    >>> valid.append(split_on_dont[0])
    >>> for substring in split_on_dont[1:]:
    ...     subsubstrings = substring.split("do()", maxsplit=1)
    ...     for subsubstring in subsubstrings[1:]:
    ...             valid.append(subsubstring)
    ...
    >>> answer = 0
    >>> for _ in valid:
    ...     answer += multiply_and_add(_)
    ...
    >>> answer
    103811193
    
      
  • Smalltalk

    I wrote matchesActual cause all of smalltalk'sstupid matchesDo: or whatever don't give you the actual match with captures, only substrings (that wasted a good 40 minutes).

    Also smalltalk really needs an index operator

     smalltalk
        
    day3p1: input
      | reg sum |
        reg := 'mul\((\d\d?\d?),(\d\d?\d?)\)' asRegex.
        sum := 0.
        
        reg matchesActual: input do: [ :m | " + sum at end cause operator precedence"
            sum := (m subexpression: 2) asInteger * (m subexpression: 3) asInteger + sum 
        ].
        
        ^ sum.
    
    
      
     smalltalk
        
    day3p2: input
      | reg sum do val |
    
        reg := 'do(\:?n''t)?\(\)|mul\((\d{1,3}),(\d{1,3})\)' asRegex.
        sum := 0.
        do := true.
        reg matchesActual: input do: [ :m |
            val := m subexpression: 1.
            (val at: 1) = $d ifTrue: [ do := (val size < 5) ]
            ifFalse: [ 
                do ifTrue: [ 
                    sum := (m subexpression: 2) asInteger * (m subexpression: 3) asInteger + sum.
            ].  ].
        ].
        
        ^ sum.
    
    
      
54 comments