aoc-2021-rust

Advent of Code 2021 Solutions in Rust
git clone https://git.sinitax.com/sinitax/aoc-2021-rust
Log | Files | Refs | README | sfeed.txt

part1 (3309B)


      1--- Day 10: Syntax Scoring ---
      2
      3You ask the submarine to determine the best route out of the deep-sea cave, but it only replies:
      4
      5Syntax error in navigation subsystem on line: all of them
      6All of them?! The damage is worse than you thought. You bring up a copy of the navigation subsystem
      7(your puzzle input).
      8
      9The navigation subsystem syntax is made of several lines containing chunks. There are one or more
     10chunks on each line, and chunks contain zero or more other chunks. Adjacent chunks are not separated
     11by any delimiter; if one chunk stops, the next chunk (if any) can immediately start. Every chunk
     12must open and close with one of four legal pairs of matching characters:
     13
     14
     15 - If a chunk opens with (, it must close with ).
     16
     17 - If a chunk opens with [, it must close with ].
     18
     19 - If a chunk opens with {, it must close with }.
     20
     21 - If a chunk opens with <, it must close with >.
     22
     23
     24So, () is a legal chunk that contains no other chunks, as is []. More complex but valid chunks
     25include ([]), {()()()}, <([{}])>, [<>({}){}[([])<>]], and even (((((((((()))))))))).
     26
     27Some lines are incomplete, but others are corrupted. Find and discard the corrupted lines first.
     28
     29A corrupted line is one where a chunk closes with the wrong character - that is, where the
     30characters it opens and closes with do not form one of the four legal pairs listed above.
     31
     32Examples of corrupted chunks include (], {()()()>, (((()))}, and <([]){()}[{}]). Such a chunk can
     33appear anywhere within a line, and its presence causes the whole line to be considered corrupted.
     34
     35For example, consider the following navigation subsystem:
     36
     37[({(<(())[]>[[{[]{<()<>>
     38[(()[<>])]({[<{<<[]>>(
     39{([(<{}[<>[]}>{[]{[(<()>
     40(((({<>}<{<{<>}{[]{[]{}
     41[[<[([]))<([[{}[[()]]]
     42[{[{({}]{}}([{[{{{}}([]
     43{<[[]]>}<{[{[{[]{()[[[]
     44[<(<(<(<{}))><([]([]()
     45<{([([[(<>()){}]>(<<{{
     46<{([{{}}[<[[[<>{}]]]>[]]
     47
     48Some of the lines aren't corrupted, just incomplete; you can ignore these lines for now. The
     49remaining five lines are corrupted:
     50
     51
     52 - {([(<{}[<>[]}>{[]{[(<()> - Expected ], but found } instead.
     53
     54 - [[<[([]))<([[{}[[()]]] - Expected ], but found ) instead.
     55
     56 - [{[{({}]{}}([{[{{{}}([] - Expected ), but found ] instead.
     57
     58 - [<(<(<(<{}))><([]([]() - Expected >, but found ) instead.
     59
     60 - <{([([[(<>()){}]>(<<{{ - Expected ], but found > instead.
     61
     62
     63Stop at the first incorrect closing character on each corrupted line.
     64
     65Did you know that syntax checkers actually have contests to see who can get the high score for
     66syntax errors in a file? It's true! To calculate the syntax error score for a line, take the
     67first illegal character on the line and look it up in the following table:
     68
     69
     70 - ): 3 points.
     71
     72 - ]: 57 points.
     73
     74 - }: 1197 points.
     75
     76 - >: 25137 points.
     77
     78
     79In the above example, an illegal ) was found twice (2*3 = 6 points), an illegal ] was found once
     80(57 points), an illegal } was found once (1197 points), and an illegal > was found once
     81(25137 points). So, the total syntax error score for this file is 6+57+1197+25137 =
     8226397 points!
     83
     84Find the first illegal character in each corrupted line of the navigation subsystem. What is the
     85total syntax error score for those errors?
     86
     87