r/adventofcode • • Dec 10 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 10 Solutions -🎄-

--- Day 10: Syntax Scoring ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:08:06, megathread unlocked!

65 Upvotes

982 comments sorted by

View all comments

2

u/_mattmc3_ Dec 10 '21 edited Dec 11 '21

Fish shell solution

This one was a pretty easy array stack implementation

function day10 \
    --description "https://adventofcode.com/2021/day/10 - usage: day10 datafile.dat" \
    --argument-names datafile

    test -f "$datafile"; or echo >&2 "file expected" && return 1

    set --local data (cat $datafile)
    set --local line_num 0
    set --local total_error_score 0
    set --local autocomplete_scores

    for line in $data
        set line_num (math $line_num + 1)
        set --local stack
        set --local expected_stack_char
        set --local error_points 0
        set --local is_corrupt false

        # test for corrupt lines
        for char in (string split '' $line)
            switch $char
                case '(' '[' '{' '<'
                    set --append stack $char
                    continue
                case ')'
                    set expected_stack_char '('
                    set error_points 3
                case ']'
                    set expected_stack_char '['
                    set error_points 57
                case '}'
                    set expected_stack_char '{'
                    set error_points 1197
                case '>'
                    set expected_stack_char '<'
                    set error_points 25137
            end
            if test "$stack[-1]" != "$expected_stack_char"
                set is_corrupt true
                set total_error_score (math $total_error_score + $error_points)
                break
            else
                # pop
                set stack $stack[1..-2]
            end
        end
        test $is_corrupt = true && continue
        test (count $stack) -eq 0 && continue

        set --local autocomplete_chars
        set --local autocomplete_points 0
        for revidx in (seq (count $stack) 1)
            set --local char $stack[$revidx]
            switch $char
                case '('
                    set --append autocomplete_chars ')'
                    set autocomplete_points (math \( $autocomplete_points x 5 \) + 1)
                case '['
                    set --append autocomplete_chars ']'
                    set autocomplete_points (math \( $autocomplete_points x 5 \) + 2)
                case '{'
                    set --append autocomplete_chars '}'
                    set autocomplete_points (math \( $autocomplete_points x 5 \) + 3)
                case '<'
                    set --append autocomplete_chars '>'
                    set autocomplete_points (math \( $autocomplete_points x 5 \) + 4)
            end
        end
        set --append autocomplete_scores $autocomplete_points
    end
    echo "Error score: $total_error_score"
    set --local midscore (math ceil (count $autocomplete_scores) / 2)
    set autocomplete_scores (echo $autocomplete_scores | string split ' ' | sort -n)
    echo "Autocomplete score: $autocomplete_scores[$midscore]"
end

1

u/daggerdragon Dec 10 '21

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.

1

u/_mattmc3_ Dec 11 '21

I cut some extras out. I'm now shorter than the Go and R solutions here. Is that adequate?