Browse Source

Day 10

main
Terrana Ninetailed 8 months ago
parent
commit
7e6b9a2b09
  1. 94
      resources/input/day10/input
  2. 90
      src/net/ninetailed/adventofcode2021/Day10.java
  3. 40
      test/net/ninetailed/adventofcode2021/TestDay10.java

94
resources/input/day10/input

@ -0,0 +1,94 @@
[{<<<{{[(<{{{{[]()}{[]()}}<[[]<>]<[]()>>}([[[][]]]<[{}<>]((){})>)}>)[<([<<(){}>{(){}}>[[<>{}](<>[
<<{(<({[<{{<[<()()><{}{}>][[()[]][(){}]]>((<[]<>>(<>{}))((<>{}){<>()}))}<{({{}}[<>{}])({{}{}}{{}
(<[(<<[((({<[{()[]}]>}{[[[{}[]]{{}{}}][{{}()}<<>()>]]})))<[([(<[{}{}]>{<<>{}>})<((<>()){[][]})<{<
{({<[[[(<{{<<[[]()]>]}{<({<>{}}([]<>))[({}{}){()()}]>((<[][]>{[]()})[(()<>)[{}{}]])}}>){[<[{[{[]}{<><>
(<{([(<[{[{<[<()<>>({}[])]>}][<{({<>}(<><>))}({[<>{}](<>())}{[{}()]<()<>>})><[<<{}<>><()[])>[<{}[]
{<{[({<<<[[<(({}))>(([<><>][<><>])<<()()][{}{}]>)]][<[{<{}{}>(<>[])}](({{}[]}(()()))<<[]()>
(<{[[(({(<([([[]{}][()[]]){{()[]}[<><>]}](<(<><>){[][]}>({{}{}}{{}()})))>){((([{()}[{}[]]]))[<[
{<[(<([<{(<[[({}{})({}())]]>[(<<{}<>>([]())><({}[]}{()[]}>)([<(){}>][[{}<>][<><>]])])([{{<()()>[()[]]}[[<>](
{(({([([<((<({<>{}}<[]>)(<[]{}>)>))({{{[<><>]<<><>>}[(<>{})[{}()]]}{[<[]{}>([]())]{({}{})}}}<({([]())}({<>()
<<{<[<(([<[{[{[]{}}([][])]{{<><>}{<><>}}}{({<>[]})({<>[]})}]}<<[[[[]()]{[]()}]<{{}{}}[{}{}]>](<(()())[[][]]>[
[{[<[{(([<<{{(()[])(<>[])}}{(<<>{}>([]<>))}]>({{((<><>)[[]{}])((()<>))}[[({}[])({}{})]([{}()]([]())
{[{[{[{({([<{[<>()][(){}]}<[[]()]{(){}}>>]{{(([][])([]{}))}})}(<{{<({}())(<>[]>><[()()]>}{[((){}){<>{}}]
[{{[<<[[(<({<{()()}(()[])>(<{}[]>)}[(<<>{}><[][]>)[({}<>]([]{})]])[<[<<>()><[]<>>]{<[][]>[<>]}>{[{{}()
{[(([<({[<(({([][])<{}()>}{<{}[]><[]<>>))(<{[]<>}[()()]>))({{({}{})}<<<>[]>([][])>}[[{()[]}<<>{}>]{(<>[])
{[<[(({{{{[((([]())[{}()])[[{}[])<{}<>>])((<()[]>{{}[]})(<<><>>(<>{})))]}}}[([[({[{}[]]<(){}>})(
{[{((<[<{{([{([]())(<>{})}[<{}[]><[][]>]])<((<[]{}>{()[]})[(()())(<>())])(({{}()}([]())))>}}>[{[<<<((
[{[[{{{<{<<({<{}{}>([][])}{([]{})[(){}]})<{(<>[]){()[]}}{[(){}][[]<>]}>>>}[[([([<>{}][[]{}])[
[((<({<({([<(<<>()>)[{()<>}{[]<>}]>[[{{}[]}<{}()}]{{()[]}}]]{[<<[][]><<><>>><(<>)>]<{{()<>}[[]<>]}{(
[<{[[[(<(({(<({}<>){<>[]}>){(<()<>>{()()})[{[]()}([]())]}}{[[<()[]){<>{}}]<<<>{}>[(){}]>]<[[()<>]<()[
<{<<[[[[{[<<{({}<>){()[]}}([<>()]{<>})><(<<><>)<()<>>)[<[]()>[<>{}]]>>(<<({}{})[[][]]>([<>{}]({}<>))>{<<<
{[[{<({<{(<[(([]{}){[]{}})]<[[[][]][{}{}]]{[{}<>}{{}<>}}>><({[()[]]<{}{}>}{({})})<<<()[]>[{}()]
<[[{{([({[{(<{()()}>{(()[])(<>[])}){<[<>[]]{[]()}>[<[]<>>[<>()]]}}]})]<{{(([<<{}()>>(([]()){{}()})]<[(<>[]
[(<<<({{<([{(((){})[{}[]])[{[]{}}(()<>)]}]{({{<>{}}[<><>]}{(()<>)})[[([]{})<()[]>]{(<>[]]<{}{}>}]})[{[[(()
[<[{{[({{{({(<[][]>{[]})[{{}()}<[]{}>]}<[<<>()>({}{})]<{[][]}<[]{}>>>)}<[[{<<>()>(<>{})}<<()<>>[<>{}]>]]<<<
<<<(<<<(<[{<(<(){}>((){}))({()<>}{<><>})>}([<({}())[<>()]><{{}()}([])>][{(())}(([])<<>{}>)})]>)>[[{((
{[[(((<[{[{[<({}())((){})>[{<>{}}<<>{}>]]{{<<>()>{[]()}}<[()]({}())>}}(([{<><>}][<<>[]><[][]>])<[({}[]]]({
{(<(({<({<[([<[][]><()()>]({{}{}}[{}<>]))<<(()[])[<>[]]>{{[]{}}(<>[])}>]((<[{}[]]>{<()()>[<>{
<([{[([(<[([{<[]{}>[<>()]}[{<><>}]])[([([]<>){()[]}])[({{}[]})<[<>[]]<[]<>>>]]]>)])[(({<<{{[<>{}]{<>()}}}
(<({{[<<{<{<{{()}{[]<>}]{{(){}}[[]()]}>{{{[][]}[()]}[(()<>)(<>[])]}}{{(<[]><(){}>){(<>())([])}}{[(<>)({}<>)
{<[({{<<{([([((){})](<{}[]><<>{}>))([<<>[]><()()>])][<{{[]<>}(()<>)}<<()[]>{(){}}>>{(<()[]>[{}])
[<[[{[{{[{(<[<()<>>]<[(){}][{}]>>)}([{{{(){}}[<>{}]}((()<>)<[]<>))}][({<[]{}>{[][]}}({{}{}
{[[([<((<(([(<<><>>{[]()})]<{[()<>]{{}{}}}{[{}[]]<[]{}>}>)((<<[]<>>>({<>()}))(<[[]{}]>{{[]{}}[(
{({[([{(<{{(<[<>[]]<[]<>>>(<{}[]>[<>[]]))<([{}<>]{{}()})<[()()]([]{})>>}(<{<<>{}>([][])}><(({}[])
{<<{[([{({[[<<<>[]>[[]{}]>]({{<>[]}(()<>)}{[{}<>]<()[]>})]<({[()()][{}[]]}<<()()><(){}>>)>}[([
<{{([<{{{{([(([]<>){<>{}})][<<<><>>[(){}]>])}{<([({}<>)<<>[]>])({{[]()}(()())){{<>[]}({}{})})>(<({<><>})><
<({<{[<<({(<{[{}[]]{{}{}}}{[(){}][[]<>)}>[(<(){}><{}{}>){(<>)[[]{}]}])}(<[<(()())><<<>()>{()()}>][
{[[<<{<({<[(([{}]<<>[]>))[<{<><>}({}{})>]]{([{()<>}{[]()}][{()()}<(){}>])(<({}{})<[]{}>>[[<>{}]
{<[({{{[(<(({{()<>}(()[])}[<<>{}>{{}<>}])<{({}[])<()()>}{(()())<{}()>}>}([<(()())([][])>[([]){()<>}]]
[{<[{{(([{{<({<>[]}(<>()))<[[][]]>>(([{}]({}())){[()[]]((){})})}{((<<>[]>[[]<>])<[<>[]}([]())>)
[<<<{[((({<[((()<>)<()[]>){{<>{}}({}())}]{{<[]()>[[]()]}{[()]<{}[]>}}>[[{[<>()]}((<><>><()<>>)](([[]()](()))[
(([<<{<[<[({{<()<>><[]{}>}{{()()><<><>>}})<[[{(){}}[[][]]][[[]<>]]]{{((){})}[[<>{}]<[][]>]}>][[({({})[[]
<<((<({[(({[[[{}{}][<>]](<[]{}>[<>{}])]{(([]())(()<>))<[[]()]([]<>)>}}{[(<{}()>((){}))<{{}{}}[()<>]>]({<
{<{(([(([{{{({<>}({}{}))[[[][]]([]<>)]}<<(()[])[[]]>((<><>)<[][]>)>}}[[[(<[]{}>)]{{([]()){{}<>}>[<[
([[[{[([{{<{<<()()>(()[])>{({}()){<>[]}}}><(((()())({}[]))([()[]]<<>[]>))[[{{}[]>]{((){})}]>}<[[[[
{[[[{(<[{(({<<{}()>{<>{}}>[[[]()]<{}{}>]}{[[{}[]]{{}}]((<>))}))[{{{(()[])<{}{}>}<<[][]>{<>()}>}{[{<>{}}<(){}>
<({[<[(<<{[{<{{}<>}(()())>(<(){}>[<><>])}[([{}]{()[]})<{<>{}}{{}}>]]<{([()[]]<()[]>)[([]())([]<>)]}((((){}
{[<<[{([((([(({})[{}<>])({[]()}{<>{}})][{{{}<>}}(({}<>))]){<{<<>()>[(){}]}(({}<>)<<>[]>)>[<[()[]]([][])
<<([<<{[<<<<<(<>{})<{}{}>>>{([()()]<<><>>)(((){}))}>{<<[[]<>][(){}]>[(()<>){[][]}]>{(<()()){<>{}})<[[]{}
[{<{[{{([<({({(){}}<{}<>>){[{}{}]{()<>}}}[{<{}[]><<>[]>}<[[]()][{}<>]>])<{([{}[]][<>[]])<[<>()](()[])>}[(<
({([{{<[[[({<(<>())<<>()>>([()[]](<><>))}{<(<>[])<{}()>>({{}[]}{{}<>})})[<{[[][]]({}[])}{{{}[]}}
{<[([{<[[<([{<[]<>>(()<>)}[(<>{})<{}{}>]][({<><>}<[]()>)(<()()>[{}{}])])>]]<[[({([<><>]{()})<{<>()}{<>
[(<[[{[{{{<({[()()][[]<>]}<(<>[]){[]{}}>)<[{()()}{[]}]<[{}{}]<{}[]>>>>}{([<[()[]]>{([][]){()[]}}])}}}]<{<[
{{<{<((<<{[<{[<>()]<<>[]>}<<[]()><{}()>>>({(<>[])<<><>>}{(<>[])(()())})]}>[[[[{<[]()>[()[]]}((<>{})[()<>])
[<<[<[{(<({{[{[]<>}<{}()>][[<>[]][{}<>]]}<(({}{})({}[]))({[]<>}[{}{}])>}<[(<[]{}>[()()])][[({}[])(()
<{[{[<<(<{([{({}{}){<>{}}}{[()](()<>)}][[({}())({}<>)][<()<>>{<><>}]])}<({{((){})(()[])}}(<[{}[]]<[][]>><{
{(<{{[[[<<{{<([]<>)<{}()>)({{}{}}([]{}))}[{({}{})}<[()]>]}{[[<{}<>>](({}{})([][]))]{([{}<>]{()<>
<([[{{<(<{(<((<>())[[]{}])>((<{}<>>{()<>})[{()()}(()<>)]))}[{(<{{}{}}[{}()]>)}([[[{}[]]<<><>>][<()()>[
[(<[{<({{<<<<{<>{}}(<><>)>[{[][]}[[][]]]>{([(){}](<><>))((()<>))}><<{{{}<>}{{}<>}}({<>}<[]<>>)>>>{{{(([])[{}{
((({[[[[<<<{<<[][]><<>()>>[<()[]>(()<>)]}{{<<>{}><<>[]>}{[{}{}][()()]}}><{[[{}[]]<{}[])]{{<>[]}
[({[[<<<<{[<(<[]<>>[<>()])[[<><>]<<>()>]>>{[{(<><>)[()()]}{(()())[<>{}]}]{{<<><>><[][]>}({<>[]}(()<>
{[[{(<<([[{(<[{}]>{{<>{}}{{}[]}})}]{<({[(){}][()()]}{(<>{})[[]]})>{<{[[]()][<>[]]}(<[]()>[<>{}]]><{[{}()
({{[[<<({<<{(<<><>>[{}<>]><((){})>}{{{[]()}([]())}[{{}{}}[<>]]}><<[[{}[]]([][])]<[{}[]]{{}{}}
<(((<(<<<[[[<{[][]}>{<[]>{<>[]}]]{<<{}[]>[()()]>}]([([()()]<[]()>){{{}()}}]<(({}{})(()()))
[{{<[{[<<<[[<<{}<>>({}<>)>[<[]()>(()())>]<<<()[]><[][]>><[[]{}]([]{})>>]>><[{(<([])<{}()>>(<{}<>><<>{}>)
<<<(<[[[<(<<[[[][]][{}{}]]>>(<([[]()))({{}[]}[[][]])>))><[{(<({}<>)(()<>)>{[{}<>]})[({<><>}[<>()])<
<{<{[(<<([[(<[[]<>][{}[]]>(<()<>>(<>{}>))]]<{{([{}()]{<>()})<(<>()){()}>}{<<()()>(<>[])>{(())[[]{}]}}}(<{[{}
{[[[[[[{[[{[[[()()][()[]]]{{{}()}(())}]}{<([{}()]{<>()})<[()<>>[<>()]>>[(<[][]>{[]<>})[<{}<>>]]}]]((({({(){}
(<{(<([([{({{{[][]}{[]<>}}[<{}[]>[<>[]]]})}(<[{{(){}}(<><>)}((()[]))](([{}()]([])){([]<>]{{}()}})>[[[{(
<{{{<<[<({([[[()[]]]][{(()[])(()())}])}){<{[{<[]()><[][]>}]([{[]()}<{}[]>][([]())[()[]]])}[<[(<>()){
(<{{[<<[<{[[[(<>[])[<>()]][[[]()][{}()]]][([<>])<[<>[]][[]<>]>]][[({{}<>}[{}{}])({[]()}{()<>})]]}(<([
{[({([{(((([{<[]()>}<[()()]([])>]{<[()[]]{[]<>}>[<<>{}><()>]}))))<([<[[<()[]><<>()>]][(<()[]><{}<
({([<[[{([([<{<>()}>[({}[]){<><>}]][((<>){{}{}})(({}())<{}{}>)])])<((({({}{})[<><>]}(((){}))){((
{[(<[[[{{[{<<{()()}(()())>({()()}[{}{}])><[{(){}}[[][]]]>}<[<(<>{})(<>())>({{}[]}{[]<>})]<{<{}<>>}{<{}<
<<[({<<{([([{{<>}[<>()]}]{[[[]()]{<>()}](([]{}))})<<[{[]{}}([]())][<[]{}>{[]{}}]>({({}[])}
<<({[[{{<[<[{[(){}][()[]]}]({{<>}<<>{}>}((<>())<{}()>))>]{{{({[][]}[[]<>])({<><>}({}))}{[{()()}[<>{}]][<<
{<<[<{<{{({{((()[])[[][]])[{[][]}[<>{}]]}{[{<><>}([][])][[[]]((){})]})){{{([{}{}]((){})){{<><>}
<[<{{(([(<<[<{{}[]}{[]<>}><([]{})[[]()]>]{{<()<>>([]())}{<()<>><<>[]>}}><[{[<>[]]<[]()>}<<[][]>{[]<>}>][{(()
<([{{[({[([{(<()()>[<>[]]){<[]{}><[]{}>}}])]<{[[<[{}<>][[]()]>[{{}()}[<>[]]]]{{([]{}]{()<>}}[[()<
({{{<<<[<<{(<([][])(<>{})>((()[])))}{({<()()>[<>{}]}{{{}[]}(()[])})<[{()())<<>[]>]({<>}<[]<>>)
<<<{(<({((<({<()>(<><>)})[[<{}()>(<>())]]>(<<({}[])[<>[]])>({{[]()}(<><>)}(([][])<<>()>)))){[[([<>][{}])
(<((<([({[<<{<{}()>(<><>)}{[[]{}]({}())}>>(<(<<>[]>)<[<>()]<<>{}>]><{<{}<>>}{{<><>}((){})}>)]{[<(<{
{(([<{(<{<<<(([][])(()[])){<()[]><{}()>}>{(([]<>)<<>()>)<<{}<>>(<>())>}>{(<{{}<>}<[][]>>[[<>{}]<<>{}>])([(<
{{{{<([([[[[(({}{})<<>>){{<><>}<()[]>}]([[(){}]<{}()>][[<>]<{}{}>])][<([[]<>][{}{}])[<[]<>>[[]<>]]>{<{[
[<(({{<(({[({[[]<>]<[]<>>}<{{}<>}<(){}>>)[([()()][()[]])<<()()><<>()>>]](<<[{}[]]{[][]}>({[]}[<>{}])>{[[[][]
(({({{([{{({<<(){}>(()[])}}{{([]<>)[{}[]]}})}<(<{[<><>]}>[[<{}>[{}()]]])<{{{<>()}<<>[]>}}[(<<
[<((<((<[<{{(<<>[]><()<>>)[<<><>><<><>>]}{([()()][(){}])}}><<<<(<>())[{}]>[([]<>)({}{})]>[[<{
{<<(({({{{(<{{()[]}}[({})({}())]>)}}}(<[[[[{()<>}<()<>>]([[]{}][<>{}])](<<{}[]>{{}<>}>(<{}<>>{[]{}})>]][({{
<<{([[[<((<{{(<>[])[()[]]}([[]()])}>))>]]]<<({[(({<{[][]}({}[])>{<{}{}>[<>{}]}}<<[<>()][[]()]>{
<[[({([<[<{([{[][]}[<><>]]{([]<>)(()<>)})[<(()())<[]<>>>[<[]<>>(()())]]}>(<(<[[]()]<[][]>>{[[]()]{[][]}}
[<([[({[{{<{{([][])}(<<><>>(()<>))}[[<{}{}>{{}[]}]{{[]{}}}]>}}[<<[{({}{}){{}()}}({[]{}}([]{}))][{[()()]
((({<(<{(<([<{[]{}}<(){}>><{[][]}(()<>)>](<[[]()]({}[])>{[()<>]}))<{(<[]()>[[][]]>[<[][]>[<>()
{({<({(<([<([([]{})({}[])])>])<((<[[[]{}]{<>[]}]{{<>[]}<<><>>}>)(<({[]{}}{[]()})[{<>[]}<{}{}>]>{({
{{{[<<<{<(<{<[[]<>]<[]()>>}<<(()[])([]())><((){})<{}[]>>>><(<((){})<[]>>(([][]))){<[[]{}][()[]
[<({[(({([<(<[{}<>]>(<()[]>{[]{}})){{{[]()}}[<()<>>[<>[]]]}>]({<<{[]}[<>()]>(<<><>>{[]{}})>[<({}{})[()<>]>[[<

90
src/net/ninetailed/adventofcode2021/Day10.java

@ -0,0 +1,90 @@
package net.ninetailed.adventofcode2021;
import java.io.IOException;
import java.util.*;
public class Day10 extends Solution {
public static void main(String[] args) throws IOException {
new Day10(Util.loadInput("resources/input/day10/input")).runConsole();
}
private final List<String> input;
public Day10(List<String> input) {
this.input = input;
}
@Override
public String part1() {
return String.valueOf(input.stream().mapToInt(Day10::invalidScore).sum());
}
private static int invalidScore(String line) {
Deque<Character> stack = new LinkedList<>();
for (char c : line.toCharArray()) {
switch (c) {
case '(':
case '[':
case '{':
case '<':
stack.push(c);
break;
case ')':
if (stack.pop() != '(') return 3;
break;
case ']':
if (stack.pop() != '[') return 57;
break;
case '}':
if (stack.pop() != '{') return 1197;
break;
case '>':
if (stack.pop() != '<') return 25137;
break;
}
}
return 0;
}
@Override
public String part2() {
long[] scores = input.stream().filter(Day10::isValid).mapToLong(Day10::completionScore).sorted().toArray();
System.out.println(Arrays.toString(scores));
return String.valueOf(scores[scores.length / 2]);
}
private static boolean isValid(String line) {
return invalidScore(line) == 0;
}
private static long completionScore(String line) {
Deque<Character> stack = new LinkedList<>();
for (char c : line.toCharArray()) {
if (c == '(' || c == '[' || c == '{' || c == '<') {
stack.push(c);
} else {
stack.pop();
}
}
long score = 0;
for (char c : stack) {
score *= 5;
switch(c) {
case '(':
score += 1;
break;
case '[':
score += 2;
break;
case '{':
score += 3;
break;
case '<':
score += 4;
break;
}
}
return score;
}
}

40
test/net/ninetailed/adventofcode2021/TestDay10.java

@ -0,0 +1,40 @@
package net.ninetailed.adventofcode2021;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class TestDay10 {
public List<String> TEST_DATA = Arrays.asList(
"[({(<(())[]>[[{[]{<()<>>",
"[(()[<>])]({[<{<<[]>>(",
"{([(<{}[<>[]}>{[]{[(<()>",
"(((({<>}<{<{<>}{[]{[]{}",
"[[<[([]))<([[{}[[()]]]",
"[{[{({}]{}}([{[{{{}}([]",
"{<[[]]>}<{[{[{[]{()[[[]",
"[<(<(<(<{}))><([]([]()",
"<{([([[(<>()){}]>(<<{{",
"<{([{{}}[<[[[<>{}]]]>[]]"
);
Day10 solution;
@Before
public void setup() {
solution = new Day10(TEST_DATA);
}
@Test
public void part1() {
Assert.assertEquals("26397", solution.part1());
}
@Test
public void part2() {
Assert.assertEquals("288957", solution.part2());
}
}
Loading…
Cancel
Save