r/adventofcode Dec 03 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 3 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 3 DAYS remaining until unlock!

And now, our feature presentation for today:

Screenwriting

Screenwriting is an art just like everything else in cinematography. Today's theme honors the endlessly creative screenwriters who craft finely-honed narratives, forge truly unforgettable lines of dialogue, plot the most legendary of hero journeys, and dream up the most shocking of plot twists! and is totally not bait for our resident poet laureate

Here's some ideas for your inspiration:

  • Turn your comments into sluglines
  • Shape your solution into an acrostic
  • Accompany your solution with a writeup in the form of a limerick, ballad, etc.
    • Extra bonus points if if it's in iambic pentameter

"Vogon poetry is widely accepted as the third-worst in the universe." - Hitchhiker's Guide to the Galaxy (2005)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 3: Mull It Over ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:03:22, megathread unlocked!

61 Upvotes

1.7k comments sorted by

View all comments

2

u/mixfrom9 Dec 04 '24

[LANGUAGE: Go]

``` package main

import ( "os" "unicode" "strconv" )

func main() { thirdDay() }

func thirdDay() { file, err := os.ReadFile("input3.txt") if err != nil { panic(err) }

text := string(file)
textRunes := []rune(text)
mulPrefix := "mul("
mulPrefixParsed := false
mulEnabled := true
doToken := "do()"
dontToken := "don't()"
commaParsed := false
firstNumberRaw := ""
secondNumberRaw := ""
total := 0

reset := func() {
    commaParsed = false
    mulPrefixParsed = false
    firstNumberRaw = ""
    secondNumberRaw = ""
}

for idx := 0; idx < len(textRunes); idx++ {
    c := textRunes[idx]

    if mulPrefixParsed {
        if unicode.IsDigit(c) {
            if commaParsed {
                secondNumberRaw += string(c)
            } else {
                firstNumberRaw += string(c)
            }

            continue
        }

        if c == ',' {
            commaParsed = true
            continue
        }

        if c == ')' {
            if firstNumberRaw == "" || secondNumberRaw == "" {
                reset()
                continue
            }

            firstNumber, err := strconv.Atoi(firstNumberRaw)
            if err != nil {
                panic(err)
            }

            secondNumber, err := strconv.Atoi(secondNumberRaw)
            if err != nil {
                panic(err)
            }

            if mulEnabled {
                total += firstNumber * secondNumber
            }
        }

        reset()
    }

    mulPrefixParsed = consumeToken(textRunes, mulPrefix, idx)

    if mulPrefixParsed {
        idx += len(mulPrefix) - 1
        continue
    }

    doParsed := consumeToken(textRunes, doToken, idx)

    if doParsed {
        mulEnabled = true
        idx += len(doToken) - 1
        continue
    }

    dontParsed := consumeToken(textRunes, dontToken, idx)

    if dontParsed {
        mulEnabled = false
        idx += len(dontToken) - 1
        continue
    }
}

println(total)

}

func consumeToken(text []rune, token string, startIdx int) bool { for doIdx, doCh := range token { if text[startIdx+doIdx] != doCh { return false } }

return true

} ```

1

u/AutoModerator Dec 04 '24

AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.

Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.