diff --git a/AOC2021.Test/AOC2021.Test.csproj b/AOC2021.Test/AOC2021.Test.csproj index 529abc5..de7451d 100644 --- a/AOC2021.Test/AOC2021.Test.csproj +++ b/AOC2021.Test/AOC2021.Test.csproj @@ -18,6 +18,18 @@ + + Always + + + Always + + + Always + + + Always + Always diff --git a/AOC2021.Test/DayTest.cs b/AOC2021.Test/DayTest.cs index a24cb91..3b2813c 100644 --- a/AOC2021.Test/DayTest.cs +++ b/AOC2021.Test/DayTest.cs @@ -89,6 +89,22 @@ namespace AOC2021.Tests Assert.IsTrue(request.Answer.Equals(result.Answer)); } + [TestMethod] + public void Day10() + { + var request = new TestRequest() { Day = "day10", Answer = new Answer() { Day_A_Test = "26397", Day_A_Input = "168417", Day_B_Test = "288957", Day_B_Input = "2802519786" } }; + var result = _tester.Test(request); + Assert.IsTrue(request.Answer.Equals(result.Answer)); + } + + [TestMethod] + public void Day11() + { + var request = new TestRequest() { Day = "day11", Answer = new Answer() { Day_A_Test = "1656", Day_A_Input = "1793", Day_B_Test = "195", Day_B_Input = "247" } }; + var result = _tester.Test(request); + Assert.IsTrue(request.Answer.Equals(result.Answer)); + } + //[TestMethod] public void MapVsEnumberable() { diff --git a/AOC2021.Test/Input/Day10_input.txt b/AOC2021.Test/Input/Day10_input.txt new file mode 100644 index 0000000..9e007be --- /dev/null +++ b/AOC2021.Test/Input/Day10_input.txt @@ -0,0 +1,94 @@ +(<[<[((<[<([<<[]()>{{}()}>({<>[]}<[][]>)])<<[{{}[]}[{}<>]]([()<>][()()])>>>]((<{{([]{})<[]<>>}(<[ +<[{({[[<{[{{{<<><>><{}>}<([]()){()[]}>}({(()()){{}<>}})}{([<()()>[<><>]><<{}[]>({}<>)>)}]{{[((<>()){<>[] +[[{[[(<((<[<<{{}()}<()[]>>(<<>{}>{{}()})>]<<[[{}()](()<>)][<<>{}><[]()>]>{((<>()){{}()))}>>[{[<[<>{} +{{((({({(<[({<<>()>([]<>)}({()[]}(<>{}))}[({{}{}}([]()))<({}[])[[]()]>]](<{([]<>)(<><>)}{[[]] +[[({[{(((<(<{({}[])}((()<>)([]<>))>({{[]<>}{{}[]}}{[<>[]][()()]})){(<{{}[]}{{}[]}><([][])[<>{}]>)}><<{{{()}[ +(<{([{<[([(<({{}{}}{<>()})[<{}<>>{()()}]><((<>}(()<>))>){<[[()[]](()[])]>[{<{}[]>(<>())}]}](<[({( +<{{{<({[{<<[<({}()){[][]}>[<{}{}>[{}<>]]][[(()<>)([][])]]>><{<[(<><>)]>{(<()[]><[]{}>)<<<><>>(<>[] +({{{{<<[{([(({{}{}})<[[]<>]>)])}]>>}[(<[<<<{[{{}()}]<[{}{}]>}>>{(({(<>())<()()>}(<[][]>{()()})))}){[<<([() +<(([{(<{{{{([({}{})(<>[])][[()[]][<>{}]])<[<()[]>]}}{<<{{}<>}><{<>}([][])>>{<{{}{}}<{}<>>><((){}) +<(<<[([<[<({{{<>[]}<()[]>}}[([()<>]([]()))(({}<>){[]{}})])>]{({[([<>()])]})}]{[[<(<{()<>}[{}<>]>{[()[]][<><>] +{[[[((<(<[<(<[()()]>)<<<{}<>><<>[]>>(<(){}>)>>](<([<(){}><[]()>](([])))[<{{}{}}{<><>}>{<<>>{<> +[<{([{<[<<<[({[][]}<{}[]>){(()<>)<{}<>>}]><{{{[]<>}<()<>>}[(()[])[{}<>]]}(({<>[]}<{}()>)(<{}[]>([]{})) +{<[<[<<[<[{[{(()<>)}(<<>{}>[<>()])](([<>[]](<>[]))[[[]<>]([]<>)])}]><{{<{{[]<>}{{}[]]}<[<><> +[((<<{<[{<{(<[[]{}]>)}<<({()[]}[[]])<(()<>)[(){}]>>(<[{}<>]{<>[]}>)>>{[<<{()<>}]<[[]<>]({}{})>>[<[( +<<<<{[<(<[[<<{()[]}({}<>)>{{[]<>}{[]()}}>](([{()}]([(){}][<>[]])))]{[(<(()[])[<>[]]>{([]())}){( +({<[({([{[(<[{<>{}}(<>[])][<()()><()<>>]>)]}({<<<{[]{}}[<><>]>>><{((<><>))<<<>[]>{<>[]}}}([(()<>)<<>{ +<(<({{[<[{<[[{()[]}]((<>()))]<[[()[]](<><>)][[{}{}]<()>]>>}]>]{<(({<((())[<>{}])[<[]<>>{<>{}}]>}<<{[<>[]] +{[([{((<({<{[{[][]}<[]{}>]{{<>{}}<{}[]>}}>[<{(<>[]){[]{}}}<{[][]}{{}}>>]}{((<[[]()]<[]>>)[[<()[]> +(<{(({[(<{{(({<>[]}[{}])[[()()][[]()]])}[{(<()()>{[]<>})(<(){}><[]()})}[(([]{})<<><>>)<(<>())<<> +<(<{<({<((<{(({}{})><(<>[])>}>[<{<<>{}>([]{})}{[<>{}]([]())}>([[()[]]<{}<>>]({[][]}<()[]>))]){<[(<{}<> +([{({([({([[[{[]{}}({}<>)]]((<{}()><<>{}>){({}[])<{}<>>})]>}{[({{(()<>){<>[]}}[<()[]>[()<>]]}<<<< +{(<<[{(<<<(<<{[]<>}<[]()>><(<>)(()[])>><{(()<>){{}<>}}>)({{(<>())[<>[]]}}<[[{}{}](()())]<{{}()}( +<<(<<{[{([{([[<>()][[]()]](<()<>>[[][]])))<(<<[]<>>>)[({<>[]})]>][<<<(<><>)(<>)>[([]{})]>>])[[{(<(<>()){[] +<<<{[(<(<[(([{{}}]((<><>)[{}<>])))<({{<>{}}[{}<>]})>]{<[<(<><>)[{}{}]>[[{}[]]({}{})]]{<[[]<> +<{(({{{[[[<([{{}[]}]({(){}}((){})))>]]<(({[{<>()}({}{})]})(((<(){}>[(){}])[{[]{}}(<><>)])) +<[[{((<{([(<<{()<>}(()[])><<[][]>>>{(<{}[]>({}<>))})<<<<()()>{[]()}>(<<>()>)>>])}>))}[<<{[<{{{{<[]<>> +({(<{<(([[({{{[]{}}[()<>])}{(((){})<()[]>)})[<([[][]][[]()]){{<>()}[[][]]}>(<(<>)<<>[]>>{[<>[]]{[]( +((({(<<({<[[[{<>}[<>()]]<<{}<>>({}[])>]]<({<{}{}>[()[]]}{<{}<>>{<>()}}){[(()())<{}[]>][([]{})[[]<>]]}>>[{[<(( +{[[<[([{{<[<[[<>()][{}[]]]{<<>()>{[]()}}>]{[<[<><>]<<>{}>><{<><>}<<>{}>>)[{<{}<>><<>[]>}{{<><>}}]}>{ +<(<{(({([<[([([][])(()<>)]{[<><>](()<>)})((([]{})[[]()]))]([({[]()}{<>{}}){{{}})])>]{<<({<<>[] +<<{([{<[{{(({{{}()}{[][]}}<{[]()}{<><>}>){{<()[])<<><>>}[<{}[]><{}[]>]}){<<{[]()}<<><>>>{<{}{}>({}( +<[[<<(<({<([((()<>){()[]})[([][])<{}[]>]]({<<>()>((){})}([[]()]({}<>})))>})[<<{({([]())<<>{}>}{{() +<{(<[[<(<<({(<[][]><<>{}>)[<{}{}>[()<>]]}(({()[]}<{}[]>)))>[<[{(<>{}){()<>]}<[{}[]][<><>]>]>]>[<[({< +(<(([{{[<(<[<{()}{[][]}>]>{(((<>())[{}{}])<[[]]<(){}>))})>](<[(<(<[][]><<>()>){<{}[]>([]{})}>{(([]))<([]<>)[( +{((<{(({({<[{<{}<>><<>[]>}((<>{})[[][]]]]{([()[]](()()))([[]<>]{{}()})}>})(<[<<<[]{}><()[]>><<{}<>><< +{<{(<[<{[[{{{[{}()]}({()()})}((([]<>){()<>})(<<><>>))}<[{{()()}{[]()}}]>]<[((<[]()>((){}))([()()][<> +([({((([<({[[[[]()](()<>)][({}())<<><>>]]}<{<[{}<>][{}{}]>(([]<>){[]()})}[<<[]()>[{}()]>]>)<([([() +<<{({<<[{[<{<(<><>)([])>({{}[]}{[][]})}<([()()])(([]{}))>>([(<[][]>{{}<>})[[<>{}]<()<>>}]{[{{}[]}({}<>)]<[[]< +([<{[[[([[<[(((){})[{}()]){{[]<>}(()<>}}][({(){}}{<>[]})]>]])({<[{<(()<>)>({[]{}}<{}()>)}(([()()]<<><>> +([({{[<([[{<[{()[]}[<>]]({<>()})><{({}[])(()<>)}{(<>{}){{}<>}}>}]])>]<([([[{[[<>{}]{[]{}}]{<[][]]{{}[]} +[[{(({[{{[[[<(()[])[[]<>]>]{<{()[]}{()()}>{(<>[])({}[])}}](<((<><>)[<><>])([<><>])))][[[(<( +[{(<<{(((([[<<{}<>>({}())><<<>[]>[{}{}]>][<(<>[])<{}>>[<[][]>{{}[]}]]]<<(<[][]>{[]()})({{}()}<{}<>>)><([< +{<[[<{{<[[(<{{()()}<[]<>>}><<{<>}<{}{}>><<[][]>>>)<{{[()<>]{{}{}}}<({}[])[(){}]>}>]]>}<<(({{(((){} +((<([[<{{<(<([<>()]{(){}}]({[]()}<()()>)>(([{}[]]((){}))))<[{[<>{}]({}<>)}]>>}[[<({(<>())(<><>)}){([<><>][[][ +<[([<(({<(([<({}<>)[()]><<[][]><<>()>>]<{([]{})}<{<>{}}{[]()}>>)){[[{[<>{}][[][]]}]<[[<>()][{}[]]]>][<[[() +((({({[[[[{{<(<><>)((){})>}}]{{[[[<>[]][<>[]]]{{()<>}[[][]]}]{{{{}{}}{<>[]}}([{}][<><>])}}({( +<<<{[<(<({[{[({}[]){()()}]}]}{[(({{}()}[[]<>])[([]<>)(<>)])]{{({()<>}{<><>})<<{}()>>}<[<[]{}>]{([]< +(({[(<<<<{((((<>{}){[]<>})[{[]<>}])(([[][]]<()<>>)<[{}<>]<[]{}>>)){(({()[]}<[][]>)[[<>[]]{()<>}])([[()[]]]< +<([<<[<<[{[[<<[][]>>{((){})[<>[]]}]((<{}{}>[[]<>]){<{}()>(()<>]})]({[(<>{})(<><>)](<(){}>[ +((<{[{[[{<<((<<>{}>{<>()}){{[]()}((){})})((([]<>)<<>[]>)<(()){<>[]}>}><[<(<>[])<{}()>>]{<< +[(<[{([{<{([[(()){(){}>]{{{}<>}<[]()>}][(<()<>>[(){}])<[()](()<>)>])}({([[[]{}]({}())]({()<>}{ +[<[{[<[{[[<[{([]){{}{}}}]>][[<<(<>{})<<>{}]><{{}()}>>(([{}<>]({}))[(<>{})<[][]>])]]]}]<[(({ +(([{{<<{<[[(((<>())<[]{}>)([()()}(()())))[((<>()))<[{}()]({}[])>]](<{{<>}{{}<>}}>)]>[{<{{[ +<<[[{<([<<<<{({}<>]([]())}({{}()}<()<>>)>[<<()()>{()()}>({()<>}[{}()])]>{<(<[][]>)>[<[{}{}]>((<>() +(<{[<[({{[[([<[]{}>[[]()]]{{{}[]}(<><>)})]<[([<>{}]<{}[]>)[<{}<>>{[]()}]]<(<<><>>(()()))<[<><>}{{}{}}>>>]( +[<((<{{[(<(((<[]()>([][]))(<()<>>{{}[]}))((<{}{}><{}[]>)({{}[]}[[]{}])))<{(<[][]>)<{<>()}>}{{[()<>]}{{<>( +{((([{{((<<<<[()()][(){}]>{[[][]](()<>)}>>{{{{<><>}[[]<>]}[{<><>}({}()]]}([[<>]{(){}}][[(){}](()<>)])}>[([([[ +[(({<<{([{(<[<()[]><[]()>]((<>[])[<>()])>([<(){}><<>()>]{[<>()][(){}]}))}[<[{[{}()]{()()}}(<<>>{[]})]{[<<>()> +<[{[<<({<<<[((<>()){()()})[(<>[])([]{})]]><[<([][])([]<>)>(<[]{}>(()()))][{[()[]]{[]<>}}[(<>{})<<> +((<[<[(({[{[[<<>[]]{[]()}]({()<>})]{[<<>()><{}()>]}}(<(([]{}){(){}})>{<{{}()}(()())>})]}([(<{[[][] +{(((((<({[<[{<<><>>{<>[]}}(<(){}><[]()>)]<{<{}()>(<>())}[[[]<>]{{}<>}]>>{[{<()()>[[]()]}[<{}>]]}]{ +(({([{<([<{{<<[]{}>[[]{}]>({{}{}}({}<>))}<[{<>{}}{()<>}][<<>{}>]>}>{({(<(){}>[{}()])[{()<>}[()<>]]}<[{[ +[[{{[<<[<((<{{{}()}<[]>}{[{}<>]<()[]>}>({[[]{}]}[([]())<[]{}>])){(<<()()>>{[{}[]]<{}{}>})})>]>>]<([<[< +{({{{{[[<{<[{[{}<>]}([[]{}][{}<>])][((()()){[]()}){[<>()](<>())}]>}>[{<[{{<>{}}({}())}[<<>{}>(( +({[((<{{<<<({[{}{}][(){}]})((<[]><()()>)<{<>()}[[]<>]>)>(({<{}[]>([][])}(({}{})<<>()>))<{<[ +[<<[<{[[{{(<{[[]<>)({}[])}>)}}(({{((<>[])<<><>>)}{[[[]<>]]{{[][]}<{}{}>}}}){[[((<><>)[{}<>]) +(({<(<([({<(<<()<>>([]<>)>[([]()><()[]>])<[<()[]>[()()]]{[[][]]{{}[]}}>>{{<<[][]><[]{}>>{[{}[]]<<>>}}{{<[]{ +([{{({({{{<([({}[])[()<>]][[[]{}]([]())])>([[[()()][<>()]]([{}[]]{{}()})]<{{[]<>}({}[])}{<()[ +(<{(({(<{{[<[[()<>]{<>()}][<(){}><()<>>]>{<<<>[]>[<><>]><({}<>>>}](<<{<>[]}[[]<>]>[[[]<>](()[])]>{{{[] +[[[<[([([(({{<<><>>([][])}})[([[{}()]<<><>>])<({{}<>})<{{}}({}{})>>])])<([((<[[]<>]{(){}}>{[[]<>]<[]<>>})( +(({(<({[[<{<([{}<>]{<>()})([()()](()<>))>(((()())[{}()])({()[]}{{}<>}))}>([({<(){}>([]<>)}{[<>()]<<>>} +({{[<<{<<[<<{[()()]<<>[]>}({[]}<<>{}>)>[<<{}{}>[<>[]]><({}<>)<[]()>>]>({(<()[]>(<>[]))}[([()()][{}]){{()()}{ +(<({{([{<[{(([()]<<><>>))}<<<[()][{}{}]><([][])(()())>>[[<<>[]><[]>]{[<>{}]<[][]>}]>](<(<<[]>({}()) +(<{<([[[<<((([<>{}]{()[]})([(){}]{{}<>}))[[[{}<>](()[])][[<>[]](<>())]])>>([{([{<><>}<{}[]>] +<{[{[{<{{({<<({}[])><{()()]<[]<>>>><[({}[])[()()]]<[{}<>]{<>{}}>>}((({()[]}{[]<>}){[[]()]([]<>)}))) +[(([[[[<<<[<{{<><>}{[]()}}[({}[])<(){}>]>(([[][]]([]{}))<{{}}(()<>)>)]({<<[]{}>{[]<>}>{[()<>}{{}}}}{{[(){}]} +{(([<{<(<{<{{<()><[]{}>}}({{{}<>}}<{<>[]}<{}<>>>)>[<([{}[]]<()<>>)(<{}{}>{()[]})><<{<>[]}(()[])>>]}( +(<((<{[[{({[([()[]]<()<>>)[<<>{}>({}[])]][<[<>[]]><{(){}}>]}[<(({})({}()))(<[]{}>(<><>))>[[([]{})[{}[ +{({([[[[{<([<{{}<>}[[][]]><<<>[]><<>{}>>]{{<{}<>><[][]>}({[]{}}<{}[]>)})>{{{{[[][]]}}}}}[<[[{[{}[]][()()] +[{(<([{{{<{<({{}[]}<()<>>){<<>())[{}[]]}>{{{[][]}}[[(){}]{<>()}]}}([(<<>()><[]()>)]{({[]<>} +[<[([{<((({<{{<>{}}}<<(){}><()()>>><({()}[[]{}])(([]())[<>[]])>}))){<[{{{(()[])({}())}{{()()}{{}<>}}}[(<{} +<([({({{(({{({<>}(<><>))}{([{}[]]{{}<>})}}[{({[]<>}([]<>))[<[]>([]{})]}({[[]{}]({}())}{<{}<> +[{(({(({<[[{<[(){}](<>[])>}[[[<>()]][([][])<<>()>]]](((<<><>>[(){}])([[][]]<{}{}>))[<(<>()){[]{}}>[[()[ +<<{{<[(([{[{[[<><>][[]()]]<(<>[])[[]<>]>}]<([<()[]>((){})][<{}>(<>())])>}][{(<([()()][<><>])(({ +({(<({([(<[<{[{}()]({}{})}<<{}{}>(()<>)>>]>{([<[<><>]<(){}>><({}[])((){})>]{{<[]()>[{}[]]}(({}{})< +<[{<{((<{((<[([]<>)]<{{}()}{()[]}>>(((())){<[]{}>[{}[]]}))<[<<{}[]>((){})>[<()()>{()()}]]<[[( +{{{((<(([{({(({}{})({}{}))}[<{()<>}[{}()]><([]()){[][]}}])[([[()[]][[]{}]])<{({}())[<>{}]}{(<>[])} +<({[{[[{(({<(<<>>[(){}])({[]{}}<[]()>)>}<{[([]())<<>{})]{(()[])<[]>}}(<[{}{}]>(((){}){{}{}}))>) +<{[[({[{[{[([[<>{}]((){})][[[][]]{()<>}])<[<{}()>(<>{})]<<<><>>[<>()]>>][[[<[]<>>{{}<>}]<{[]{}}<[]{}>>] +(({(<((({({{<<{}()>(()<>)><<<>{}>[{}{}]>}(((()<>)(()()))(<{}<>>))}[<[[[]<>]]({[][]}[<>{}])>]){[(({{}{}}){([][ +[{{<({<<{(<({<{}<>>[[]<>]}<[{}[]](()()]>)>([{{<>[]}}{([][])([][])}]))({(<{[]{}}{()<>}>([<>()]{[]{}})) +[<({[(<{{{[((<[]<>>(<>[]))([[]<>]<()<>>))(<((){})>)]}}}>{{<([<[<()[]>{{}<>}](([]())[[]<>))>]{(({[][]}<<>( +([(({<([{({<(([]<>)(()[])){<()()>{<>()}}><([[][]]<()<>>)[[[][]]<()<>>]>}[{[{()[]}(<>[])]([<>[]]<<>()>)}]) +{{{([<[([{(<[{[]()}<[]{}>]>)}])<<<<({(<>())<{}()>})[{({}[]}{<>[]}}<<[]<>><{}[]>>]><(<[()<> \ No newline at end of file diff --git a/AOC2021.Test/Input/Day10_test.txt b/AOC2021.Test/Input/Day10_test.txt new file mode 100644 index 0000000..4861500 --- /dev/null +++ b/AOC2021.Test/Input/Day10_test.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] \ No newline at end of file diff --git a/AOC2021.Test/Input/Day11_input.txt b/AOC2021.Test/Input/Day11_input.txt new file mode 100644 index 0000000..1a0f415 --- /dev/null +++ b/AOC2021.Test/Input/Day11_input.txt @@ -0,0 +1,10 @@ +6111821767 +1763611615 +3512683131 +8582771473 +8214813874 +2325823217 +2222482823 +5471356782 +3738671287 +8675226574 \ No newline at end of file diff --git a/AOC2021.Test/Input/Day11_test.txt b/AOC2021.Test/Input/Day11_test.txt new file mode 100644 index 0000000..d7b4395 --- /dev/null +++ b/AOC2021.Test/Input/Day11_test.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file diff --git a/AOC2021/Controllers/AdventOfCodeController.cs b/AOC2021/Controllers/AdventOfCodeController.cs index 54fc4b8..7800904 100644 --- a/AOC2021/Controllers/AdventOfCodeController.cs +++ b/AOC2021/Controllers/AdventOfCodeController.cs @@ -28,6 +28,8 @@ namespace AOC2021.Controllers [Route("day7")] [Route("day8")] [Route("day9")] + [Route("day10")] + [Route("day11")] public AOCResponse Day(AOCVersion version, [FromBody] string input, bool IgnoreLogMessages = false) { AOCRequest request = new AOCRequest() { Input = input, Version = version, IgnoreLogMessages = IgnoreLogMessages }; diff --git a/AOC2021/Days/Day10.cs b/AOC2021/Days/Day10.cs new file mode 100644 index 0000000..36a1cf7 --- /dev/null +++ b/AOC2021/Days/Day10.cs @@ -0,0 +1,70 @@ +using AOC2021.Models; +using AOC2021.Models.Day10; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AOC2021.Days +{ + public class Day10 : AOCDay + { + private List _results; + public Day10() + { + _results = new List(); + } + protected override AOCResponse ExecutePartA() + { + foreach (var line in GetSplitInput()) + { + _results.Add(new ChunkReader(line).Process()); + } + var illegalScore = 0; + illegalScore += _results.Where(x => x.isCorrupted && x.CorruptedChar == ')').Count() * 3; + illegalScore += _results.Where(x => x.isCorrupted && x.CorruptedChar == ']').Count() * 57; + illegalScore += _results.Where(x => x.isCorrupted && x.CorruptedChar == '}').Count() * 1197; + illegalScore += _results.Where(x => x.isCorrupted && x.CorruptedChar == '>').Count() * 25137; + this._response.Answer = illegalScore; + return this._response; + } + + protected override AOCResponse ExecutePartB() + { + foreach (var line in GetSplitInput()) + { + _results.Add(new ChunkReader(line).Process()); + } + var incomplete = _results.Where(x => x.isIncomplete); + var totalPoints = new List(); + foreach (var inc in incomplete) + { + var total = 0L; + foreach (var c in inc.CompletionString) + { + total *= 5; + total += GetPointValueForCompletion(c); + } + totalPoints.Add(total); + } + this.Answer = totalPoints.OrderBy(x => x).ToArray()[totalPoints.Count / 2]; + return this._response; + } + + private int GetPointValueForCompletion(char c) + { + switch (c) + { + case ')': + return 1; + case ']': + return 2; + case '}': + return 3; + case '>': + return 4; + } + throw new Exception("Invalid char for Part B: " + c); + } + } +} diff --git a/AOC2021/Days/Day11.cs b/AOC2021/Days/Day11.cs new file mode 100644 index 0000000..067fed8 --- /dev/null +++ b/AOC2021/Days/Day11.cs @@ -0,0 +1,167 @@ +using AOC2021.Models; +using AOC2021.Models.Day11; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AOC2021.Days +{ + public class Day11 : AOCDay + { + private static readonly int BOARD_SIZE = 10; + public Octopus[,] _board; + public Day11() + { + this._board = new Octopus[BOARD_SIZE, BOARD_SIZE]; + } + protected override AOCResponse ExecutePartA() + { + CreateOctoBoard(); + int totalFlashes = 0; + PrintBoard("0"); + for (int i = 1; i <= 100; i++) + { + //Initial energy assumption + for (int x = 0; x < BOARD_SIZE; x++) + { + for (int y = 0; y < BOARD_SIZE; y++) + { + _board[x, y].IncreaseEnergy(i); + } + } + //Energy propagation with the flash + totalFlashes += Propagate(i); + PrintBoard(i.ToString()); + } + this.Answer = totalFlashes; + return this._response; + } + + + protected override AOCResponse ExecutePartB() + { + CreateOctoBoard(); + int totalFlashes = 0; + PrintBoard("0"); + int i = 1; + while(true) + { + if (DidAllFlash()) + { + Log("Found case where all Octopus flashed at " + (i - 1)); + this.Answer = i - 1; + break; + } + //Initial energy assumption + for (int x = 0; x < BOARD_SIZE; x++) + { + for (int y = 0; y < BOARD_SIZE; y++) + { + _board[x, y].IncreaseEnergy(i); + } + } + //Energy propagation with the flash + totalFlashes += Propagate(i); + PrintBoard(i.ToString()); + i++; + } + return this._response; + } + + + private int Propagate(int step) + { + int totalFlashes = 0; + while (AnyLeftToFlash(step)) + { + for (int x = 0; x < BOARD_SIZE; x++) + { + for (int y = 0; y < BOARD_SIZE; y++) + { + if (_board[x, y].CanFlash(step)) + { + _board[x, y].Flash(step); + totalFlashes++; + foreach (var t in GetSurroundingCoords(x, y)) + _board[t.Item1, t.Item2].IncreaseEnergy(step); + } + } + } + } + return totalFlashes; + } + + public bool AnyLeftToFlash(int step) + { + bool flash = false; + for (int x = 0; x < BOARD_SIZE; x++) + { + for (int y = 0; y < BOARD_SIZE; y++) + { + if (_board[x, y].CanFlash(step)) + { + flash = true; + break; + } + } + } + return flash; + } + + private bool DidAllFlash() + { + for (int x = 0; x < BOARD_SIZE; x++) + { + for (int y = 0; y < BOARD_SIZE; y++) + { + if (_board[x,y].EnergyLevel != 0) + { + return false; + } + } + } + return true; + } + + private void PrintBoard(string s) + { + Log("Step " + s); + for (int i = 0; i < BOARD_SIZE; i++) + { + string line = ""; + for (int j = 0; j < BOARD_SIZE; j++) + { + line += _board[i, j] + " "; + } + Log(line); + } + } + + private void CreateOctoBoard() + { + int row = 0; + foreach (var line in GetSplitInput()) + { + for (int col = 0; col < line.Length; col++) + { + _board[row, col] = new Octopus(Convert.ToInt32(line[col].ToString())); + } + row++; + } + } + + private List> GetSurroundingCoords(int x, int y) + { + var positions = new List>(); + for (int i = -1; i <= 1; i++) + { + for (int j = -1; j <= 1; j++) + if ((x + i >= 0 && x + i < BOARD_SIZE) + && (y + j >= 0 && y + j < BOARD_SIZE)) + positions.Add(Tuple.Create(x + i, y + j)); + } + return positions; + } + } +} diff --git a/AOC2021/Models/AOCDay.cs b/AOC2021/Models/AOCDay.cs index 6e00c2e..5cf667f 100644 --- a/AOC2021/Models/AOCDay.cs +++ b/AOC2021/Models/AOCDay.cs @@ -12,7 +12,7 @@ namespace AOC2021.Models protected AOCResponse _response; protected ILogger _logger; private List _debugMessages; - + protected object Answer { set { this._response.Answer = value; } } public AOCDay() { this._response = new AOCResponse() diff --git a/AOC2021/Models/Day10/ChunkReader.cs b/AOC2021/Models/Day10/ChunkReader.cs new file mode 100644 index 0000000..550268a --- /dev/null +++ b/AOC2021/Models/Day10/ChunkReader.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AOC2021.Models.Day10 +{ + public class ChunkReader + { + private Stack _stack; + private string _inputLine; + public ChunkReader(string line) + { + this._inputLine = line; + this._stack = new Stack(); + } + + public ChunkReaderResult Process() + { + var result = new ChunkReaderResult() { Line = this._inputLine }; + foreach (char c in this._inputLine) + { + //Implies its an opening char so we sent to stack + if (isOpeningChar(c)) + { + _stack.Push(c); + continue; + } + if (_stack.Peek() == GetCorrespondingChar(c)) + { + _stack.Pop(); + } + else + { + result.CorruptedChar = c; + result.ExpectedChar = GetCorrespondingChar(_stack.Peek()); + result.isCorrupted = true; + break; + } + } + if (!result.isCorrupted) + { + if (this._stack.Count == 0) + result.isValid = true; + else + { + result.isIncomplete = true; + while (_stack.Count() > 0) + { + result.CompletionString += GetCorrespondingChar(_stack.Pop()); + } + } + } + return result; + } + + private bool isOpeningChar(char c) + { + if (c == '(' || c == '[' || c == '{' || c == '<') + return true; + return false; + } + + private char GetCorrespondingChar(char c) + { + switch (c) + { + case '(': + return ')'; + case '[': + return ']'; + case '{': + return '}'; + case '<': + return '>'; + case ')': + return '('; + case ']': + return '['; + case '}': + return '{'; + case '>': + return '<'; + } + throw new Exception("Invalid char given, no corresponing char found for: " + c); + } + } +} diff --git a/AOC2021/Models/Day10/ChunkReaderResult.cs b/AOC2021/Models/Day10/ChunkReaderResult.cs new file mode 100644 index 0000000..52b8e69 --- /dev/null +++ b/AOC2021/Models/Day10/ChunkReaderResult.cs @@ -0,0 +1,14 @@ + +namespace AOC2021.Models.Day10 +{ + public class ChunkReaderResult + { + public string Line { get; set; } + public bool isValid { get; set; } + public bool isIncomplete { get; set; } + public string CompletionString { get; set; } + public bool isCorrupted { get; set; } + public char CorruptedChar { get; set; } + public char ExpectedChar { get; set; } + } +} diff --git a/AOC2021/Models/Day11/Octopus.cs b/AOC2021/Models/Day11/Octopus.cs new file mode 100644 index 0000000..0c44c87 --- /dev/null +++ b/AOC2021/Models/Day11/Octopus.cs @@ -0,0 +1,39 @@ + +namespace AOC2021.Models.Day11 +{ + public class Octopus + { + public Octopus(int initialEnergy) + { + this.EnergyLevel = initialEnergy; + } + public int LastFlashedStep { get; set; } + public int EnergyLevel { get; set; } + public void IncreaseEnergy(int step) + { + if (step != LastFlashedStep) + EnergyLevel++; + } + + public bool CanFlash(int step) + { + if (EnergyLevel > 9 && step != LastFlashedStep) + { + return true; + } + return false; + } + + public void Flash(int step) + { + LastFlashedStep = step; + EnergyLevel = 0; + } + + public override string ToString() + { + return EnergyLevel.ToString(); + } + + } +}