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 6[1m[37mAll of them?![0m 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 [1m[37mchunks[0m. 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 [1m[37mopen[0m and [1m[37mclose[0m 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 [1m[37mincomplete[0m, but others are [1m[37mcorrupted[0m. Find and discard the corrupted lines first. 28 29A corrupted line is one where a chunk [1m[37mcloses with the wrong character[0m - 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 67[1m[37mfirst illegal character[0m 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 = [1m[37m6[0m points), an illegal ] was found once 80([1m[37m57[0m points), an illegal } was found once ([1m[37m1197[0m points), and an illegal > was found once 81([1m[37m25137[0m points). So, the total syntax error score for this file is 6+57+1197+25137 = 82[1m[37m26397[0m points! 83 84Find the first illegal character in each corrupted line of the navigation subsystem. [1m[37mWhat is the 85total syntax error score for those errors?[0m 86 87