From dbdd716f052d991e4f32b4aa0a444a15a6ffcd76 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sun, 4 Dec 2022 13:57:15 -0500 Subject: [PATCH] Updates to Runner Added Previous years of AOC --- AdventOfCode/AdventOfCode.csproj | 14 + .../Problems/AOC2015/Day10/LookAndSay.cs | 62 + .../Problems/AOC2015/Day5/NiceList.cs | 109 + AdventOfCode/Problems/AOC2015/Day5/input.txt | 1000 +++++++++ .../Problems/AOC2019/Day1/FuelCaluclation.cs | 48 + AdventOfCode/Problems/AOC2019/Day1/input.txt | 100 + AdventOfCode/Problems/AOC2019/Day2/IntCode.cs | 72 + AdventOfCode/Problems/AOC2019/Day2/input.csv | 1 + .../Problems/AOC2019/Day3/CrossedWires.cs | 328 +++ AdventOfCode/Problems/AOC2019/Day3/input.txt | 2 + .../Problems/AOC2019/Day4/SecureContainer.cs | 157 ++ .../AOC2019/Day5/ChanceOfAsteroids.cs | 36 + .../Problems/AOC2019/Day5/IntCodeV2.cs | 240 +++ AdventOfCode/Problems/AOC2019/Day5/input.csv | 1 + .../Problems/AOC2019/Day6/OrbitMap.cs | 119 ++ .../Problems/AOC2019/Day6/UniversalOrbits.cs | 60 + AdventOfCode/Problems/AOC2019/Day6/input.txt | 1799 +++++++++++++++++ AdventOfCode/Problems/AOC2019/Day6/test.txt | 13 + .../AOC2019/Day7/AmplificationCircuit.cs | 161 ++ AdventOfCode/Problems/AOC2019/Day7/input.csv | 1 + .../Problems/AOC2019/Day8/SpaceImageFormat.cs | 91 + AdventOfCode/Problems/AOC2019/Day8/input.txt | 1 + AdventOfCode/Problems/AOC2019/InputParsing.cs | 37 + .../Problems/AOC2021/Day11/DumboOctopus.cs | 200 ++ AdventOfCode/Problems/AOC2021/Day11/input.txt | 10 + .../Problems/AOC2021/Day11/input2.txt | 10 + .../Problems/AOC2021/Day3/BinaryDiagnostic.cs | 78 + AdventOfCode/Problems/AOC2021/Day3/Input.txt | 1000 +++++++++ AdventOfCode/Problems/AOC2021/Day3/sara.txt | 1000 +++++++++ AdventOfCode/Problems/AOC2021/Day3/test.txt | 12 + .../Problems/AOC2021/Day6/LanternFish.cs | 60 + AdventOfCode/Problems/AOC2021/Day6/input.txt | 1 + AdventOfCode/Problems/AOC2021/Day6/test.txt | 1 + .../Problems/AOC2022/Day0/TestProblem.cs | 2 +- .../Problems/AOC2022/Day1/CalorieCounting.cs | 2 +- .../AOC2022/Day2/RockPaperScissors.cs | 2 +- .../AOC2022/Day3/RucksackReorganization.cs | 2 +- .../Problems/AOC2023/Day0/TestProblem.cs | 2 +- AdventOfCode/Program.cs | 3 +- AdventOfCode/Runner/AOCRunner.cs | 33 +- .../Runner/Attributes/ProblemInfoAttribute.cs | 4 +- AdventOfCode/Runner/Problem.cs | 26 +- 42 files changed, 6875 insertions(+), 25 deletions(-) create mode 100644 AdventOfCode/Problems/AOC2015/Day10/LookAndSay.cs create mode 100644 AdventOfCode/Problems/AOC2015/Day5/NiceList.cs create mode 100644 AdventOfCode/Problems/AOC2015/Day5/input.txt create mode 100644 AdventOfCode/Problems/AOC2019/Day1/FuelCaluclation.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day1/input.txt create mode 100644 AdventOfCode/Problems/AOC2019/Day2/IntCode.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day2/input.csv create mode 100644 AdventOfCode/Problems/AOC2019/Day3/CrossedWires.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day3/input.txt create mode 100644 AdventOfCode/Problems/AOC2019/Day4/SecureContainer.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day5/ChanceOfAsteroids.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day5/IntCodeV2.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day5/input.csv create mode 100644 AdventOfCode/Problems/AOC2019/Day6/OrbitMap.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day6/UniversalOrbits.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day6/input.txt create mode 100644 AdventOfCode/Problems/AOC2019/Day6/test.txt create mode 100644 AdventOfCode/Problems/AOC2019/Day7/AmplificationCircuit.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day7/input.csv create mode 100644 AdventOfCode/Problems/AOC2019/Day8/SpaceImageFormat.cs create mode 100644 AdventOfCode/Problems/AOC2019/Day8/input.txt create mode 100644 AdventOfCode/Problems/AOC2019/InputParsing.cs create mode 100644 AdventOfCode/Problems/AOC2021/Day11/DumboOctopus.cs create mode 100644 AdventOfCode/Problems/AOC2021/Day11/input.txt create mode 100644 AdventOfCode/Problems/AOC2021/Day11/input2.txt create mode 100644 AdventOfCode/Problems/AOC2021/Day3/BinaryDiagnostic.cs create mode 100644 AdventOfCode/Problems/AOC2021/Day3/Input.txt create mode 100644 AdventOfCode/Problems/AOC2021/Day3/sara.txt create mode 100644 AdventOfCode/Problems/AOC2021/Day3/test.txt create mode 100644 AdventOfCode/Problems/AOC2021/Day6/LanternFish.cs create mode 100644 AdventOfCode/Problems/AOC2021/Day6/input.txt create mode 100644 AdventOfCode/Problems/AOC2021/Day6/test.txt diff --git a/AdventOfCode/AdventOfCode.csproj b/AdventOfCode/AdventOfCode.csproj index 073e1ca..33915d4 100644 --- a/AdventOfCode/AdventOfCode.csproj +++ b/AdventOfCode/AdventOfCode.csproj @@ -14,10 +14,24 @@ + + + PreserveNewest + + + + + + + + + + + diff --git a/AdventOfCode/Problems/AOC2015/Day10/LookAndSay.cs b/AdventOfCode/Problems/AOC2015/Day10/LookAndSay.cs new file mode 100644 index 0000000..a79bd07 --- /dev/null +++ b/AdventOfCode/Problems/AOC2015/Day10/LookAndSay.cs @@ -0,0 +1,62 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace AdventOfCode.Problems.AOC2015.Day10; +[ProblemInfo(2015, 10, "Evles Look, Elves Say")] +internal class LookAndSay : Problem +{ + private string _input = string.Empty; + + public override void CalculatePart1() + { + Part1 = Run(40); + } + + public override void CalculatePart2() + { + Part2 = Run(50); + } + + public override void LoadInput() + { + _input = "3113322113"; + } + + public int Run(int iter) + { + var value = new StringBuilder(_input); + for (int i = 0; i < iter; i++) + CalculateNext(ref value); + + return value.Length; + } + + private static void CalculateNext(ref StringBuilder input) + { + var next = new StringBuilder(); + var len = input.Length; + var curCount = 1; + var curChar = input[0]; + for (int i = 1; i < len; i++) + { + var c = input[i]; + if (c != curChar) + { + next.Append(curCount).Append(curChar); + curChar = c; + curCount = 1; + continue; + } + curCount++; + } + next.Append(curCount).Append(curChar); + + input = next; + } +} diff --git a/AdventOfCode/Problems/AOC2015/Day5/NiceList.cs b/AdventOfCode/Problems/AOC2015/Day5/NiceList.cs new file mode 100644 index 0000000..65da96d --- /dev/null +++ b/AdventOfCode/Problems/AOC2015/Day5/NiceList.cs @@ -0,0 +1,109 @@ +using AdventOfCode.Runner.Attributes; + +namespace AdventOfCode.Problems.AOC2015.Day5; + +[ProblemInfo(2015, 5, "Doesn't He Have Intern-Elves For This?")] +public class NiceList : Problem +{ + private string[] _inputData = Array.Empty(); + + public override void LoadInput() + { + _inputData = ReadInputLines(); + } + + public override void CalculatePart1() + { + for (int i = 0; i < _inputData.Length; i++) + { + if (IsNice(_inputData[i])) + Part1++; + } + } + + public override void CalculatePart2() + { + for (int i = 0; i < _inputData.Length; i++) + { + if (IsNice2(_inputData[i])) + { + Part2++; + } + } + } + private static bool IsNice2(string value) + { + var pairs = new Dictionary>(); + var separatedPair = false; + + for (int i = 1; i < value.Length; i++) + { + var c = value[i]; + var curIndex = i - 1; + var pair = value[curIndex..(i + 1)]; + if (pairs.ContainsKey(pair)) + { + if (pairs[pair].Contains(curIndex - 1)) + continue; + pairs[pair].Add(curIndex); + } + else + { + pairs.Add(pair, new List() { curIndex }); + } + + if (i == 1) + continue; + if (value[i - 2] == c) + separatedPair = true; + } + + return separatedPair && pairs.Any(p => p.Value.Count >= 2); + } + + private static bool IsNice(string value) + { + var vowelCount = 0; + var doubleLetters = false; + for (int i = 0; i < value.Length; i++) + { + char c = value[i]; + if (IsVowel(c)) + vowelCount++; + if (i == 0) + continue; + var lastChar = value[i - 1]; + if (IsIllegal(c, lastChar)) + return false; + if (IsDouble(c, lastChar)) + doubleLetters = true; + } + return doubleLetters && vowelCount >= 3; + } + + private static bool IsVowel(char c) + { + return c switch + { + 'a' or 'e' or 'i' or 'o' or 'u' => true, + _ => false + }; + } + + private static bool IsDouble(char c, char lastChar) + { + return c == lastChar; + } + + private static bool IsIllegal(char c, char lastChar) + { + return (lastChar, c) switch + { + ('a', 'b') => true, + ('c', 'd') => true, + ('p', 'q') => true, + ('x', 'y') => true, + _ => false + }; + } +} \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2015/Day5/input.txt b/AdventOfCode/Problems/AOC2015/Day5/input.txt new file mode 100644 index 0000000..a9b6e77 --- /dev/null +++ b/AdventOfCode/Problems/AOC2015/Day5/input.txt @@ -0,0 +1,1000 @@ +sszojmmrrkwuftyv +isaljhemltsdzlum +fujcyucsrxgatisb +qiqqlmcgnhzparyg +oijbmduquhfactbc +jqzuvtggpdqcekgk +zwqadogmpjmmxijf +uilzxjythsqhwndh +gtssqejjknzkkpvw +wrggegukhhatygfi +vhtcgqzerxonhsye +tedlwzdjfppbmtdx +iuvrelxiapllaxbg +feybgiimfthtplui +qxmmcnirvkzfrjwd +vfarmltinsriqxpu +oanqfyqirkraesfq +xilodxfuxphuiiii +yukhnchvjkfwcbiq +bdaibcbzeuxqplop +ivegnnpbiyxqsion +ybahkbzpditgwdgt +dmebdomwabxgtctu +ibtvimgfaeonknoh +jsqraroxudetmfyw +dqdbcwtpintfcvuz +tiyphjunlxddenpj +fgqwjgntxagidhah +nwenhxmakxqkeehg +zdoheaxqpcnlhnen +tfetfqojqcdzlpbm +qpnxkuldeiituggg +xwttlbdwxohahwar +hjkwzadmtrkegzye +koksqrqcfwcaxeof +wulwmrptktliyxeq +gyufbedqhhyqgqzj +txpunzodohikzlmj +jloqfuejfkemcrvu +amnflshcheuddqtc +pdvcsduggcogbiia +yrioavgfmeafjpcz +uyhbtmbutozzqfvq +mwhgfwsgyuwcdzik +auqylgxhmullxpaa +lgelzivplaeoivzh +uyvcepielfcmswoa +qhirixgwkkccuzlp +zoonniyosmkeejfg +iayfetpixkedyana +ictqeyzyqswdskiy +ejsgqteafvmorwxe +lhaiqrlqqwfbrqdx +ydjyboqwhfpqfydc +dwhttezyanrnbybv +edgzkqeqkyojowvr +rmjfdwsqamjqehdq +ozminkgnkwqctrxz +bztjhxpjthchhfcd +vrtioawyxkivrpiq +dpbcsznkpkaaclyy +vpoypksymdwttpvz +hhdlruwclartkyap +bqkrcbrksbzcggbo +jerbbbnxlwfvlaiw +dwkasufidwjrjfbf +kkfxtjhbnmqbmfwf +vmnfziwqxmioukmj +rqxvcultipkecdtu +fhmfdibhtjzkiqsd +hdpjbuzzbyafqrpd +emszboysjuvwwvts +msyigmwcuybfiooq +druyksfnbluvnwoh +fvgstvynnfbvxhsx +bmzalvducnqtuune +lzwkzfzttsvpllei +olmplpvjamynfyfd +padcwfkhystsvyfb +wjhbvxkwtbfqdilb +hruaqjwphonnterf +bufjobjtvxtzjpmj +oiedrjvmlbtwyyuy +sgiemafwfztwsyju +nsoqqfudrtwszyqf +vonbxquiiwxnazyl +yvnmjxtptujwqudn +rrnybqhvrcgwvrkq +taktoxzgotzxntfu +quffzywzpxyaepxa +rfvjebfiddcfgmwv +iaeozntougqwnzoh +scdqyrhoqmljhoil +bfmqticltmfhxwld +brbuktbyqlyfpsdl +oidnyhjkeqenjlhd +kujsaiqojopvrygg +vebzobmdbzvjnjtk +uunoygzqjopwgmbg +piljqxgicjzgifso +ikgptwcjzywswqnw +pujqsixoisvhdvwi +trtuxbgigogfsbbk +mplstsqclhhdyaqk +gzcwflvmstogdpvo +tfjywbkmimyyqcjd +gijutvhruqcsiznq +ibxkhjvzzxgavkha +btnxeqvznkxjsgmq +tjgofgauxaelmjoq +sokshvyhlkxerjrv +ltogbivktqmtezta +uduwytzvqvfluyuf +msuckpthtgzhdxan +fqmcglidvhvpirzr +gwztkqpcwnutvfga +bsjfgsrntdhlpqbx +xloczbqybxmiopwt +orvevzyjliomkkgu +mzjbhmfjjvaziget +tlsdxuhwdmghdyjb +atoecyjhwmznaewi +pyxpyvvipbqibiox +ajbfmpqqobfsmesj +siknbzefjblnohgd +eqfhgewbblwdfkmc +opylbscrotckkrbk +lbwxbofgjkzdxkle +ceixfjstaptdomvm +hnkrqxifjmmjktie +aqykzeuzvvetoygd +fouahjimfcisxima +prkzhutbqsyrhjzx +qqwliakathnsbzne +sayhgqtlcqqidqhj +ygduolbysehdudra +zricvxhdzznuxuce +ucvzakslykpgsixd +udirhgcttmyspgsb +yuwzppjzfsjhhdzi +gtqergjiuwookwre +xvxexbjyjkxovvwf +mlpaqhnnkqxrmwmm +ezuqbrjozwuqafhb +mcarusdthcbsonoq +weeguqeheeiigrue +pngtfugozxofaqxv +copphvbjcmfspenv +jiyahihykjjkdaya +gdqnmesvptuyrfwp +vbdscfywqmfxbohh +crtrfuxyjypzubrg +seihvevtxywxhflp +fvvpmgttnapklwou +qmqaqsajmqwhetpk +zetxvrgjmblxvakr +kpvwblrizaabmnhz +mwpvvzaaicntrkcp +clqyjiegtdsswqfm +ymrcnqgcpldgfwtm +nzyqpdenetncgnwq +cmkzevgacnmdkqro +kzfdsnamjqbeirhi +kpxrvgvvxapqlued +rzskbnfobevzrtqu +vjoahbfwtydugzap +ykbbldkoijlvicbl +mfdmroiztsgjlasb +quoigfyxwtwprmdr +ekxjqafwudgwfqjm +obtvyjkiycxfcdpb +lhoihfnbuqelthof +eydwzitgxryktddt +rxsihfybacnpoyny +bsncccxlplqgygtw +rvmlaudsifnzhcqh +huxwsyjyebckcsnn +gtuqzyihwhqvjtes +zreeyomtngvztveq +nwddzjingsarhkxb +nuqxqtctpoldrlsh +wkvnrwqgjooovhpf +kwgueyiyffudtbyg +tpkzapnjxefqnmew +ludwccvkihagvxal +lfdtzhfadvabghna +njqmlsnrkcfhtvbb +cajzbqleghhnlgap +vmitdcozzvqvzatp +eelzefwqwjiywbcz +uyztcuptfqvymjpi +aorhnrpkjqqtgnfo +lfrxfdrduoeqmwwp +vszpjvbctblplinh +zexhadgpqfifcqrz +ueirfnshekpemqua +qfremlntihbwabtb +nwznunammfexltjc +zkyieokaaogjehwt +vlrxgkpclzeslqkq +xrqrwfsuacywczhs +olghlnfjdiwgdbqc +difnlxnedpqcsrdf +dgpuhiisybjpidsj +vlwmwrikmitmoxbt +sazpcmcnviynoktm +pratafauetiknhln +ilgteekhzwlsfwcn +ywvwhrwhkaubvkbl +qlaxivzwxyhvrxcf +hbtlwjdriizqvjfb +nrmsononytuwslsa +mpxqgdthpoipyhjc +mcdiwmiqeidwcglk +vfbaeavmjjemfrmo +qzcbzmisnynzibrc +shzmpgxhehhcejhb +wirtjadsqzydtyxd +qjlrnjfokkqvnpue +dxawdvjntlbxtuqc +wttfmnrievfestog +eamjfvsjhvzzaobg +pbvfcwzjgxahlrag +omvmjkqqnobvnzkn +lcwmeibxhhlxnkzv +uiaeroqfbvlazegs +twniyldyuonfyzqw +wgjkmsbwgfotdabi +hnomamxoxvrzvtew +ycrcfavikkrxxfgw +isieyodknagzhaxy +mgzdqwikzullzyco +mumezgtxjrrejtrs +nwmwjcgrqiwgfqel +wjgxmebfmyjnxyyp +durpspyljdykvzxf +zuslbrpooyetgafh +kuzrhcjwbdouhyme +wyxuvbciodscbvfm +kbnpvuqwmxwfqtqe +zddzercqogdpxmft +sigrdchxtgavzzjh +lznjolnorbuddgcs +ycnqabxlcajagwbt +bnaudeaexahdgxsj +rlnykxvoctfwanms +jngyetkoplrstfzt +tdpxknwacksotdub +yutqgssfoptvizgr +lzmqnxeqjfnsxmsa +iqpgfsfmukovsdgu +qywreehbidowtjyz +iozamtgusdctvnkw +ielmujhtmynlwcfd +hzxnhtbnmmejlkyf +ftbslbzmiqkzebtd +bcwdqgiiizmohack +dqhfkzeddjzbdlxu +mxopokqffisxosci +vciatxhtuechbylk +khtkhcvelidjdena +blatarwzfqcapkdt +elamngegnczctcck +xeicefdbwrxhuxuf +sawvdhjoeahlgcdr +kmdcimzsfkdfpnir +axjayzqlosrduajb +mfhzreuzzumvoggr +iqlbkbhrkptquldb +xcvztvlshiefuhgb +pkvwyqmyoazocrio +ajsxkdnerbmhyxaj +tudibgsbnpnizvsi +cxuiydkgdccrqvkh +cyztpjesdzmbcpot +nnazphxpanegwitx +uphymczbmjalmsct +yyxiwnlrogyzwqmg +gmqwnahjvvdyhnfa +utolskxpuoheugyl +mseszdhyzoyavepd +ycqknvbuvcjfgmlc +sknrxhxbfpvpeorn +zqxqjetooqcodwml +sesylkpvbndrdhsy +fryuxvjnsvnjrxlw +mfxusewqurscujnu +mbitdjjtgzchvkfv +ozwlyxtaalxofovd +wdqcduaykxbunpie +rlnhykxiraileysk +wgoqfrygttlamobg +kflxzgxvcblkpsbz +tmkisflhativzhde +owsdrfgkaamogjzd +gaupjkvkzavhfnes +wknkurddcknbdleg +lltviwincmbtduap +qwzvspgbcksyzzmb +ydzzkumecryfjgnk +jzvmwgjutxoysaam +icrwpyhxllbardkr +jdopyntshmvltrve +afgkigxcuvmdbqou +mfzzudntmvuyhjzt +duxhgtwafcgrpihc +tsnhrkvponudumeb +sqtvnbeiigdzbjgv +eczmkqwvnsrracuo +mhehsgqwiczaiaxv +kaudmfvifovrimpd +lupikgivechdbwfr +mwaaysrndiutuiqx +aacuiiwgaannunmm +tjqjbftaqitukwzp +lrcqyskykbjpaekn +lirrvofbcqpjzxmr +jurorvzpplyelfml +qonbllojmloykjqe +sllkzqujfnbauuqp +auexjwsvphvikali +usuelbssqmbrkxyc +wyuokkfjexikptvv +wmfedauwjgbrgytl +sfwvtlzzebxzmuvw +rdhqxuechjsjcvaf +kpavhqkukugocsxu +ovnjtumxowbxduts +zgerpjufauptxgat +pevvnzjfwhjxdoxq +pmmfwxajgfziszcs +difmeqvaghuitjhs +icpwjbzcmlcterwm +ngqpvhajttxuegyh +mosjlqswdngwqsmi +frlvgpxrjolgodlu +eazwgrpcxjgoszeg +bbtsthgkjrpkiiyk +tjonoglufuvsvabe +xhkbcrofytmbzrtk +kqftfzdmpbxjynps +kmeqpocbnikdtfyv +qjjymgqxhnjwxxhp +dmgicrhgbngdtmjt +zdxrhdhbdutlawnc +afvoekuhdboxghvx +hiipezngkqcnihty +bbmqgheidenweeov +suprgwxgxwfsgjnx +adeagikyamgqphrj +zzifqinoeqaorjxg +adhgppljizpaxzld +lvxyieypvvuqjiyc +nljoakatwwwoovzn +fcrkfxclcacshhmx +ownnxqtdhqbgthch +lmfylrcdmdkgpwnj +hlwjfbvlswbzpbjr +mkofhdtljdetcyvp +synyxhifbetzarpo +agnggugngadrcxoc +uhttadmdmhidpyjw +ohfwjfhunalbubpr +pzkkkkwrlvxiuysn +kmidbxmyzkjrwjhu +egtitdydwjxmajnw +civoeoiuwtwgbqqs +dfptsguzfinqoslk +tdfvkreormspprer +zvnvbrmthatzztwi +ffkyddccrrfikjde +hrrmraevdnztiwff +qaeygykcpbtjwjbr +purwhitkmrtybslh +qzziznlswjaussel +dfcxkvdpqccdqqxj +tuotforulrrytgyn +gmtgfofgucjywkev +wkyoxudvdkbgpwhd +qbvktvfvipftztnn +otckgmojziezmojb +inxhvzbtgkjxflay +qvxapbiatuudseno +krpvqosbesnjntut +oqeukkgjsfuqkjbb +prcjnyymnqwqksiz +vuortvjxgckresko +orqlyobvkuwgathr +qnpyxlnazyfuijox +zwlblfkoklqmqzkw +hmwurwtpwnrcsanl +jzvxohuakopuzgpf +sfcpnxrviphhvxmx +qtwdeadudtqhbely +dbmkmloasqphnlgj +olylnjtkxgrubmtk +nxsdbqjuvwrrdbpq +wbabpirnpcsmpipw +hjnkyiuxpqrlvims +enzpntcjnxdpuqch +vvvqhlstzcizyimn +triozhqndbttglhv +fukvgteitwaagpzx +uhcvukfbmrvskpen +tizcyupztftzxdmt +vtkpnbpdzsaluczz +wodfoyhoekidxttm +otqocljrmwfqbxzu +linfbsnfvixlwykn +vxsluutrwskslnye +zbshygtwugixjvsi +zdcqwxvwytmzhvoo +wrseozkkcyctrmei +fblgtvogvkpqzxiy +opueqnuyngegbtnf +qxbovietpacqqxok +zacrdrrkohfygddn +gbnnvjqmkdupwzpq +qgrgmsxeotozvcak +hnppukzvzfmlokid +dzbheurndscrrtcl +wbgdkadtszebbrcw +fdmzppzphhpzyuiz +bukomunhrjrypohj +ohodhelegxootqbj +rsplgzarlrknqjyh +punjjwpsxnhpzgvu +djdfahypfjvpvibm +mlgrqsmhaozatsvy +xwktrgyuhqiquxgn +wvfaoolwtkbrisvf +plttjdmguxjwmeqr +zlvvbwvlhauyjykw +cigwkbyjhmepikej +masmylenrusgtyxs +hviqzufwyetyznze +nzqfuhrooswxxhus +pdbdetaqcrqzzwxf +oehmvziiqwkzhzib +icgpyrukiokmytoy +ooixfvwtiafnwkce +rvnmgqggpjopkihs +wywualssrmaqigqk +pdbvflnwfswsrirl +jeaezptokkccpbuj +mbdwjntysntsaaby +ldlgcawkzcwuxzpz +lwktbgrzswbsweht +ecspepmzarzmgpjm +qmfyvulkmkxjncai +izftypvwngiukrns +zgmnyjfeqffbooww +nyrkhggnprhedows +yykzzrjmlevgffah +mavaemfxhlfejfki +cmegmfjbkvpncqwf +zxidlodrezztcrij +fseasudpgvgnysjv +fupcimjupywzpqzp +iqhgokavirrcvyys +wjmkcareucnmfhui +nftflsqnkgjaexhq +mgklahzlcbapntgw +kfbmeavfxtppnrxn +nuhyvhknlufdynvn +nviogjxbluwrcoec +tyozixxxaqiuvoys +kgwlvmvgtsvxojpr +moeektyhyonfdhrb +kahvevmmfsmiiqex +xcywnqzcdqtvhiwd +fnievhiyltbvtvem +jlmndqufirwgtdxd +muypbfttoeelsnbs +rypxzbnujitfwkou +ubmmjbznskildeoj +ofnmizdeicrmkjxp +rekvectjbmdnfcib +yohrojuvdexbctdh +gwfnfdeibynzjmhz +jfznhfcqdwlpjull +scrinzycfhwkmmso +mskutzossrwoqqsi +rygoebkzgyzushhr +jpjqiycflqkexemx +arbufysjqmgaapnl +dbjerflevtgweeoj +snybnnjlmwjvhois +fszuzplntraprmbj +mkvaatolvuggikvg +zpuzuqygoxesnuyc +wnpxvmxvllxalulm +eivuuafkvudeouwy +rvzckdyixetfuehr +qgmnicdoqhveahyx +miawwngyymshjmpj +pvckyoncpqeqkbmx +llninfenrfjqxurv +kzbjnlgsqjfuzqtp +rveqcmxomvpjcwte +bzotkawzbopkosnx +ktqvpiribpypaymu +wvlzkivbukhnvram +uohntlcoguvjqqdo +ajlsiksjrcnzepkt +xsqatbldqcykwusd +ihbivgzrwpmowkop +vfayesfojmibkjpb +uaqbnijtrhvqxjtb +hhovshsfmvkvymba +jerwmyxrfeyvxcgg +hncafjwrlvdcupma +qyvigggxfylbbrzt +hiiixcyohmvnkpgk +mmitpwopgxuftdfu +iaxderqpceboixoa +zodfmjhuzhnsqfcb +sthtcbadrclrazsi +bkkkkcwegvypbrio +wmpcofuvzemunlhj +gqwebiifvqoeynro +juupusqdsvxcpsgv +rbhdfhthxelolyse +kjimpwnjfrqlqhhz +rcuigrjzarzpjgfq +htxcejfyzhydinks +sxucpdxhvqjxxjwf +omsznfcimbcwaxal +gufmtdlhgrsvcosb +bssshaqujtmluerz +uukotwjkstgwijtr +kbqkneobbrdogrxk +ljqopjcjmelgrakz +rwtfnvnzryujwkfb +dedjjbrndqnilbeh +nzinsxnpptzagwlb +lwqanydfirhnhkxy +hrjuzfumbvfccxno +okismsadkbseumnp +sfkmiaiwlktxqvwa +hauwpjjwowbunbjj +nowkofejwvutcnui +bqzzppwoslaeixro +urpfgufwbtzenkpj +xgeszvuqwxeykhef +yxoldvkyuikwqyeq +onbbhxrnmohzskgg +qcikuxakrqeugpoa +lnudcqbtyzhlpers +nxduvwfrgzaailgl +xniuwvxufzxjjrwz +ljwithcqmgvntjdj +awkftfagrfzywkhs +uedtpzxyubeveuek +bhcqdwidbjkqqhzl +iyneqjdmlhowwzxx +kvshzltcrrururty +zgfpiwajegwezupo +tkrvyanujjwmyyri +ercsefuihcmoaiep +ienjrxpmetinvbos +jnwfutjbgenlipzq +bgohjmrptfuamzbz +rtsyamajrhxbcncw +tfjdssnmztvbnscs +bgaychdlmchngqlp +kfjljiobynhwfkjo +owtdxzcpqleftbvn +ltjtimxwstvzwzjj +wbrvjjjajuombokf +zblpbpuaqbkvsxye +gwgdtbpnlhyqspdi +abipqjihjqfofmkx +nlqymnuvjpvvgova +avngotmhodpoufzn +qmdyivtzitnrjuae +xfwjmqtqdljuerxi +csuellnlcyqaaamq +slqyrcurcyuoxquo +dcjmxyzbzpohzprl +uqfnmjwniyqgsowb +rbmxpqoblyxdocqc +ebjclrdbqjhladem +ainnfhxnsgwqnmyo +eyytjjwhvodtzquf +iabjgmbbhilrcyyp +pqfnehkivuelyccc +xgjbyhfgmtseiimt +jwxyqhdbjiqqqeyy +gxsbrncqkmvaryln +vhjisxjkinaejytk +seexagcdmaedpcvh +lvudfgrcpjxzdpvd +fxtegyrqjzhmqean +dnoiseraqcoossmc +nwrhmwwbykvwmgep +udmzskejvizmtlce +hbzvqhvudfdlegaa +cghmlfqejbxewskv +bntcmjqfwomtbwsb +qezhowyopjdyhzng +todzsocdkgfxanbz +zgjkssrjlwxuhwbk +eibzljqsieriyrzr +wamxvzqyycrxotjp +epzvfkispwqynadu +dwlpfhtrafrxlyie +qhgzujhgdruowoug +girstvkahaemmxvh +baitcrqmxhazyhbl +xyanqcchbhkajdmc +gfvjmmcgfhvgnfdq +tdfdbslwncbnkzyz +jojuselkpmnnbcbb +hatdslkgxtqpmavj +dvelfeddvgjcyxkj +gnsofhkfepgwltse +mdngnobasfpewlno +qssnbcyjgmkyuoga +glvcmmjytmprqwvn +gwrixumjbcdffsdl +lozravlzvfqtsuiq +sicaflbqdxbmdlch +inwfjkyyqbwpmqlq +cuvszfotxywuzhzi +igfxyoaacoarlvay +ucjfhgdmnjvgvuni +rvvkzjsytqgiposh +jduinhjjntrmqroz +yparkxbgsfnueyll +lyeqqeisxzfsqzuj +woncskbibjnumydm +lltucklragtjmxtl +ubiyvmyhlesfxotj +uecjseeicldqrqww +xxlxkbcthufnjbnm +lhqijovvhlffpxga +fzdgqpzijitlogjz +efzzjqvwphomxdpd +jvgzvuyzobeazssc +hejfycgxywfjgbfw +yhjjmvkqfbnbliks +sffvfyywtlntsdsz +dwmxqudvxqdenrur +asnukgppdemxrzaz +nwqfnumblwvdpphx +kqsmkkspqvxzuket +cpnraovljzqiquaz +qrzgrdlyyzbyykhg +opoahcbiydyhsmqe +hjknnfdauidjeydr +hczdjjlygoezadow +rtflowzqycimllfv +sfsrgrerzlnychhq +bpahuvlblcolpjmj +albgnjkgmcrlaicl +pijyqdhfxpaxzdex +eeymiddvcwkpbpux +rqwkqoabywgggnln +vckbollyhgbgmgwh +ylzlgvnuvpynybkm +hpmbxtpfosbsjixt +ocebeihnhvkhjfqz +tvctyxoujdgwayze +efvhwxtuhapqxjen +rusksgefyidldmpo +nkmtjvddfmhirmzz +whvtsuadwofzmvrt +iiwjqvsdxudhdzzk +gucirgxaxgcassyo +rmhfasfzexeykwmr +hynlxcvsbgosjbis +huregszrcaocueen +pifezpoolrnbdqtv +unatnixzvdbqeyox +xtawlpduxgacchfe +bdvdbflqfphndduf +xtdsnjnmzccfptyt +nkhsdkhqtzqbphhg +aqcubmfkczlaxiyb +moziflxpsfubucmv +srdgnnjtfehiimqx +pwfalehdfyykrohf +sysxssmvewyfjrve +brsemdzosgqvvlxe +bimbjoshuvflkiat +hkgjasmljkpkwwku +sbnmwjvodygobpqc +bbbqycejueruihhd +corawswvlvneipyc +gcyhknmwsczcxedh +kppakbffdhntmcqp +ynulzwkfaemkcefp +pyroowjekeurlbii +iwksighrswdcnmxf +glokrdmugreygnsg +xkmvvumnfzckryop +aesviofpufygschi +csloawlirnegsssq +fkqdqqmlzuxbkzbc +uzlhzcfenxdfjdzp +poaaidrktteusvyf +zrlyfzmjzfvivcfr +qwjulskbniitgqtx +gjeszjksbfsuejki +vczdejdbfixbduaq +knjdrjthitjxluth +jweydeginrnicirl +bottrfgccqhyycsl +eiquffofoadmbuhk +lbqfutmzoksscswf +xfmdvnvfcnzjprba +uvugkjbkhlaoxmyx +wadlgtpczgvcaqqv +inzrszbtossflsxk +dbzbtashaartczrj +qbjiqpccefcfkvod +hluujmokjywotvzy +thwlliksfztcmwzh +arahybspdaqdexrq +nuojrmsgyipdvwyx +hnajdwjwmzattvst +sulcgaxezkprjbgu +rjowuugwdpkjtypw +oeugzwuhnrgiaqga +wvxnyymwftfoswij +pqxklzkjpcqscvde +tuymjzknntekglqj +odteewktugcwlhln +exsptotlfecmgehc +eeswfcijtvzgrqel +vjhrkiwmunuiwqau +zhlixepkeijoemne +pavfsmwesuvebzdd +jzovbklnngfdmyws +nbajyohtzfeoiixz +ciozmhrsjzrwxvhz +gwucrxieqbaqfjuv +uayrxrltnohexawc +flmrbhwsfbcquffm +gjyabmngkitawlxc +rwwtggvaygfbovhg +xquiegaisynictjq +oudzwuhexrwwdbyy +lengxmguyrwhrebb +uklxpglldbgqsjls +dbmvlfeyguydfsxq +zspdwdqcrmtmdtsc +mqfnzwbfqlauvrgc +amcrkzptgacywvhv +ndxmskrwrqysrndf +mwjyhsufeqhwisju +srlrukoaenyevykt +tnpjtpwawrxbikct +geczalxmgxejulcv +tvkcbqdhmuwcxqci +tiovluvwezwwgaox +zrjhtbgajkjqzmfo +vcrywduwsklepirs +lofequdigsszuioy +wxsdzomkjqymlzat +iabaczqtrfbmypuy +ibdlmudbajikcncr +rqcvkzsbwmavdwnv +ypxoyjelhllhbeog +fdnszbkezyjbttbg +uxnhrldastpdjkdz +xfrjbehtxnlyzcka +omjyfhbibqwgcpbv +eguucnoxaoprszmp +xfpypldgcmcllyzz +aypnmgqjxjqceelv +mgzharymejlafvgf +tzowgwsubbaigdok +ilsehjqpcjwmylxc +pfmouwntfhfnmrwk +csgokybgdqwnduwp +eaxwvxvvwbrovypz +nmluqvobbbmdiwwb +lnkminvfjjzqbmio +mjiiqzycqdhfietz +towlrzriicyraevq +obiloewdvbrsfwjo +lmeooaajlthsfltw +ichygipzpykkesrw +gfysloxmqdsfskvt +saqzntehjldvwtsx +pqddoemaufpfcaew +mjrxvbvwcreaybwe +ngfbrwfqnxqosoai +nesyewxreiqvhald +kqhqdlquywotcyfy +liliptyoqujensfi +nsahsaxvaepzneqq +zaickulfjajhctye +gxjzahtgbgbabtht +koxbuopaqhlsyhrp +jhzejdjidqqtjnwe +dekrkdvprfqpcqki +linwlombdqtdeyop +dvckqqbnigdcmwmx +yaxygbjpzkvnnebv +rlzkdkgaagmcpxah +cfzuyxivtknirqvt +obivkajhsjnrxxhn +lmjhayymgpseuynn +bbjyewkwadaipyju +lmzyhwomfypoftuu +gtzhqlgltvatxack +jfflcfaqqkrrltgq +txoummmnzfrlrmcg +ohemsbfuqqpucups +imsfvowcbieotlok +tcnsnccdszxfcyde +qkcdtkwuaquajazz +arcfnhmdjezdbqku +srnocgyqrlcvlhkb +mppbzvfmcdirbyfw +xiuarktilpldwgwd +ypufwmhrvzqmexpc +itpdnsfkwgrdujmj +cmpxnodtsswkyxkr +wayyxtjklfrmvbfp +mfaxphcnjczhbbwy +sjxhgwdnqcofbdra +pnxmujuylqccjvjm +ivamtjbvairwjqwl +deijtmzgpfxrclss +bzkqcaqagsynlaer +tycefobvxcvwaulz +ctbhnywezxkdsswf +urrxxebxrthtjvib +fpfelcigwqwdjucv +ngfcyyqpqulwcphb +rltkzsiipkpzlgpw +qfdsymzwhqqdkykc +balrhhxipoqzmihj +rnwalxgigswxomga +ghqnxeogckshphgr +lyyaentdizaumnla +exriodwfzosbeoib +speswfggibijfejk +yxmxgfhvmshqszrq +hcqhngvahzgawjga +qmhlsrfpesmeksur +eviafjejygakodla +kvcfeiqhynqadbzv +fusvyhowslfzqttg +girqmvwmcvntrwau +yuavizroykfkdekz +jmcwohvmzvowrhxf +kzimlcpavapynfue +wjudcdtrewfabppq +yqpteuxqgbmqfgxh +xdgiszbuhdognniu +jsguxfwhpftlcjoh +whakkvspssgjzxre +ggvnvjurlyhhijgm +krvbhjybnpemeptr +pqedgfojyjybfbzr +jzhcrsgmnkwwtpdo +yyscxoxwofslncmp +gzjhnxytmyntzths +iteigbnqbtpvqumi +zjevfzusnjukqpfw +xippcyhkfuounxqk +mcnhrcfonfdgpkyh +pinkcyuhjkexbmzj +lotxrswlxbxlxufs +fmqajrtoabpckbnu +wfkwsgmcffdgaqxg +qfrsiwnohoyfbidr +czfqbsbmiuyusaqs +ieknnjeecucghpoo +cevdgqnugupvmsge +gjkajcyjnxdrtuvr +udzhrargnujxiclq +zqqrhhmjwermjssg +ggdivtmgoqajydzz +wnpfsgtxowkjiivl +afbhqawjbotxnqpd +xjpkifkhfjeqifdn +oyfggzsstfhvticp +kercaetahymeawxy +khphblhcgmbupmzt +iggoqtqpvaebtiol +ofknifysuasshoya +qxuewroccsbogrbv +apsbnbkiopopytgu +zyahfroovfjlythh +bxhjwfgeuxlviydq +uvbhdtvaypasaswa +qamcjzrmesqgqdiz +hjnjyzrxntiycyel +wkcrwqwniczwdxgq +hibxlvkqakusswkx +mzjyuenepwdgrkty +tvywsoqslfsulses +jqwcwuuisrclircv +xanwaoebfrzhurct +ykriratovsvxxasf +qyebvtqqxbjuuwuo +telrvlwvriylnder +acksrrptgnhkeiaa +yemwfjhiqlzsvdxf +banrornfkcymmkcc +ytbhxvaeiigjpcgm +crepyazgxquposkn +xlqwdrytzwnxzwzv +xtrbfbwopxscftps +kwbytzukgseeyjla +qtfdvavvjogybxjg +ytbmvmrcxwfkgvzw +nbscbdskdeocnfzr +sqquwjbdxsxhcseg +ewqxhigqcgszfsuw +cvkyfcyfmubzwsee +dcoawetekigxgygd +ohgqnqhfimyuqhvi +otisopzzpvnhctte +bauieohjejamzien +ewnnopzkujbvhwce +aeyqlskpaehagdiv +pncudvivwnnqspxy +ytugesilgveokxcg +zoidxeelqdjesxpr +ducjccsuaygfchzj +smhgllqqqcjfubfc +nlbyyywergronmir +prdawpbjhrzsbsvj +nmgzhnjhlpcplmui +eflaogtjghdjmxxz +qolvpngucbkprrdc +ixywxcienveltgho +mwnpqtocagenkxut +iskrfbwxonkguywx +ouhtbvcaczqzmpua +srewprgddfgmdbao +dyufrltacelchlvu +czmzcbrkecixuwzz +dtbeojcztzauofuk +prrgoehpqhngfgmw +baolzvfrrevxsyke +zqadgxshwiarkzwh +vsackherluvurqqj +surbpxdulvcvgjbd +wqxytarcxzgxhvtx +vbcubqvejcfsgrac +zqnjfeapshjowzja +hekvbhtainkvbynx +knnugxoktxpvoxnh +knoaalcefpgtvlwm +qoakaunowmsuvkus +ypkvlzcduzlezqcb +ujhcagawtyepyogh +wsilcrxncnffaxjf +gbbycjuscquaycrk +aduojapeaqwivnly +ceafyxrakviagcjy +nntajnghicgnrlst +vdodpeherjmmvbje +wyyhrnegblwvdobn +xlfurpghkpbzhhif +xyppnjiljvirmqjo +kglzqahipnddanpi +omjateouxikwxowr +ocifnoopfglmndcx +emudcukfbadyijev +ooktviixetfddfmh +wtvrhloyjewdeycg +cgjncqykgutfjhvb +nkwvpswppeffmwad +hqbcmfhzkxmnrivg +mdskbvzguxvieilr +anjcvqpavhdloaqh +erksespdevjylenq +fadxwbmisazyegup +iyuiffjmcaahowhj +ygkdezmynmltodbv +fytneukxqkjattvh +woerxfadbfrvdcnz +iwsljvkyfastccoa +movylhjranlorofe +drdmicdaiwukemep +knfgtsmuhfcvvshg +ibstpbevqmdlhajn +tstwsswswrxlzrqs +estyydmzothggudf +jezogwvymvikszwa +izmqcwdyggibliet +nzpxbegurwnwrnca +kzkojelnvkwfublh +xqcssgozuxfqtiwi +tcdoigumjrgvczfv +ikcjyubjmylkwlwq +kqfivwystpqzvhan +bzukgvyoqewniivj +iduapzclhhyfladn +fbpyzxdfmkrtfaeg +yzsmlbnftftgwadz diff --git a/AdventOfCode/Problems/AOC2019/Day1/FuelCaluclation.cs b/AdventOfCode/Problems/AOC2019/Day1/FuelCaluclation.cs new file mode 100644 index 0000000..5c65e86 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day1/FuelCaluclation.cs @@ -0,0 +1,48 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; + +namespace AdventOfCode.Problems.AOC2019.Day1 +{ + [ProblemInfo(2019, 1, "The Tyranny of the Rocket Equation")] + public class FuelCaluclation : Problem + { + private int[] _input = Array.Empty(); + + public static int GetFuelRequirement(int[] input) + { + var curFuel = input.Sum(i => GetFuelCost(i)); + return curFuel; + } + + public static int GetFuelCost(int mass) + { + var curCost = mass / 3 - 2; + if (curCost <= 0) + return 0; + return curCost + GetFuelCost(curCost); + } + + public static int GetCost(int mass) => mass/ 3 - 2; + + public override void LoadInput() + { + _input = InputParsing.ParseIntArray(GetInputFile()); + } + + public override void CalculatePart1() + { + Part1 = _input.Sum(i => GetCost(i)); + } + + public override void CalculatePart2() + { + + Part2 = GetFuelRequirement(_input); + } + } +} diff --git a/AdventOfCode/Problems/AOC2019/Day1/input.txt b/AdventOfCode/Problems/AOC2019/Day1/input.txt new file mode 100644 index 0000000..4daaecb --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day1/input.txt @@ -0,0 +1,100 @@ +102480 +121446 +118935 +54155 +102510 +142419 +73274 +57571 +123916 +99176 +143124 +141318 +72224 +145479 +97027 +126427 +94990 +100521 +105589 +123009 +77143 +142861 +92366 +66478 +102195 +128373 +128447 +120178 +99122 +98671 +89541 +125720 +107984 +126544 +145231 +110241 +123926 +72793 +76705 +128338 +74262 +68845 +65297 +112536 +59892 +57115 +73230 +80569 +146118 +108843 +59221 +140492 +122616 +140652 +64404 +99782 +104375 +86926 +143145 +114969 +108948 +77236 +143655 +71406 +97588 +64892 +105345 +104393 +93442 +54525 +94116 +123606 +106813 +59904 +149253 +81620 +80892 +66309 +142604 +97984 +79743 +79448 +123756 +64927 +139703 +71448 +135964 +86083 +94767 +116856 +73786 +141083 +122581 +82239 +122282 +96092 +80029 +52957 +72062 +52124 diff --git a/AdventOfCode/Problems/AOC2019/Day2/IntCode.cs b/AdventOfCode/Problems/AOC2019/Day2/IntCode.cs new file mode 100644 index 0000000..0fed8bf --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day2/IntCode.cs @@ -0,0 +1,72 @@ +using AdventOfCode.Runner.Attributes; + +namespace AdventOfCode.Problems.AOC2019.Day2 +{ + [ProblemInfo(2019, 2, "Program Alarm")] + public class IntCode : Problem + { + private int[] _inputPart1 = Array.Empty(); + private int[] _inputPart2 = Array.Empty(); + + public static int ExecuteCode(int[] code, int noun, int verb) + { + int[] memory = code; + memory[1] = noun; + memory[2] = verb; + var curAddr = 0; + + while (true) + { + var opCode = memory[curAddr]; + + if (opCode == 99) //Halt + return memory[0]; + + //Working Adresses + int a = memory[curAddr + 1], b = memory[curAddr + 2], c = memory[curAddr + 3]; + + if (a > memory.Length || b > memory.Length || c > memory.Length) + { + Console.WriteLine("ERROR: Out of Bounds"); + return 0; + } + + if (opCode == 1) //Add + memory[c] = memory[a] + memory[b]; + if (opCode == 2) //Multiply + memory[c] = memory[a] * memory[b]; + + curAddr += 4; + } + } + + public override void LoadInput() + { + _inputPart1 = InputParsing.ParseIntCsv(GetInputFile("input.csv")); + _inputPart2 = InputParsing.ParseIntCsv(GetInputFile("input.csv")); + } + + public override void CalculatePart1() + { + Part1 = ExecuteCode(_inputPart1, 12, 2); + } + + public override void CalculatePart2() + { + int targetOutput = 19690720; + for (int n = 0; n < 100; n++) + { + for (int v = 0; v < 100; v++) + { + var curInput = new int[_inputPart2.Length]; + Array.Copy(_inputPart2, curInput, _inputPart2.Length); + if (ExecuteCode(curInput, n, v) == targetOutput) + { + Part2 = 100 * n + v; + return; + } + } + } + } + } +} \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2019/Day2/input.csv b/AdventOfCode/Problems/AOC2019/Day2/input.csv new file mode 100644 index 0000000..b1aa1ea --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day2/input.csv @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,5,23,2,23,9,27,1,5,27,31,1,9,31,35,1,35,10,39,2,13,39,43,1,43,9,47,1,47,9,51,1,6,51,55,1,13,55,59,1,59,13,63,1,13,63,67,1,6,67,71,1,71,13,75,2,10,75,79,1,13,79,83,1,83,10,87,2,9,87,91,1,6,91,95,1,9,95,99,2,99,10,103,1,103,5,107,2,6,107,111,1,111,6,115,1,9,115,119,1,9,119,123,2,10,123,127,1,127,5,131,2,6,131,135,1,135,5,139,1,9,139,143,2,143,13,147,1,9,147,151,1,151,2,155,1,9,155,0,99,2,0,14,0 diff --git a/AdventOfCode/Problems/AOC2019/Day3/CrossedWires.cs b/AdventOfCode/Problems/AOC2019/Day3/CrossedWires.cs new file mode 100644 index 0000000..0bd97d6 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day3/CrossedWires.cs @@ -0,0 +1,328 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Net.Http.Headers; +using System.Text; + +namespace AdventOfCode.Problems.AOC2019.Day3 +{ + [ProblemInfo(2019, 3, "Crossed Wires")] + public class CrossedWires : Problem + { + private string[] _inputLines = Array.Empty(); + + public struct WireSegment + { + public Point min, max; + public Point start, end; + + public int Length; + + public bool Vertical => min.X == max.X; + + public WireSegment(Point a, Point b) + { + start = a; + end = b; + if (a.X == b.X) //Vertical + { + if (a.Y < b.Y) + { + min = a; + max = b; + } + else + { + min = b; + max = a; + } + Length = Math.Abs(b.Y - a.Y); + } + else + { + if (a.X < b.X) + { + min = a; + max = b; + } + else + { + min = b; + max = a; + } + Length = Math.Abs(b.X - a.X); + } + } + + public WireSegment CreateRelative(Point offset) + { + return new WireSegment(end, new Point(end.X + offset.X, end.Y + offset.Y)); + } + + public bool ContainsPoint(Point point) + { + if (Vertical) + { + if (min.X == point.X) + { + return min.Y <= point.Y && max.Y >= point.Y; + } + else + return false; + } + else + { + if (min.Y == point.Y) + { + return min.X <= point.X && max.X >= point.X; + } + else + return false; + } + } + + public bool Contains(WireSegment other) + { + if (Vertical != other.Vertical) + return false; + if (Vertical) + { + return min.Y <= other.min.Y && max.Y >= other.max.Y; + } + else + { + return min.X <= other.min.X && max.X >= other.max.X; + } + } + + public WireSegment GetOverlap(WireSegment other) + { + if (Vertical) + { + if (other.Contains(this)) + return this; + else if (Contains(other)) + return other; + if (max.Y >= other.min.Y && min.Y <= other.min.Y && max.Y <= other.max.Y) + { + return new WireSegment(other.min, max); + } + else if (max.Y >= other.max.Y && min.Y >= other.min.Y && min.Y <= other.max.Y) + return new WireSegment(min, other.max); + else + throw new Exception("No Overlap"); + } + else + { + if (other.Contains(this)) + return this; + else if (Contains(other)) + return other; + if (max.X >= other.min.X && min.X <= other.min.X && max.X <= other.max.X) + { + return new WireSegment(other.min, max); + } + else if (max.X >= other.max.X && min.X >= other.min.X && min.X <= other.max.X) + return new WireSegment(min, other.max); + else + throw new Exception("No Overlap"); + } + } + + public bool Intersect(WireSegment other, out Point intersection) + { + if (Vertical) + { + if (!other.Vertical)//Other Horizontal + { + var potInt = new Point(min.X, other.min.Y); + if (potInt == default) + { + intersection = default; + return false; + } + if (ContainsPoint(potInt) && other.ContainsPoint(potInt)) + { + intersection = potInt; + return true; + } + else + { + intersection = default; + return false; + } + } + else //Both + { + if (min.X != other.min.X) + { + intersection = default; + return false; + } + else + { + var overlap = GetOverlap(other); + if (ManhattanMagnitude(overlap.min) < ManhattanMagnitude(overlap.max)) + intersection = overlap.min == default ? overlap.max : overlap.min; + else + intersection = overlap.max == default ? overlap.min : overlap.max; + if (intersection == default) + return false; + return true; + } + } + } + else + { + if (!other.Vertical) //Other Horizontal + { + if (min.Y != other.min.Y) + { + intersection = default; + return false; + } + var overlap = GetOverlap(other); + if (ManhattanMagnitude(overlap.min) < ManhattanMagnitude(overlap.max)) + intersection = overlap.min == default ? overlap.max : overlap.min; + else + intersection = overlap.max == default ? overlap.min : overlap.max; + if (intersection == default) + return false; + return true; + } + else + return other.Intersect(this, out intersection); + } + } + + public override string ToString() + { + return $"{start} > {end}"; + } + } + + public static int StepsToPoint(List wires, Point p) + { + var steps = 0; + for (int i = 0; i < wires.Count; i++) + { + if (wires[i].ContainsPoint(p)) + { + if (wires[i].Vertical) + steps += Math.Abs(wires[i].start.Y - p.Y); + else + steps += Math.Abs(wires[i].start.X - p.X); + break; + } + else + steps += wires[i].Length; + } + return steps; + } + + public static int SolveWires(string[] wires) + { + + var (wireSegmentsA, wireSegmentsB) = CreateWirePair(wires); + + int shortestWire = int.MaxValue; + for (int i = 0; i < wireSegmentsA.Count; i++) + { + for (int j = 0; j < wireSegmentsB.Count; j++) + { + if (wireSegmentsA[i].Intersect(wireSegmentsB[j], out var intersection)) + { + var len = StepsToPoint(wireSegmentsA, intersection) + StepsToPoint(wireSegmentsB, intersection); + if (len < shortestWire) + shortestWire = len; + } + } + } + return shortestWire; + } + + public static int SolveClosestDistane(string[] wires) + { + var (wireSegmentsA, wireSegmentsB) = CreateWirePair(wires); + + int lastIntersection = int.MaxValue; + for (int i = 0; i < wireSegmentsA.Count; i++) + { + for (int j = 0; j < wireSegmentsB.Count; j++) + { + if (wireSegmentsA[i].Intersect(wireSegmentsB[j], out var intersection)) + { + var dist = ManhattanMagnitude(intersection); + if (dist < lastIntersection) + lastIntersection = dist; + } + } + } + return lastIntersection; + } + + private static (List A, List B) CreateWirePair(string[] wires) + { + var wireA = wires[0].Split(','); + var wireSegmentsA = CreateWire(wireA); + + + var wireB = wires[1].Split(','); + var wireSegmentsB = CreateWire(wireB); + + return (wireSegmentsA, wireSegmentsB); + } + + private static List CreateWire(string[] wires) + { + var wireSegments = new List(); + + for (int i = 0; i < wires.Length; i++) + { + var curSegment = wires[i]; + var offset = GetOffset(curSegment); + if (i == 0) + wireSegments.Add(new WireSegment(new Point(0, 0), offset)); + else + wireSegments.Add(wireSegments.Last().CreateRelative(offset)); + } + return wireSegments; + } + + public static int ManhattanMagnitude(Point point) => Math.Abs(point.X) + Math.Abs(point.Y); + + public static Point GetOffset(string move) + { + int x = 0, y = 0; + if (move[0] == 'R') + x = int.Parse(move.Remove(0, 1)); + else if (move[0] == 'L') + x = -int.Parse(move.Remove(0, 1)); + else if (move[0] == 'U') + y = int.Parse(move.Remove(0, 1)); + else if (move[0] == 'D') + y = -int.Parse(move.Remove(0, 1)); + return new Point(x, y); + } + + public override void LoadInput() + { + _inputLines = ReadInputLines(); + } + + public override void CalculatePart1() + { + Part1 = SolveClosestDistane(_inputLines); + } + + public override void CalculatePart2() + { + Part2 = SolveWires(_inputLines); + } + } +} diff --git a/AdventOfCode/Problems/AOC2019/Day3/input.txt b/AdventOfCode/Problems/AOC2019/Day3/input.txt new file mode 100644 index 0000000..d6fe263 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day3/input.txt @@ -0,0 +1,2 @@ +R1000,U573,L25,U468,L833,D867,R515,D941,L513,D1,L380,U335,L661,D725,L506,U365,L103,D987,L425,U756,R129,D153,R326,U297,L456,D632,L142,U666,R864,D255,R85,D661,L566,D125,R445,U293,R295,D14,R181,D772,R376,U151,L146,D344,L947,D519,L455,D232,L873,U617,R143,D600,R654,D14,R813,U176,L443,U712,R230,U629,L554,U886,L931,D591,R716,U904,R605,D176,R801,U911,L746,D316,R30,U240,R975,D929,L879,U295,L56,U662,R429,U117,R282,D716,R57,D445,L7,D486,R147,D991,R750,D252,R134,U43,L410,D757,R252,U595,R986,U978,L883,D664,R267,D718,R28,U727,R926,U395,L81,D70,L67,D92,R209,D633,L253,D798,R820,U816,R754,U646,R846,D863,L868,U911,L678,D893,R686,D466,L153,D884,L589,U960,L924,U603,R93,D518,L291,D324,L67,D40,R722,U384,R195,D916,R64,D666,R896,D860,R388,D833,L662,D192,R567,U551,L558,U11,L674,U19,L669,U110,R681,D882,L997,U535,R683,U313,L904,U674,L476,D969,L464,D342,R574,D981,R405,D352,R431,U429,L329,D160,L573,U978,R930,U683,R592,D877,L88,D512,R676,U436,R708,U187,L664,U614,L734,D480,L242,U489,R732,U876,L416,D524,R181,U846,L396,D974,L620,D282,L124,D206,R119,U179,L171,D528,R469,U516,L708,D599,R913,U63,R922,D300,L856,U700,L396,D185,R933,D453,L234,D385,R426,D189,L25,U599,L715,U355,L574,D857,R662,D504,R746,U386,R389,U751,R85,U499,R255,D150,R998,U804,L832,D642,R102,U202,R972,U312,L265,D484,R314,D591,L250,U791,L120,D536,L808,D972,L808,D46,L626,D284,R60,D155,L849,D501,L206,U445,L765,U770,L67,U780,R876,D409,R603,U713,L459,D81,L294,D471,R656,U603,R55,D650,L211,D333,L44,D168,L187,D52,R60,D574,R54 +L1004,U110,R738,D383,R606,U840,L123,D756,L234,D585,R475,U429,L585,D615,L859,D669,L812,U672,L415,D114,L538,D899,R444,D379,L886,D276,R268,D90,R200,D247,L704,D802,L10,U313,R437,D854,R899,U21,L553,D352,L736,U604,R162,D504,R509,D471,R501,D472,L117,U796,L828,U906,R450,U697,R831,D302,R879,U730,R381,U788,L654,U927,R971,D355,L712,D959,L104,D169,L297,U898,R82,D673,R21,D608,L813,U754,L554,U239,L1,U834,R456,D671,L692,D855,L784,U664,R832,U446,L673,D898,R146,U507,L934,D569,R249,D755,L212,D475,R970,U122,R418,U820,L754,U313,L843,D608,R165,D881,L293,U628,R492,D37,L120,U659,L471,D275,R790,U372,L736,U318,L353,U439,L669,U18,R683,U768,R518,U300,L478,U601,R14,U233,L33,U765,L910,U591,R304,D528,R637,D376,L704,U27,L226,U384,R870,U318,L975,U876,R576,U500,R880,D108,L670,U171,R561,U873,L391,U717,L455,D909,L34,U211,R919,U376,L228,D632,L91,U408,R354,U454,L81,D547,L624,U464,R480,D630,L596,D57,L206,U736,R255,U185,L236,U705,L221,D511,L461,U718,R351,D59,L142,U236,R623,D124,R736,D758,L368,D605,L417,U990,R228,D207,L792,U150,L353,U612,R269,D459,L855,U808,L852,U168,R838,D794,R478,U281,L453,D134,L643,D862,L299,D590,L570,D782,L294,U935,R835,U849,R842,U997,R890,U20,L370,D157,R89,U203,L243,U71,R987,D812,R595,U664,L926,D359,L915,D382,R190,D443,R360,U253,R230,D879,L606,D755,R859,U232,R771,U465,R858,D823,R405,D499,L737,U846,R241,D976,R415,U541,L746,D569,L563,D410,L409,D39,R117,U638,R824,D215,R232,U578,R790,U535,R873,D477,R805,U94,L313,U570,L500,U783,L556,U663,L335,U152,L524,D583,L462,U710,R741,U641,L135 diff --git a/AdventOfCode/Problems/AOC2019/Day4/SecureContainer.cs b/AdventOfCode/Problems/AOC2019/Day4/SecureContainer.cs new file mode 100644 index 0000000..2afea0a --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day4/SecureContainer.cs @@ -0,0 +1,157 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; + +namespace AdventOfCode.Problems.AOC2019.Day4 +{ + [ProblemInfo(2019, 4, "Secure Container")] + public class SecureContainer : Problem + { + public static bool IsValidPassword(int[] password) + { + if (password.Length != 6) + return false; + return HasRepeating(password) && IsAssending(password); + } + + public static bool HasRepeating(int[] password) + { + for (int i = 0; i < password.Length - 1; i++) + { + if (password[i] == password[i + 1]) + return true; + } + return false; + } + + public static bool HasDoubles(int[] password) + { + + bool foundDouble = false; + for (int i = 0; i < 6; i++) + { + int c = 0; + for (int j = 0; j < 6; j++) + { + if (password[j] == password[i]) + c++; + else + { + if (c != 0) + { + if (c == 2) + foundDouble = true; + c = 0; + } + } + } + if (c == 2) + foundDouble = true; + } + return foundDouble; + + } + + public static bool IsAssending(int[] password) + { + for (int i = 1; i < 6; i++) + { + if (password[i] < password[i - 1]) + { + return false; + } + } + return true; + } + + + + public static int CountPasswordsPart1(int lower, int upper) + { + int passwordCount = 0; + int[] curPassword = lower.ToIntArray(); + CleanPassword(ref curPassword); + while (curPassword.ToInt() <= upper) + { + if (IsValidPassword(curPassword)) + { + passwordCount++; + } + curPassword[^1]++; + Propagate(ref curPassword, curPassword.Length - 1); + CleanPassword(ref curPassword); + } + return passwordCount; + } + public static int CountPasswordsPart2(int lower, int upper) + { + int passwordCount = 0; + int[] curPassword = lower.ToIntArray(); + CleanPassword(ref curPassword); + while (curPassword.ToInt() <= upper) + { + if (HasDoubles(curPassword)) + { + passwordCount++; + } + curPassword[^1]++; + Propagate(ref curPassword, curPassword.Length - 1); + CleanPassword(ref curPassword); + } + return passwordCount; + } + + public static void CleanPassword(ref int[] password) + { + for (int i = 1; i < 6; i++) + { + if (password[i] < password[i - 1]) + { + password[i] += password[i - 1] - password[i]; + if (password[i] == 10) + { + Propagate(ref password, i); + password[i] = password[i - 1]; + } + } + } + } + + public static void Propagate(ref int[] password, int digit) + { + for (int i = digit; i >= 0; i--) + { + if (i == 0 && password[i] == 10) + { + password[i] = 9; + break; + } + + if (password[i] == 10) + { + password[i] = 0; + password[i - 1]++; + } + } + } + + + public override void LoadInput() + { + } + + public override void CalculatePart1() + { + Part1 = CountPasswordsPart1(147981, 691423); + } + + public override void CalculatePart2() + { + Part2 = CountPasswordsPart2(147981, 691423); + } + } +} diff --git a/AdventOfCode/Problems/AOC2019/Day5/ChanceOfAsteroids.cs b/AdventOfCode/Problems/AOC2019/Day5/ChanceOfAsteroids.cs new file mode 100644 index 0000000..3605c4a --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day5/ChanceOfAsteroids.cs @@ -0,0 +1,36 @@ +using AdventOfCode.Day_5; +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AdventOfCode.Problems.AOC2019.Day5; +[ProblemInfo(2019, 5, "Sunny with a Chance of Asteroids")] +internal class ChanceOfAsteroids : Problem +{ + private IntCodeV2 _cpu = new IntCodeV2(); + private int[] _baseInput = Array.Empty(); + + public override void CalculatePart1() + { + var output = new int[1]; + _cpu.ExecuteCode(_baseInput, new int[] { 1 }, output); + Part1 = output[0]; + } + + public override void CalculatePart2() + { + var output = new int[1]; + _cpu.ExecuteCode(_baseInput, new int[] { 5 }, output); + Part2 = output[0]; + } + + public override void LoadInput() + { + _baseInput = InputParsing.ParseIntCsv(GetInputFile("input.csv")); + } +} diff --git a/AdventOfCode/Problems/AOC2019/Day5/IntCodeV2.cs b/AdventOfCode/Problems/AOC2019/Day5/IntCodeV2.cs new file mode 100644 index 0000000..25cdfdd --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day5/IntCodeV2.cs @@ -0,0 +1,240 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; + +namespace AdventOfCode.Day_5 +{ + public class IntCodeV2 + { + public struct Instruction + { + public int opcode; + public int paramCount; + public Func action; + + public Instruction(int opcode, int paramCount, Func action) + { + this.opcode = opcode; + this.paramCount = paramCount; + this.action = action; + } + + } + + public bool IsHalted { get; private set; } + public bool IsRunning { get; private set; } + public bool PersistentMode { get; private set; } + public bool SuspendOnWrite { get; private set; } + + private Dictionary _instructions; + private int _instructionPointer; + private int[]? _inputBuffer; + private int _inputCounter = 0; + private int[]? _outputBuffer; + private int _outputCounter = 0; + private int[] memory = Array.Empty(); + + public IntCodeV2(bool persistentMode = false, bool suspendOnOutput = false) + { + _instructions = new Dictionary(); + PersistentMode = persistentMode; + SuspendOnWrite = suspendOnOutput; + IsHalted = false; + //Add + _instructions.Add(1, new Instruction(1, 3, (mem, mode, p1, p2, p3) => + { + var v1 = mode[2] == 1 ? p1 : mem[p1]; + var v2 = mode[1] == 1 ? p2 : mem[p2]; + var v3 = mode[0] == 1 ? mem[p3] : p3; + mem[v3] = v1 + v2; + + return true; + })); + //Multiply + _instructions.Add(2, new Instruction(2, 3, (mem, mode, p1, p2, p3) => + { + var v1 = mode[2] == 1 ? p1 : mem[p1]; + var v2 = mode[1] == 1 ? p2 : mem[p2]; + var v3 = mode[0] == 1 ? mem[p3] : p3; + mem[v3] = v1 * v2; + return true; + })); + //Halt + _instructions.Add(99, new Instruction(99, 0, (mem, mode, p1, p2, p3) => + { + IsHalted = true; + IsRunning = false; + return false; + })); + //Read Input + _instructions.Add(3, new Instruction(3, 1, (mem, mode, p1, p2, p3) => + { + var v1 = mode[2] == 1 ? mem[p1] : p1; + mem[v1] = ReadInput(); + //Console.WriteLine($"Input Read: {mem[v1]}"); + return true; + })); + //Write Output + _instructions.Add(4, new Instruction(4, 1, (mem, mode, p1, p2, p3) => + { + var v1 = mode[2] == 1 ? p1 : mem[p1]; + WriteOutput(v1); + if (SuspendOnWrite) + IsRunning = false; + return true; + })); + //Jump if True + _instructions.Add(5, new Instruction(5, 2, (mem, mode, p1, p2, p3) => + { + var v1 = mode[2] == 1 ? p1 : mem[p1]; + var v2 = mode[1] == 1 ? p2 : mem[p2]; + if (v1 != 0) + { + _instructionPointer = v2; + return false; + } + return true; + })); + //Jump if False + _instructions.Add(6, new Instruction(6, 2, (mem, mode, p1, p2, p3) => + { + var v1 = mode[2] == 1 ? p1 : mem[p1]; + var v2 = mode[1] == 1 ? p2 : mem[p2]; + if (v1 == 0) + { + _instructionPointer = v2; + return false; + } + return true; + })); + //Less than + _instructions.Add(7, new Instruction(7, 3, (mem, mode, p1, p2, p3) => + { + var v1 = mode[2] == 1 ? p1 : mem[p1]; + var v2 = mode[1] == 1 ? p2 : mem[p2]; + var v3 = mode[0] == 1 ? mem[p3] : p3; + if (v1 < v2) + mem[v3] = 1; + else + mem[v3] = 0; + return true; + })); + //Equals + _instructions.Add(8, new Instruction(8, 3, (mem, mode, p1, p2, p3) => + { + var v1 = mode[2] == 1 ? p1 : mem[p1]; + var v2 = mode[1] == 1 ? p2 : mem[p2]; + var v3 = mode[0] == 1 ? mem[p3] : p3; + if (v1 == v2) + mem[v3] = 1; + else + mem[v3] = 0; + return true; + })); + } + + private int ReadInput() + { + _inputCounter = Math.Min(_inputCounter, (_inputBuffer?.Length ?? 1) - 1); + if (_inputBuffer != null && _inputCounter < _inputBuffer.Length) + return _inputBuffer[_inputCounter++]; + else + { + Console.Write("Input: "); + return int.Parse(Console.ReadLine()!); + } + } + + private void WriteOutput(int output) + { + _outputCounter = Math.Min(_outputCounter, (_outputBuffer?.Length ?? 1) - 1); + if (_outputBuffer != null && _outputCounter < _outputBuffer.Length) + _outputBuffer[_outputCounter++] = output; + else + Console.WriteLine(output); + } + + public void ExecuteCode(int[] code, int[]? input = null, int[]? output = null) + { + LoadCode(code); + SetIO(input, output); + IsHalted = false; + Run(); + } + + public static (int[] opModes, int opcode) ParseInstruction(int instruction) + { + var opModes = new int[3]; + var arr = instruction.ToIntArray(); + switch(arr.Length) + { + case 1: + return (opModes, arr[0]); + case 2: + return (opModes, (arr[^2] * 10) + arr[^1]); + } + var opcode = (arr[^2] * 10) + arr[^1]; + for (int i = 1; i <= 3; i++) + { + if (arr.Length < i + 2) + opModes[^i] = 0; + else + opModes[^i] = arr[^(i + 2)]; + } + + return (opModes, opcode); + } + + public void ResetIO() + { + _inputCounter = _outputCounter = 0; + } + + public void SetInputIndex(int index) + { + _inputCounter = index; + } + + public void SetIO(int[]? inputBuffer, int[]? outputBuffer) + { + ResetIO(); + _inputBuffer = inputBuffer ?? Array.Empty(); + _outputBuffer = outputBuffer ?? Array.Empty(); + } + + public void Run() + { + IsRunning = true; + while (IsRunning) + { + var (modes, opcode) = ParseInstruction(memory[_instructionPointer]); + var curInstruction = _instructions[opcode]; + int[] parameters = new int[3]; + for (int i = 0; i < 3; i++) + { + if (i >= curInstruction.paramCount) + parameters[i] = 0; + else + parameters[i] = memory[_instructionPointer + i + 1]; + } + + if (curInstruction.action(memory, modes, parameters[0], parameters[1], parameters[2])) + _instructionPointer += curInstruction.paramCount + 1; + + if (IsHalted) + IsRunning = false; + } + } + + public IntCodeV2 LoadCode(int[] code) + { + memory = new int[code.Length]; + code.CopyTo(memory, 0); + _instructionPointer = 0; + return this; + } + + + } +} diff --git a/AdventOfCode/Problems/AOC2019/Day5/input.csv b/AdventOfCode/Problems/AOC2019/Day5/input.csv new file mode 100644 index 0000000..2109937 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day5/input.csv @@ -0,0 +1 @@ +3,225,1,225,6,6,1100,1,238,225,104,0,1102,40,93,224,1001,224,-3720,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1101,56,23,225,1102,64,78,225,1102,14,11,225,1101,84,27,225,1101,7,82,224,1001,224,-89,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,1,35,47,224,1001,224,-140,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1101,75,90,225,101,9,122,224,101,-72,224,224,4,224,1002,223,8,223,101,6,224,224,1,224,223,223,1102,36,63,225,1002,192,29,224,1001,224,-1218,224,4,224,1002,223,8,223,1001,224,7,224,1,223,224,223,102,31,218,224,101,-2046,224,224,4,224,102,8,223,223,101,4,224,224,1,224,223,223,1001,43,38,224,101,-52,224,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,1102,33,42,225,2,95,40,224,101,-5850,224,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1102,37,66,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1007,226,677,224,1002,223,2,223,1005,224,329,1001,223,1,223,1007,226,226,224,1002,223,2,223,1006,224,344,101,1,223,223,1107,677,226,224,102,2,223,223,1006,224,359,1001,223,1,223,108,677,677,224,1002,223,2,223,1006,224,374,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,389,101,1,223,223,8,677,677,224,1002,223,2,223,1005,224,404,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,419,101,1,223,223,1008,677,677,224,1002,223,2,223,1005,224,434,101,1,223,223,1008,226,226,224,1002,223,2,223,1005,224,449,101,1,223,223,7,677,226,224,1002,223,2,223,1006,224,464,1001,223,1,223,7,226,226,224,1002,223,2,223,1005,224,479,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,494,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,509,1001,223,1,223,8,677,226,224,102,2,223,223,1005,224,524,1001,223,1,223,1107,226,226,224,102,2,223,223,1006,224,539,1001,223,1,223,1008,226,677,224,1002,223,2,223,1006,224,554,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,569,1001,223,1,223,1108,677,677,224,102,2,223,223,1005,224,584,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,599,1001,223,1,223,1108,226,677,224,102,2,223,223,1006,224,614,101,1,223,223,107,226,677,224,1002,223,2,223,1005,224,629,101,1,223,223,108,226,677,224,1002,223,2,223,1005,224,644,101,1,223,223,8,226,677,224,1002,223,2,223,1005,224,659,1001,223,1,223,107,226,226,224,1002,223,2,223,1006,224,674,101,1,223,223,4,223,99,226 diff --git a/AdventOfCode/Problems/AOC2019/Day6/OrbitMap.cs b/AdventOfCode/Problems/AOC2019/Day6/OrbitMap.cs new file mode 100644 index 0000000..62b6d56 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day6/OrbitMap.cs @@ -0,0 +1,119 @@ +namespace AdventOfCode.Problems.AOC2019.Day6 +{ + public class OrbitMap + { + public CelestialObject root; + public Dictionary objectMap; + + public OrbitMap(string[] orbits) + { + objectMap = new Dictionary(); + GenerateOrbits(orbits); + } + + public void GenerateOrbits(string[] orbits) + { + for (int i = 0; i < orbits.Length; i++) + { + var bodies = orbits[i].Split(')'); + if (bodies[0] == "COM") + root = CreateObject("COM"); + + var parent = GetOrCreateObject(bodies[0]); + var child = GetOrCreateObject(bodies[1]); + + parent.AddChild(child); + } + } + + public CelestialObject GetOrCreateObject(string name) + { + if (objectMap.ContainsKey(name)) + return objectMap[name]; + else + return CreateObject(name); + } + + public CelestialObject CreateObject(string name) + { + var o = new CelestialObject(name); + objectMap.Add(name, o); + return o; + } + + public int CalculateOrbits() + { + return root.GetOrbitCount(); + } + + public List FindPathTo(string name) + { + var path = new List(); + root.FindPathTo(name, path); + return path; + } + + public int GetDepthOf(string name) => root.GetDepth(name); + + public class CelestialObject + { + public string Name { get; set; } + public int ChildCount => children.Count; + + public List children; + + public CelestialObject(string name) + { + children = new List(); + Name = name; + } + + public void AddChild(CelestialObject child) + { + children.Add(child); + } + + public int GetOrbitCount(int depth = 0) + { + var count = 0; + for (int i = 0; i < children.Count; i++) + { + count += children[i].GetOrbitCount(depth + 1); + } + return depth + count; + } + + public bool FindPathTo(string name, List path) + { + if (name == Name) + return true; + for (int i = 0; i < ChildCount; i++) + { + if (children[i].FindPathTo(name, path)) + { + path.Add(children[i]); + return true; + } + } + return false; + } + + public int GetDepth(string name, int depth = 0) + { + if (name == Name) + return depth; + var d = 0; + for (int i = 0; i < ChildCount; i++) + { + d += children[i].GetDepth(name, depth + 1); + } + return d; + } + + public override string ToString() + { + return Name; + } + } + } +} \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2019/Day6/UniversalOrbits.cs b/AdventOfCode/Problems/AOC2019/Day6/UniversalOrbits.cs new file mode 100644 index 0000000..39887e3 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day6/UniversalOrbits.cs @@ -0,0 +1,60 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AdventOfCode.Problems.AOC2019.Day6; +[ProblemInfo(2019, 6, "Universal Orbit Map")] +internal class UniversalOrbits : Problem +{ + private OrbitMap? _map; + + public override void CalculatePart1() + { + if(_map == null) + return; + Part1 = _map.CalculateOrbits(); + } + + public override void CalculatePart2() + { + if (_map == null) + return; + + var pathToYOU = _map.FindPathTo("YOU"); + var pathToSAN = _map.FindPathTo("SAN"); + string pivot = ""; + int dist = 0; + + HashSet pathYOU = new(pathToYOU.Select(o => o.Name)); + + + for (int i = 0; i < pathToSAN.Count; i++) + { + if (pathYOU.Contains(pathToSAN[i].Name)) + { + pivot = pathToSAN[i].Name; + dist = i; + break; + } + } + for (int i = 0; i < pathToYOU.Count; i++) + { + if (pathToYOU[i].Name == pivot) + { + dist += i; + break; + } + } + Part2 = dist - 2; + + } + + public override void LoadInput() + { + _map = new OrbitMap(ReadInputLines()); + } +} diff --git a/AdventOfCode/Problems/AOC2019/Day6/input.txt b/AdventOfCode/Problems/AOC2019/Day6/input.txt new file mode 100644 index 0000000..a88af44 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day6/input.txt @@ -0,0 +1,1799 @@ +797)67Y +W32)48L +J31)N9K +QQ3)NVL +MP6)JBY +8T3)H27 +DRK)THF +BSN)7MD +Z2G)VRX +CVV)XDC +WVW)SV6 +D3H)QMG +9BM)NTH +4T7)FQS +F58)D1Q +CR7)B7L +4W8)T9Z +5SB)5JB +DRJ)YQD +76X)X1Z +WC9)LNG +52C)W7J +9ZS)KM7 +3RW)C8R +7RQ)TJW +HVN)ZNQ +SQK)CBT +GWC)N76 +KBQ)W44 +QD5)RVQ +SQF)N3K +RZ1)B2R +TB5)34W +J56)CQD +34Y)HLX +DTM)TD4 +2RF)SQV +GJT)TFD +CNK)88B +PJD)GD8 +4V8)FQN +HKH)6RS +7T4)2JT +DBM)DMQ +59X)137 +HK7)B62 +D34)K7Z +RR2)M74 +Q13)FC5 +1F1)8XJ +K1L)QZ9 +T73)4VT +Z7S)59X +1MQ)GB6 +BJT)4BZ +WW8)DT2 +JYL)6SH +R2K)WC9 +21H)4ZD +Z7Z)QWZ +F8N)LRZ +QZ9)9TD +3DP)VDT +GZK)ZGF +G3P)KLZ +7ZM)G3P +3VX)P4H +7T4)LNV +VVV)Q99 +BYX)87K +4C1)9XS +SB3)4SV +FYV)JQ4 +HQ4)ZMR +K7Z)LKT +TQM)BBH +9V5)PJD +7VB)H6L +66X)FDY +NT9)17R +K9Z)T26 +1BX)VRN +TJD)5ST +QWZ)5CT +MD6)GJT +TT1)W3Y +B2R)444 +MB8)NFN +FB1)4T3 +V3Y)DJ7 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6/test.txt new file mode 100644 index 0000000..f7b80d0 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day6/test.txt @@ -0,0 +1,13 @@ +COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L +K)YOU +I)SAN \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2019/Day7/AmplificationCircuit.cs b/AdventOfCode/Problems/AOC2019/Day7/AmplificationCircuit.cs new file mode 100644 index 0000000..0ed64e3 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day7/AmplificationCircuit.cs @@ -0,0 +1,161 @@ +using AdventOfCode.Day_5; +using AdventOfCode.Runner.Attributes; + +namespace AdventOfCode.Problems.AOC2019.Day7 +{ + [ProblemInfo(2019, 7, "Amplification Circuit")] + public class AmplificationCircuit : Problem + { + private int[] _code = Array.Empty(); + + public static int RunPhase(IntCodeV2 cpu, int[] code, int[] phaseSettings) + { + if (HasDuplicateValues(phaseSettings)) + return int.MinValue; + int[] outputBuffer = { 0 }; + int[] inputBuffer; + //Amp A + inputBuffer = new int[] { phaseSettings[0], outputBuffer[0] }; + cpu.ExecuteCode(code, inputBuffer, outputBuffer); + //Amp B + inputBuffer = new int[] { phaseSettings[1], outputBuffer[0] }; + cpu.ExecuteCode(code, inputBuffer, outputBuffer); + //Amp C + inputBuffer = new int[] { phaseSettings[2], outputBuffer[0] }; + cpu.ExecuteCode(code, inputBuffer, outputBuffer); + //Amp D + inputBuffer = new int[] { phaseSettings[3], outputBuffer[0] }; + cpu.ExecuteCode(code, inputBuffer, outputBuffer); + //Amp E + inputBuffer = new int[] { phaseSettings[4], outputBuffer[0] }; + cpu.ExecuteCode(code, inputBuffer, outputBuffer); + return outputBuffer[0]; + } + + public static int RunFeedback(int[] code, int[] phaseSettings) + { + if (HasDuplicateValues(phaseSettings)) + return int.MinValue; + var ampA = new IntCodeV2(true, true).LoadCode(code); + var ampB = new IntCodeV2(true, true).LoadCode(code); + var ampC = new IntCodeV2(true, true).LoadCode(code); + var ampD = new IntCodeV2(true, true).LoadCode(code); + var ampE = new IntCodeV2(true, true).LoadCode(code); + var outputA = new int[] { 273 }; + var outputB = new int[] { 0 }; + var outputC = new int[] { 0 }; + var outputD = new int[] { 0 }; + var outputE = new int[] { 0 }; + var inputA = new int[] { phaseSettings[0], outputE[0] }; + var inputB = new int[] { phaseSettings[1], outputA[0] }; + var inputC = new int[] { phaseSettings[2], outputB[0] }; + var inputD = new int[] { phaseSettings[3], outputC[0] }; + var inputE = new int[] { phaseSettings[4], outputD[0] }; + ampA.SetIO(inputA, outputA); + ampB.SetIO(inputB, outputB); + ampC.SetIO(inputC, outputC); + ampD.SetIO(inputD, outputD); + ampE.SetIO(inputE, outputE); + int iter = 0; + while (!ampE.IsHalted) + { + //Console.WriteLine($"Iteration {iter}"); + inputA[1] = outputE[0]; + + ampA.Run(); + inputB[1] = outputA[0]; + ampB.Run(); + inputC[1] = outputB[0]; + ampC.Run(); + inputD[1] = outputC[0]; + ampD.Run(); + inputE[1] = outputD[0]; + ampE.Run(); + + //Console.WriteLine($"Output {outputE[0]}"); + iter++; + } + + return outputE[0]; + } + + public static bool HasDuplicateValues(int[] arr) + { + for (int i = 0; i < arr.Length; i++) + { + for (int j = 0; j < arr.Length; j++) + { + if (i == j) + continue; + if (arr[i] == arr[j]) + return true; + } + } + return false; + } + + public override void LoadInput() + { + _code = InputParsing.ParseIntCsv(GetInputFile("input.csv")); + } + + public override void CalculatePart1() + { + int output = int.MinValue; + int min = 0; + int max = 5; + var cpu = new IntCodeV2(); + + for (int i = min; i < max; i++) + { + for (int j = min; j < max; j++) + { + for (int k = min; k < max; k++) + { + for (int l = min; l < max; l++) + { + for (int m = min; m < max; m++) + { + var result = RunPhase(cpu, _code, new int[] { i, j, k, l, m }); + if (output < result) + { + output = result; + } + } + } + } + } + } + Part1 = output; + } + + public override void CalculatePart2() + { + int output = int.MinValue; + int min = 5; + int max = 10; + + for (int i = min; i < max; i++) + { + for (int j = min; j < max; j++) + { + for (int k = min; k < max; k++) + { + for (int l = min; l < max; l++) + { + for (int m = min; m < max; m++) + { + var result = RunFeedback(_code, new int[] { i, j, k, l, m }); + if (output < result) + { + output = result; + } + } + } + } + } + } + Part2 = output; + } + } +} \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2019/Day7/input.csv b/AdventOfCode/Problems/AOC2019/Day7/input.csv new file mode 100644 index 0000000..26ad04b --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day7/input.csv @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,34,59,68,85,102,183,264,345,426,99999,3,9,101,3,9,9,102,3,9,9,4,9,99,3,9,1002,9,4,9,1001,9,2,9,1002,9,2,9,101,5,9,9,102,5,9,9,4,9,99,3,9,1001,9,4,9,4,9,99,3,9,101,3,9,9,1002,9,2,9,1001,9,5,9,4,9,99,3,9,1002,9,3,9,1001,9,5,9,102,3,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99 diff --git a/AdventOfCode/Problems/AOC2019/Day8/SpaceImageFormat.cs b/AdventOfCode/Problems/AOC2019/Day8/SpaceImageFormat.cs new file mode 100644 index 0000000..94eeb3c --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day8/SpaceImageFormat.cs @@ -0,0 +1,91 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace AdventOfCode.Problems.AOC2019.Day8 +{ + [ProblemInfo(2019, 8, "Space Image Format")] + public class SpaceImageFormat : Problem + { + private int[] _imageData = Array.Empty(); + + public static void Execute() + { + var imageData = File.ReadAllText("Day8/input.txt").Replace("\n", "").Select(c => int.Parse(c.ToString())).ToArray(); + + Console.WriteLine(Checksum(imageData, 25, 6)); + + } + + public static void RenderImage(int[] data, int h, int w) + { + var imgSize = h * w; + var layerCount = data.Length / imgSize; + + for (int l = 0; l < layerCount; l++) + { + + } + } + + + public static int Checksum(int[] data, int h, int w) + { + var imgSize = h * w; + var layerCount = data.Length / imgSize; + + int[] zeroCount = new int[layerCount]; + int[] oneCount = new int[layerCount]; + int[] twoCount = new int[layerCount]; + + int smallestLayer = -1; + int smallestLayerCount = int.MaxValue; + + for (int l = 0; l < layerCount; l++) + { + for (int i = imgSize * l; i < imgSize * (l + 1); i++) + { + switch (data[i]) + { + case 0: + zeroCount[l]++; + break; + case 1: + oneCount[l]++; + break; + case 2: + twoCount[l]++; + break; + } + } + if (zeroCount[l] <= smallestLayerCount) + { + smallestLayer = l; + smallestLayerCount = zeroCount[l]; + } + } + + return oneCount[smallestLayer] * twoCount[smallestLayer]; + + } + + public override void LoadInput() + { + _imageData = ReadInputText().Replace("\n", "").Select(c => int.Parse(c.ToString())).ToArray(); + } + + public override void CalculatePart1() + { + Part1 = Checksum(_imageData, 25, 6); + } + + public override void CalculatePart2() + { + Part2 = null; + } + } +} diff --git a/AdventOfCode/Problems/AOC2019/Day8/input.txt b/AdventOfCode/Problems/AOC2019/Day8/input.txt new file mode 100644 index 0000000..7fa510e --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/Day8/input.txt @@ -0,0 +1 @@ +221002222122020222021022222022222222222222222222221222222022220222112022220222022020222202222222220222222222221222212222222222222222222222222222022222220202222122121222022122222122222222222222222222222222222022221222102022221222122220222202222222220222222222222222202222222222222222222222222222222222221112222222220222021222222222222222222222222222220222222122221222122122221222222221222202222222220222222222220222202222222222222222222222222222222222222212222222220222221122222222222222222222222202221222222022221222002222220222022020222222222222222222222222220222202022222222222222222222222222222222222002222122221222020122222022222222222212222222222222022222220222002022220222222122222212222222222222222222222222212022222222222222222222222222122222221222222222220222122222222022222222222212222202220222122122222222012122220222022020222212222122221222222222221222222122222222222222222222222222222222222122222022120222022222222122222222222212222202222222222122220222102222221122122120222202222122221222222222222222202022202222222222222222222222022222222212222022220222120122222122222222222212222212220222222222221222222022220122222120222202222022221222222222220222212122222222222222222222222222022222220112222022120222121022222222222221222222222222220222122122220222222122220102222120222202222122222222222222222222212222222222222222222222122222022222222122222222121222222222222222222221222222222222220222222122221222112222220212022022222212222122021222222222221222222122212222222222222222122222122222222122222022221222222122222222222221222212222212221220022122220222202022222102222222222212222022220222222222220222212022212222222222222222022222022222222022222022222222120222222022222222222202222202222220122022222222022122221222202020222212222122221222222222221222222222222222222222222222022222122222220212222122222222122222222222222222222202222222221220222222211222022022222202222122222212222122021222222222220222222122202222222222222222122222222222222202222122122222120022222222222222222212222222220222222022202222222122222202012122222212222022022222222222222222222222202222222222222222022222022222220002222222122222120122222122222222222202222212222222122022202222102122220000222122222202222122120222222222222222222122212222222222222222022222122222221102222222121222222022220122222221222212212202221220222122201222002222220210012120222222222022222222222222222022222122222222222222222222222222122222222102222022122222121222220122222221222212212222221220022022212222112022220220112120222212222122021222222222222022222022202222222222222222222222122222222212222122022222122122222122222221222202212222221221122022201222022222221021102020222222222022020222222222222022222022212222222222222222222222022222220222222122222222220022221122222220222202222222220221022222222222122122220100122221222212222222021222222222221022212122222222222222222222122222122222221212222122120222222222221122222222222222212212221222222022220222022222222120112021222222222022022222222222221222222022222222222222222222122222022222220012222222122222120022220222222222222202202212222222122222202222122022222022022121222222222222122222222222222222212222202222222222222222222222222222222122222222021222222022220122222222222222212202220220022122201222012022221202022121222222222022222222222222222222222222222222222222222222122222222222221012222122020222121222222222202220222202222212222201122122221222112122220202002122222212222222221222022222220022212022212222222222222222122222222222220202222222120222220122221022202220222212202212221210022122211222002122222112212220222212222222221222122222221022222222202222222222222222222222222222221002222122020222121222220022212221222202212202221200022120220222102022220201122122222222222022022222022222021022202022222222222222122222122222122222220022222122020222121122220122202222222212212222222200122022212222222122222121102021222202222022020222022222021222202122222222222222122222022222222222220122222222221222021022222022222222222202212202220221122022200222202222221202112022222222222022020222022222020122212222222222222222122222022222222220221022222222020222121022221022212220222222212222220210122120220222212222222112002022222202222122022222222222022122220022222222220222122222122222222220221222222222122222221222221122202222222212202212221200022220210222012222220122212221222212222122022222022222221022201222212222222222122222222222022221221002222122020222220222222222202222222222202222221211022022202222122022220122022121222212222222221222122222222222200122202022221222022222022222222222220202222222020222022022221122222221222222202222220202022222221222202122221021202122222222222222122222022222022222221022212022220222022222022222122220221002222122121222122222222222222222222200222202221200022221212222012022222202012122222222222020221222022222221222200022222122220222222222222222222221221022222122122222120122220122202222222211202212221200222120201222012022220020012020222212222021220222222222121022201022212122220222222222022222122221221112222022122222220022222022212220222212212202220002222220220222102222222201212011222222222122022222122222021022211222202222220222022222222222122222220122222022221222120222221022202221222221222202222000022220200222112122221122202020222212222021120222022222020122210122212122222222120222122222222222221012222122222122120122220022212222222212212202222101122211202222122222220102212102222202222122021222222222120122202022212122221222121222022222122222221212222222222022021122222222212221222200202202222021022202210222102022220211002222222212222021121222222222122122210122222122220222220222121222222202220212222122222022022122220022222220222202222222220210122202212222222121212202112210222202222222022222022222221122201022022122220222022222121222022201222002222122222122120022222222222220222221202212222001122111202222002120220212022000222202222121020222022222021022220022122022220222020222222222022200220012222122022222121222222222222222222201222222221000222222212222122020221201022121222202222120121222222222221222202222002022220222221222020222022211022112222022220022221122221022222221222220212222221022022000221222012020210200112102222220222221220222222222221222222222202222220222122222200222122220220222222022020112022122221122222221222222212222222211022110220222212220202221202020212201122022021222022222220022222122122022220222220222222222122211221222222222121012121022220222212221222211202222221120022102202222222021221210202212202212222120022222022222020222220222122122220222120222211222222221222112222222210002021222221022202221222201212202221220202222202222102221200202102111212202122221220222022222020222200222112122212222122222100222122220220212222222122022120022221022222221222220222212220112122100201222022222200100002222222212222020122222222222222022210222122222211222221222222222222202022012222222202212220222120022222221222022202222220112002102202222222022221002122102212222022020020222022222122122212222022022212222022222120222022212112022222122111122122222121222202221222020212202220012222111221222112120210121122220212202222220022022122222220222222022222122212222221222201222122201012122222122210202122022020222202220222102222222220010202200221022012120210002122010212210022122222022122222222022201222002222221222222222011222122212121012222122202112221022220120202220222122202210222220212121210222012121210001102012222200122120121122122222022022201122102122200222220222111222022200022212222022002112221122222021222222222220202212220120012200212122122220220011022102202210022021022022022222220122210222122122212222120222120222222211111222222222102012221222221221212222222011202222220220122200201122112122210212202100202202222222122122022222222222211022012122222222222222122222022211002202222222212202021122222220212022222010222210220011022200222222222221220111102011202220022221221122022222020122201222002222221222122222102222222211000212222122002022121022120020212220222121212202221202012102221122202022221002212010202200122220122222222221121222220222222122201222222222022222222220122112222122201002122022220220202122222121212220220111012211222022002021220020022112202202022222121122222220221222212222112122220222122222012222122212101122222022021022020022121220202221222112202202220101022100220222202021222122212001202222022222222022222221222222202122202222221222222222110222122212021102222222221202221122122021222022222202202200222112222000221022112021210011022222222202022020122122222221220022200122222120202222121222222222222202102222222222110122021022022021202120222112212210222110202011202022012022221100102221202220122022022022122220022222200222202220210222221222121222022200212122222222001122021022021121222021222102212211221000022021220122022222220021002201222201222221221222122221020222200022202020202222022222212222022201012102222222012212220122221020212222222022212211220211202210212122222020011200102001222211122220122122222222220022222122212221202222221222000222220222202122222222020022020022021021202022222122222201222001012010202022102122220022122201212200122120020022122221222022212022222020202222121222121222122200200212222022200012220022120122202120222102222202221212122110212222022020022211102102222220222022020222122222121022220022102220200222120222221222020201010122222122002212021222021122222020222210202201222122102110221122012221111102022112202220222122221122222220220222212222112220211202022222210222020200002212222122020112022022221122202221222021212120222222021011210122212120220121022110212220222120022022122220220122221122012122202202221222102222221221212222222222010012220122221122222120222221202010220112020112211122222021020010212011222222222120122122122221222022211222212122210202220222110222120202120012222122222202222122022021212022222202212222222212102011220122002221000202122100212220122021121220222222022122202222022120211212020222211222002211000012222022001122022122120021222222222011222010222110210200220122222021011000222020212201222022222120222221222022201122112021202222222222020222101200022212222222001102222122222021222020222120202210222110020020210222212222210011012022212221122222220120022222121022211022102121211222021222111222112220002012222222102022020022121120202122222021222022222000000202200022022022122221122112222220022022220221022222022122220022022221222222020222212222202220000102222222120122120222220221222021222220202112220101111222201222212220102121012111202210022120120120222221020022221022012122220212120222120222202222022102222022102112121222121021102222222202222111221022202111201022012120012220102001222202120222122220122221020222211022002020211212121222110222020211021002222222100222221202110222022222222111222020220220211022211222022021200022012021212222222221021120022222020222211022202120220202020222201222201212222022222022000002220212110220022020222121212121222002212211221022002222112110122112212200120022220220122222222222221222222022211222021222210222220221221122222022112012220202212121222212222210212120222220221020212122222222122212212111202210221221222121222220220122222022022121200222122222201222100211122202222122111202122202121221002012222010202211220020010102212222112022201002112121202202120220220121022221020012212222112220200202022222220222110222110012222022010122221122200012212200222110212010222111221211110122212221202221012022222202221222021120222222121102212122221021221220120222211222111222220202222222212202121012021010202010222020222210221221200010111122202022020220222011202212022020120221222222021002202122020221201210022222112222222211001212222222120022222122102112012022222022212002220221010212121222112121020121010101202221220122222122222222222212220022012220202212220222220222211222112202222222100102221212200001102210222122222000221001001222110222122122211001200120222220122020120222122021120212211222102120200210222222011222021222101202222022021212220122000210112112222021202212222102200220101222102020210200211221222220121120021222022222220002201122121121220211221222202222112200122022222022201222220202122112012002222212202010220210201202212122022221121001220020222202222120120221222121021102202122112222210202120222101222001210010122222022210122120112111101212110222110212221222120000201021022122222102221212001222211122220020220222121122202210222001021221211020222222220121211210022222022220222221122100020112011222012212002221111102011102202002220212221002000202201121020221122222020221122200222212120201200222222222221112200201102222022011102021022021101002111212222202221221100122022101012002122212011100121222202022021022221222020021112202022000120211220020220110220000212001222222222000012222222112210222022212010202210222200012222112212102221221001200101222221222221121220222220120002220222002221210220121220102220122201020212222120002222120212012110102120202020212100220100001010111102202222111220202200212202220120020221022221020112212022120121211200121222001222020201101012222122002022121012201021112122202221202202221101120120212021002120122212100220222212222121120022022120222102201222020020210202120220000220101221211002222220121012121202211222222112222110202001221010100001200122022120201002210002212200222120021021122221121122211022202220202201220221012220210200020102222022010212220112122021222010202101202222222121121202202222202222220211020012212200222221021020122120021202210022020102200212022222211222122211201112222120221202220222212002122000202012222212221021001000222202012222011001021211222111021222122022212100221112201122220202200221222222210220212222202202022222212012020122011111012020212102222010221211121111102211022121212202000011212202222220222021112120020222220222020021200220220221112221020222010012022122000112000112211210122001202111222121220012000020021110212220210100211012222011021122122020202100221012202022022202220201120220000222120200221202122020221022120222202121112100212120212102221122021001120022102021201022110000212200022122221020222000120022202122111212211201221220101220222211021212022211200222011112012221022120202102212122022012201010011000222021211022110222202221021220020020022020220012210122120022201222120222211222122220121112122120000222022102022112222222212012212122122021200222200010202221202000222000202012022022020222002010021022221022022010221202220220020222222200220122122200022022221012122220002212202201202200021001221210111000022222201110221022220020122120022021102201121102220122211201210212122220011120100221212122122220011112120112112110112012202120202210222002112200012011222022011211021201220221121221122022202122101002222222122201201201122222102212221211201222022020102002010212221020022210202110202102020002110222212012222122100122010012022211222021122121202001110222210022211110212202220221212102112202010112222120102112121112121102012221222212022020021000012022111001012221002021022201012100220021120120022220222002221022210120212211002220100200010202010002222212020002112002111212012001222012122222111010001000102000122021020111122010100000221121020221122201000112210022112012222211122220000102200200122211000120212021002220110220200210010001220022100210112221200210200100122202211202002112202002110211210012202100021210110012101012021101101212002112 \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2019/InputParsing.cs b/AdventOfCode/Problems/AOC2019/InputParsing.cs new file mode 100644 index 0000000..84d8bd2 --- /dev/null +++ b/AdventOfCode/Problems/AOC2019/InputParsing.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace AdventOfCode +{ + public static class InputParsing + { + public static int[] ParseIntArray(string file) + { + return File.ReadAllLines(file).Select(s => int.Parse(s)).ToArray(); + } + + public static int[] ParseIntCsv(string file) + { + return File.ReadAllText(file).Split(',').Select(s => int.Parse(s)).ToArray(); + } + + public static int ToInt(this int[] intArr) + { + int value = 0; + for (int i = 0; i < intArr.Length; i++) + { + value += (int)Math.Pow(10, intArr.Length - i - 1) * intArr[i]; + } + return value; + } + + public static int[] ToIntArray(this int number) + { + int[] intArr = number.ToString().Select(d => int.Parse(d.ToString())).ToArray(); + return intArr; + } + } +} diff --git a/AdventOfCode/Problems/AOC2021/Day11/DumboOctopus.cs b/AdventOfCode/Problems/AOC2021/Day11/DumboOctopus.cs new file mode 100644 index 0000000..60d5a44 --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day11/DumboOctopus.cs @@ -0,0 +1,200 @@ +using AdventOfCode.Runner.Attributes; + +using System.Collections; +using System.Numerics; +using System.Text; + + +namespace AdventOfCode.Problems.AOC2021.Day11; + +[ProblemInfo(2021, 11, "Dumbo Octopus")] +public class DumboOctopus : Problem, IEnumerable +{ + public byte[] dataPart1 = Array.Empty(); + public byte[] dataPart2 = Array.Empty(); + + public override void LoadInput() + { + var data = ReadInputLines(); + dataPart1 = new byte[10 * 10]; + if (data.Length != 10) + throw new ArgumentException("Data must contain 10 elements", nameof(data)); + for (int y = 0; y < data.Length; y++) + { + var line = data[y]; + if (line.Length != 10) + throw new ArgumentException($"Lines must contain 10 elements. Line: {y + 1}", nameof(data)); + + for (int x = 0; x < line.Length; x++) + { + dataPart1[x + y * 10] = byte.Parse(line.Substring(x, 1)); + } + } + dataPart2 = new byte[10 * 10]; + Array.Copy(dataPart1, dataPart2, dataPart1.Length); + } + + public override void CalculatePart1() + { + Part1 = Run(ref dataPart1, 100, out _); + } + + public override void CalculatePart2() + { + Run(ref dataPart2, 210, out var fullFlash); + Part2 = fullFlash; + } + + public IEnumerator GetEnumerator() + { + return ((IEnumerable)dataPart1).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return dataPart1.GetEnumerator(); + } + + public int Run(ref byte[] data, int count, out int fullFlash) + { + var flashes = 0; + fullFlash = 0; + for (int i = 0; i < count; i++) + { + var start = flashes; + Run(ref data, ref flashes); + if (flashes - start == 100) + { + fullFlash = i + 1; + break; + } + } + return flashes; + } + + public void Run(ref byte[] data, ref int flashes) + { + Increment(ref data); + Flash(ref data, ref flashes); + + } + + private static void Increment(ref byte[] data) + { + for (int i = 0; i < data.Length; i++) + data[i]++; + } + + private void Flash(ref byte[] data, ref int flashes) + { + int diff; + do + { + var startFlash = flashes; + for (int i = 0; i < data.Length; i++) + { + if (data[i] > 9) + PerformFlash(ref data, i, ref flashes); + } + diff = flashes - startFlash; + } while (diff != 0); + } + + private void PerformFlash(ref byte[] data, int index, ref int flashes) + { + flashes++; + var y = index / 10; + var x = index % 10; + + data[index] = 0; + + if (x > 0) + { + //Left + index = x - 1 + y * 10; + if (index >= 0 && data[index] != 0) + data[index]++; + } + if (x < 9) + { + //Right + index = x + 1 + y * 10; + if (index < data.Length && data[index] != 0) + data[index]++; + } + if (y > 0) + { + //Up + index = x + 0 + (y - 1) * 10; + if (index >= 0 && data[index] != 0) + data[index]++; + if (x > 0) + { + //Up Left + index = x - 1 + (y - 1) * 10; + if (index >= 0 && data[index] != 0) + data[index]++; + } + if (x < 9) + { + //Up Right + index = x + 1 + (y - 1) * 10; + if (index < data.Length && data[index] != 0) + data[index]++; + } + } + + if (y < 9) + { + //Bottom + index = x + 0 + (y + 1) * 10; + if (index < data.Length && data[index] != 0) + data[index]++; + if (x > 0) + { + //Bottom Left + index = x - 1 + (y + 1) * 10; + if (index < data.Length && data[index] != 0) + data[index]++; + } + if (x < 9) + { + //Bottom Right + index = x + 1 + (y + 1) * 10; + if (index < data.Length && data[index] != 0) + data[index]++; + } + } + } + + //public override string ToString() + //{ + // var output = new StringBuilder(); + // for (int y = 0; y < 10; y++) + // { + // for (int x = 0; x < 10; x++) + // { + // output.Append(DataPart1[x + y * 10]); + // } + // output.AppendLine(); + // } + // return output.ToString(); + //} + + //public void Render() + //{ + // for (int y = 0; y < 10; y++) + // { + // for (int x = 0; x < 10; x++) + // { + // var index = x + y * 10; + // if (DataPart1[index] == 0) + // Console.ForegroundColor = ConsoleColor.Magenta; + // else + // Console.ForegroundColor = ConsoleColor.White; + // Console.Write(DataPart1[index]); + // } + // Console.WriteLine(); + // } + //} +} \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2021/Day11/input.txt b/AdventOfCode/Problems/AOC2021/Day11/input.txt new file mode 100644 index 0000000..586105d --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day11/input.txt @@ -0,0 +1,10 @@ +6318185732 +1122687135 +5173237676 +8754362612 +5718474666 +8443654137 +1247634346 +1446514585 +6717288267 +1727871228 \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2021/Day11/input2.txt b/AdventOfCode/Problems/AOC2021/Day11/input2.txt new file mode 100644 index 0000000..c046fc3 --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day11/input2.txt @@ -0,0 +1,10 @@ +1111111111 +1111111111 +1111111111 +1111111111 +1111111111 +1111111119 +1111111111 +1111111111 +1111111111 +1111111119 diff --git a/AdventOfCode/Problems/AOC2021/Day3/BinaryDiagnostic.cs b/AdventOfCode/Problems/AOC2021/Day3/BinaryDiagnostic.cs new file mode 100644 index 0000000..4106576 --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day3/BinaryDiagnostic.cs @@ -0,0 +1,78 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace AdventOfCode.Problems.AOC2021.Day3; + +[ProblemInfo(2021, 3, "Binary Diagnostic")] +public class BinaryDiagnostic : Problem +{ + public int[][] Data { get; set; } = Array.Empty(); + + private static int ToDecimal(int[] value) + { + return (int)value.Select((b, idx) => b * Math.Pow(2, value.Length - idx - 1)).Sum(); + } + + public override void LoadInput() + { + var data = ReadInputLines(); + Data = data.Select(line => line.Select(b => int.Parse(b.ToString())).ToArray()).ToArray(); + } + + public override void CalculatePart1() + { + var width = Data[0].Length; + var transposed = Enumerable.Range(0, width).Select(idx => Data.Select(row => row[idx])); + + var transposed2 = new int[width][]; + for (int i = 0; i < width; i++) + { + transposed2[i] = Data.Select(row => row[i]).ToArray(); + } + + var mid = Data.Length / 2; + var gamma = transposed.Select(col => col.Count(c => c == 1) > mid ? 1 : 0).ToArray(); + var epsilon = gamma.Select(b => b == 0 ? 1 : 0).ToArray(); + + var gammaDec = ToDecimal(gamma); + var epsilongDec = ToDecimal(epsilon); + + Part1 = gammaDec * epsilongDec; + } + + public override void CalculatePart2() + { + var oxygen = Data; + var index = 0; + while (oxygen.Length > 1) + { + var t = oxygen.Select(row => row[index]); + var m = oxygen.Length / 2f; + var g = t.Count(c => c == 1) >= m ? 1 : 0; + oxygen = oxygen.Where(row => row[index] == g).ToArray(); + index++; + } + + var carbon = Data; + index = 0; + while (carbon.Length > 1) + { + var t = carbon.Select(row => row[index]); + var m = carbon.Length / 2f; + var e = t.Count(c => c == 1) < m ? 1 : 0; + carbon = carbon.Where(row => row[index] == e).ToArray(); + index++; + } + + var oxygenLevel = ToDecimal(oxygen.First()); + var carbonLevel = ToDecimal(carbon.First()); + + Part2 = oxygenLevel * carbonLevel; + } +} diff --git a/AdventOfCode/Problems/AOC2021/Day3/Input.txt b/AdventOfCode/Problems/AOC2021/Day3/Input.txt new file mode 100644 index 0000000..98a204b --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day3/Input.txt @@ -0,0 +1,1000 @@ +010111111011 +010010101110 +011001001100 +001000001010 +111100101000 +111010101100 +000111101111 +010011010011 +100010111011 +101011000111 +100111101010 +101101101101 +110010110110 +100110011100 +001110011000 +011000101010 +001100111101 +100011101111 +100111011001 +011100101101 +111101000111 +111000101011 +001001000101 +010110011000 +110100100001 +010010010011 +100100100100 +011011001000 +111101011101 +101011110011 +110011101101 +001001000100 +100111101110 +101101101010 +111110101000 +111011011001 +111110101101 +110101010100 +011100110000 +000010111110 +111011111111 +010111111110 +101001110101 +001111010100 +001111110001 +000010000000 +010001101000 +100001001111 +101111000010 +000011110001 +110111101110 +000000100111 +010100000111 +011111100001 +100011110001 +101000001111 +101010111001 +001101100100 +001100111001 +001000011010 +001001111100 +011001011001 +101011001010 +100101101101 +000101011110 +101100110111 +011000010110 +110101000110 +011010100101 +110011100110 +111101000010 +111110101011 +110101100101 +101110101010 +100110001011 +000110101100 +010100001001 +010011001110 +101010010101 +101010000101 +100100001011 +010010100101 +011000111111 +001010000001 +000001100111 +010101101111 +101110000110 +100101001001 +010000101001 +001101011001 +011101111110 +011110011100 +110100110111 +000101011010 +100000010011 +011100111000 +001110101000 +001001001000 +110101011101 +000011001111 +101000011010 +100011110010 +100100010111 +110001010100 +111010100110 +101001010100 +110100111101 +111000010111 +010001111000 +000101010110 +010100000100 +001000110010 +100111001100 +111001000100 +011100001011 +100110001010 +000010100111 +001010111110 +101100110001 +010100100001 +011001110010 +010011100101 +011111010110 +111000010010 +011110110101 +110101101100 +101010110110 +000111000001 +001111111101 +110011101010 +011000110010 +101110101011 +110100001100 +001000011000 +110110110010 +101111000111 +000111111001 +111100110100 +010010011010 +010010111010 +000001101000 +000101010101 +110111110101 +111001110110 +110011001100 +011110011001 +000010110101 +011011000111 +010101010110 +001011010100 +100001111001 +011000111010 +011100010000 +110001011010 +110010110111 +001101010101 +000110011000 +000010011110 +101100111001 +100011001110 +100010000000 +001111010001 +011100010011 +010001000010 +110010010111 +011011111101 +010011010100 +100001101001 +101100110110 +000000101011 +111000110111 +111101010110 +111110000000 +101010000111 +011010110110 +100110100111 +001011001011 +110011111000 +010111100110 +001100101010 +110001001000 +000100000111 +000001011100 +000100000000 +100010100000 +110001011011 +110110111101 +100110010001 +001101001110 +011111011000 +110010000100 +100011010110 +011001100000 +010101001110 +111100000001 +110100010111 +000001000011 +101100010011 +000100011111 +111000001001 +101100011100 +010110010001 +000110000010 +110000101111 +011010011100 +111000011110 +100110110001 +100000011100 +101010001011 +010111001100 +100111110111 +111100001011 +010100100100 +100111100101 +111100000111 +101001001101 +111001111000 +001110010100 +111010011001 +010100111111 +110111010011 +100100000001 +001101111111 +111101001100 +000111101010 +000100101001 +111011011101 +010001110111 +111010100001 +001101010100 +101010011100 +000011100100 +100101101010 +011111100000 +011011001110 +000101000111 +010010111111 +001100111011 +000001101001 +001110110001 +010011011110 +001011011000 +100000111010 +001000100101 +010001100011 +011001000111 +010110101010 +101000001001 +110101011000 +001100000101 +100110011001 +000011001001 +100110111001 +111101101001 +011011001001 +010001100111 +001100011100 +011111101111 +100100111010 +101111110011 +110111001110 +000111100000 +011110000101 +111110100010 +001011001101 +110100101011 +011100001101 +111001111100 +111110100110 +000010010100 +010111110010 +001110001101 +011111010011 +110110111010 +101001110000 +011110110011 +000111011000 +010111000110 +001011100010 +001110000010 +010111000011 +010011111110 +011000010000 +101111011110 +101010000110 +011100011101 +011101101100 +100110101001 +000000011001 +100101010010 +110011010101 +010100100111 +011101011100 +011001111001 +000001001000 +001000101011 +010110111101 +100110010010 +010010010110 +000000111001 +011111110101 +011111001000 +100100011111 +000001111000 +111101010010 +101010100100 +011010111101 +000001011011 +101111100001 +100011000000 +110011001010 +000100010111 +100000010000 +101111010110 +010010010001 +101011100001 +101001011100 +001111000011 +111000100010 +001101110110 +001110111011 +100001110010 +000001110001 +111100001000 +100010011101 +000011000110 +111100100110 +000111110111 +000100111001 +011001110001 +111101010001 +110110101100 +010001011001 +001101111110 +000101111010 +110100100110 +111011001110 +110011010100 +010000100011 +011000111001 +110001110001 +101000000001 +101011000101 +011010000110 +000010111000 +010011100011 +111101101110 +001111010010 +100011011100 +111110111010 +000010110010 +101000011100 +010011101011 +110001011000 +000000010011 +001101010010 +110111111011 +111101011100 +111001010011 +110111100101 +111000111001 +001110010010 +001011101101 +110100100111 +101011100000 +011000100101 +110101010010 +000110000111 +111111001000 +000011100110 +101101110011 +001110011111 +110000101110 +110011100101 +101110100101 +001000111000 +101111000011 +100111111011 +011010101111 +101101001000 +000101101110 +111101000011 +000011000010 +000101010010 +101001011101 +110011010011 +111010010001 +110100010001 +010100001010 +110111101010 +100111000101 +101001100010 +011010011011 +101011101101 +011001100001 +001000110111 +000100101000 +010000000111 +111111110000 +001011110110 +100010010001 +100110101111 +110110010000 +100110011011 +001100110101 +100011101100 +000011011001 +010110100001 +011110101111 +101011011000 +101110111001 +111000010000 +011100000010 +100110100101 +010101011111 +100000111001 +011000111100 +101100100111 +101011111000 +111101111101 +101000100011 +101000010101 +111001010010 +101110000111 +101010000001 +000100100111 +100001011101 +110010101010 +000000100100 +011101000101 +110010001101 +111011101110 +111111111110 +011100000111 +101111110010 +000101100110 +000110001011 +100100011000 +010001101111 +011110010000 +100011011111 +010101101010 +000011100000 +000101001110 +001001110001 +111110011001 +101110000000 +010001010010 +110001000110 +101011001110 +010001101001 +000000011011 +101001010011 +011010000100 +001100001100 +100010001001 +100000111111 +001000000000 +100110110100 +100111110000 +101001110001 +000110101101 +000111001010 +101100011011 +110001101000 +000111101110 +110110010011 +100010001000 +001010001100 +001011111110 +001110000111 +110010100000 +100001110101 +000110111101 +110010011000 +111110111011 +101000000011 +011010011110 +110000001111 +011000110100 +111111001101 +000010110100 +101100001100 +110000101011 +010000001110 +101001101000 +011000000100 +101101011000 +100111111100 +110001001010 +001010101100 +111010001110 +101110100110 +011110010001 +111011111010 +000001100110 +010001100101 +100101001111 +111001001011 +111101101010 +011011000001 +010111010101 +110001111110 +110100101110 +111101011011 +011000101011 +010100111100 +011000000111 +101000101001 +111000110001 +101000110001 +001111010011 +100000010100 +001001101101 +010101110001 +100110000101 +011000101101 +001110110010 +100111000001 +011001000001 +000101101000 +101000101110 +000100001110 +111110101111 +011110110001 +101001110010 +000000111010 +101111010010 +011110011010 +101000011111 +000100001100 +110001111001 +110000110101 +111010011100 +001000111011 +111110001111 +011011011000 +000110110001 +101001010010 +111010100011 +000001111100 +110010000001 +110101111011 +100011001001 +010011100001 +101101011111 +100000001010 +001011001000 +000011100010 +011110111000 +000110111011 +010111001011 +011100011000 +101010011011 +001001011110 +000110001010 +111011010101 +010101110010 +011100101001 +100000110010 +111100111001 +001011011101 +110101101001 +001000111110 +101011000011 +001110111111 +100101010110 +111011011110 +010111111001 +011001010001 +110101111111 +110100100101 +111111110001 +010011000101 +011101101010 +101111000100 +011101000100 +001110110110 +010011011000 +000011011010 +110000000110 +001100001101 +111001111001 +100001100101 +100100010011 +111011111000 +001001111110 +101101000000 +000001100001 +001100001011 +110011011111 +010101111110 +001110011100 +111111000100 +010011000010 +101100010000 +000010110111 +110100000101 +110100010010 +111001100101 +010000010001 +101100100110 +001111101100 +011100110110 +000000111101 +001000000100 +010101101101 +100101101000 +001001010101 +000000001111 +110111110100 +000010110110 +001000000010 +011000101000 +100101001101 +010110101110 +100010110001 +010111010000 +001111011001 +000011110100 +100100111000 +011011100110 +100100010110 +000001111101 +010111011011 +110001000101 +111100010001 +101001000111 +110100010000 +001010000111 +101001110111 +010010010010 +100010111100 +001101000011 +011001110011 +000100101111 +010110011101 +001001011010 +111011101001 +111111000000 +111110000111 +110000001011 +010000110010 +011100011001 +011001100011 +001101000110 +011001011111 +100111100000 +110111010100 +101101000001 +110011101000 +001010110101 +100101010001 +101000101100 +010110001000 +111011111011 +000010010001 +010111011010 +011101101011 +001000101110 +101010100111 +001011000111 +011011010110 +110111101111 +100001110001 +100100101111 +110000010011 +011110111011 +000110000100 +110000111111 +100110101100 +001101110101 +011100000100 +001100011110 +010111010010 +001110101101 +100110001110 +100100010000 +001000001101 +011101110011 +011001100110 +110101000011 +100101001010 +111111101111 +010011000100 +100111000111 +101100101100 +010110000100 +111100000100 +000000101001 +000011110010 +011011110111 +011101110001 +111110000001 +011000011001 +100001101111 +110000011010 +110110101010 +110100110000 +111111000011 +100011100010 +100100010010 +110100011000 +000110000110 +001101001000 +101011110111 +000011110111 +001001100100 +100010110011 +100111111000 +000001111010 +110001111101 +100011111111 +111001001101 +011111101011 +101111110111 +100111000011 +010010001010 +010101101011 +011011110000 +110111000000 +010011101111 +010001010101 +111110101010 +001010101010 +001100101101 +111011000111 +101001000001 +000101000100 +010011110010 +001110100010 +110111001000 +111011001101 +111110001010 +110000100001 +000000010101 +000010011001 +011100100111 +001101111010 +000001100100 +011111101100 +110100011111 +010110000000 +001010101001 +000010010011 +111100000110 +110101000101 +010110001011 +111001001000 +010110101111 +001000000001 +111100001101 +001000110110 +110001101001 +001110001011 +111101101011 +011101111100 +100011001011 +001000001001 +000001001100 +100100110111 +111111101101 +001110100000 +001001001001 +001111001100 +101100101111 +101100110010 +101000100001 +101000010110 +100000010101 +011010011000 +101101101110 +011101001100 +101011111100 +101110101000 +110010100100 +011111011110 +110010011101 +110100100011 +111111100100 +011011011101 +011100100001 +101011110000 +001100100011 +000111111000 +110010001010 +100001001001 +110010001000 +111010001001 +010110111111 +101011001101 +101010011110 +011011110001 +101010100011 +100000110111 +100011101001 +101110010011 +101011011011 +110010010000 +011110100011 +010010110110 +100110010111 +110100000110 +101011101011 +001100010100 +101101010000 +110110001001 +010010001101 +111011010110 +111100001110 +000101111001 +100001000001 +101111101001 +100101101110 +000011001101 +000001000110 +111110001011 +000010101111 +011101101101 +000000000110 +111111101010 +101001110110 +111011000100 +001011101111 +110010111010 +110011110001 +111100011111 +110110010001 +100101110000 +010001001011 +101100010100 +110100001010 +001011101001 +110110000001 +001101001001 +100000111100 +001101101010 +010000110011 +011111101110 +110001110011 +011110011000 +000010001010 +001110000100 +000110101111 +100011111000 +100101100010 +111111110010 +101100011001 +011100100000 +100001100011 +001010011110 +111100011010 +010110100010 +101110010010 +001011110010 +100010111000 +001100110010 +001001101001 +011111101101 +101100111100 +011010011101 +101011100100 +111000001000 +010111011100 +001010000000 +011110000110 +110101101110 +000111000101 +101001011010 +110010100010 +100101110010 +000101100100 +010100110010 +011001000010 +110010011010 +010100001011 +011001111110 +011000101001 +011101011011 +010010000111 +000001110111 +100010101001 +111100011101 +100010001011 +111100101100 +011111010101 +111100101110 +111101001110 +111000010110 +000000110110 +011110000001 +000111011110 +111010111010 +101010001101 +111110101100 +000111110010 +110000111101 +111101110101 +101010101100 +101110110010 +110110011010 +111110110001 +011101111011 +101011111011 +100111101101 +000110011001 +100101101011 +110101101101 +010111110100 +001111110010 +101010101000 +011001100100 +101011111111 +001001000001 +010100100110 +111100011100 +011100011010 +000011011110 +001011101010 +010010001110 +101101101111 +000101000010 +010111100010 +010010010000 +111000000101 +010001100010 +101100001111 +011110111100 +100011000010 +010100001111 +011100101110 +011000111110 +111101010000 +110100111000 +001010001111 +111100011011 +101000111110 +100000011011 +010110000001 +001111100111 +001111101111 +011001011100 +111001000011 +110011101111 +001101100011 +101100111111 +011111101010 +010000011110 +001111111111 +010001001001 +111010101000 +110100001001 +111111010111 +000101110100 +101101100000 +001101100001 +001111111000 +100111101001 +000110010001 +100011101011 +000010000101 +101010001100 +000111110011 +001010110001 +111110001110 +010011100010 +100011010000 +100001010010 +100011000101 +110011000100 +010000010000 +110100110011 +100011000111 +010111101010 +101011000010 +110000101100 +111100001111 +000110100001 +010010110001 +111000100110 +000110111110 +100101000000 +011101001010 +101111011011 +111011101111 +000000010100 +110011101110 +111001110111 +011111000111 +111111011100 +110010011111 +111111110111 +001110110011 +000010010010 +100100100001 +111010000101 +001001110011 +101001101111 +101110101110 +101000101111 \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2021/Day3/sara.txt b/AdventOfCode/Problems/AOC2021/Day3/sara.txt new file mode 100644 index 0000000..d4a5493 --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day3/sara.txt @@ -0,0 +1,1000 @@ +101000111100 +000011111101 +011100000100 +100100010000 +011110010100 +101001100000 +110001010000 +111110011011 +000110100110 +110100000001 +110100001001 +010000100011 +000010011100 +111001110001 +011001000010 +100011100110 +101010100111 +001011001101 +001101100100 +111010110100 +010101011100 +001010010000 +101001111001 +101110010001 +110100011100 +001010111110 +011110110100 +110110101110 +000000101101 +001100000110 +110010110001 +010110101110 +100111000111 +000010101111 +010101111011 +011101000100 +010000011011 +110011111111 +000001100100 +100100110110 +100001101001 +110001000010 +010111110110 +101101011001 +010101101101 +010001101100 +100010000000 +111111001001 +111101001011 +010110011001 +011000000100 +011100101000 +111101001000 +111000110010 +110000000110 +001101011000 +101110010011 +100010011111 +111110010000 +000011010011 +111110100010 +111111001111 +101001011110 +111001101001 +111010011100 +001011001111 +001010011110 +110111101010 +101111101101 +011101110101 +001111110101 +010111110011 +000010011000 +111111111000 +000101001100 +110011001000 +100010100110 +100110101110 +001010101101 +110000111000 +101100010110 +000000100101 +111001101101 +111010001101 +111110000111 +010101000010 +100000101101 +000011100011 +011011000111 +011000011111 +100101011100 +101011000110 +111110001110 +001010011101 +101001101001 +001101111110 +000100000010 +011110000001 +010111101101 +010101100010 +000110000011 +100001111010 +011001011000 +001011110100 +001101111011 +001110110010 +001010101000 +100001001100 +101010100110 +100010101011 +111000010000 +100101100111 +010001011001 +010010101101 +001001000111 +100010111100 +001011100011 +100100001101 +110000110010 +100010011101 +111001011100 +101101011011 +010011111111 +010100111111 +000101100011 +111001110111 +000010100100 +101100000000 +111110100011 +110101111111 +001101001101 +010110000110 +110100010100 +011100011100 +110011001100 +000001010111 +110100001000 +110110100111 +010000001111 +100011111110 +001000010000 +011101001001 +100110101111 +000010101101 +100011010101 +010101001000 +110101011101 +111111101010 +101011101101 +010101001001 +101101000110 +110110101000 +110100100101 +000011110101 +001110101001 +011101011110 +111111110100 +010111000011 +100101010000 +010011111010 +111001101100 +100100010101 +100101100001 +001111000111 +110100101001 +010010011100 +011001000101 +111101010001 +001100010000 +111100000011 +000101001000 +010011010111 +101110011000 +110000111010 +110010001011 +111110110010 +000000001011 +011001111001 +001001001110 +100111001111 +110110001010 +110110010101 +110011001101 +010001101001 +000010100111 +100111111110 +111011100001 +011000100000 +111111000100 +111100100010 +110110110110 +001100111110 +101000100001 +000010000101 +111101011101 +001001001101 +101011001101 +000010010001 +011010110001 +110000000010 +000111001000 +111110101100 +011111000111 +010101111000 +010100010110 +111111100100 +110011010001 +010110001110 +000100010011 +100101100011 +101010001011 +001010111101 +111110111000 +101001100011 +011100110010 +101001101010 +111011110000 +011101110000 +000001101111 +111101101100 +110101000101 +011000110101 +000111100101 +111110011101 +000111101010 +010011100101 +101001001001 +100010101000 +111111100011 +011011101111 +001011111001 +001001101011 +100101100101 +100011111000 +010110100001 +000100111111 +010100111101 +010100100000 +110000101000 +100001011000 +100010011000 +100011101111 +001100010001 +010001010001 +011011001001 +001001110110 +111011011100 +000110010000 +111110011111 +001111101000 +010010010001 +000011001110 +101011101100 +001000110110 +111000010001 +010111000101 +110000001110 +000011111010 +010000010100 +011101001110 +000111011011 +000111110010 +111110110101 +101110100001 +111101011100 +010001111001 +010011011000 +111110111011 +111000100110 +000110001001 +100000111110 +001101101010 +101100111010 +000101011111 +001101010111 +011001010011 +001111010110 +010101000000 +010011100001 +010011001000 +110110011011 +110100111000 +101110000111 +100101110111 +000001011100 +111110111110 +000001011010 +010101101000 +010010100100 +011100010011 +110111001000 +110011100001 +111100111010 +010001110100 +000100100111 +000001110011 +110011100111 +010010101111 +100010001101 +111101101011 +110100010101 +101010111110 +111010011110 +111101101101 +000100010000 +111101010010 +011100001111 +101010110001 +000111111100 +010100001101 +111100010000 +101011110110 +010100011000 +100111101001 +011100000000 +000110110110 +001111010011 +111100111101 +101111101100 +001000101100 +111100011000 +010101110000 +001110110001 +000000011110 +010010111101 +000001001001 +110100101101 +100010010100 +010100001001 +100110001000 +000011001011 +101011001111 +100010100111 +011101100000 +110111101111 +001110100010 +100101101110 +110100000000 +001010100100 +011100110100 +011100011111 +010101100110 +011000001000 +100000111010 +011011100000 +000011110011 +010111011111 +011100101111 +111011111001 +110010111101 +101101110101 +101100100010 +001001111100 +100100111001 +101101111110 +001100100110 +011011100101 +110111010010 +101100111111 +000011110111 +001110111101 +100110000000 +001100010100 +110001101101 +011010100000 +000100011010 +011001011010 +011111111111 +011000001100 +111111110001 +000000101110 +000110011111 +001110100000 +001110011101 +000000011000 +101001110011 +000101101010 +000100001110 +101100011011 +110010101111 +011101000001 +110001011000 +111011011111 +111110100100 +010101011111 +110011111000 +000101111111 +001101011101 +010111011011 +101000110011 +001110010001 +010010001001 +000010011010 +000010101001 +110111110100 +100101001000 +000101000100 +101111001001 +101111111111 +010000010010 +101001110000 +100100001110 +001001110010 +111010110010 +110011011111 +101001110100 +011100111111 +100000111111 +110101101100 +000100110100 +100110101010 +100100000100 +010010000110 +010110001100 +000010001001 +001100001010 +100001101000 +100010011001 +010101101110 +010000111001 +101110110010 +111100100000 +110000101110 +101001101111 +100101001100 +111011101111 +001111000001 +011101111101 +011000010111 +101011111001 +101011010110 +010000000011 +100010011011 +001111001011 +101000011101 +001101100010 +100111010101 +000111100001 +101111010011 +000101101100 +111110000101 +110100011110 +011101101000 +100000111001 +111000101001 +000011010100 +110101011011 +010011100011 +111011010110 +010110111000 +110111010101 +001111111110 +000011011101 +001011000100 +000101101111 +101100011100 +001001101100 +011110111100 +010001000001 +100100101101 +001101010101 +101101111001 +111001101011 +100111001010 +100110111001 +110000101111 +001001110101 +011101101111 +101101100011 +101000011111 +101010111101 +011100010111 +011010110000 +100111101111 +110101110100 +011111011000 +110010100000 +010110100000 +100010101111 +100101001110 +000101000000 +100111110111 +000101100100 +010111101111 +110100101000 +110111001010 +010011000000 +000110110100 +000110011010 +110000010110 +111000100100 +001110001110 +100011100111 +100101000100 +100110110100 +110010110011 +001100101000 +000101001001 +110101000100 +100101010101 +000101011101 +111110000001 +000101110001 +011001011100 +101001110010 +010111001000 +100000000111 +101110101000 +111000001011 +101111110111 +111000111010 +011101000111 +101111110001 +011010101111 +111100100100 +100111000101 +000011100110 +011110110010 +110110100001 +011111010000 +100111010000 +011000101000 +011001000111 +000000001000 +000100101010 +010011001111 +100100011100 +011110100011 +000010111101 +000110010110 +110010100010 +001100011011 +101101000001 +110111101011 +110101100000 +100101011000 +110111011111 +111011100000 +011101101101 +110101001000 +000100111000 +101100101001 +111101101111 +000100111100 +000101111110 +011100101011 +111101000101 +111000101000 +010111110001 +111011001001 +110001010010 +110101001101 +101101000010 +110101010100 +001100011010 +001100001001 +101011000101 +110101010001 +010111011100 +001110110011 +111110101000 +101101010111 +001110001010 +011000011101 +101111000011 +101000000000 +001000000010 +000110111001 +110100110110 +111000100010 +110011010000 +101000101111 +001100101111 +111111111001 +110001001000 +010100010101 +000100101100 +101110111111 +101001001100 +010111100011 +101100101101 +101101001111 +010110011010 +000101001010 +100111110010 +101110000110 +000000110111 +001001010110 +010101000110 +001101000101 +001011101000 +101010110110 +010111000000 +111110001001 +011101110001 +000101110100 +101110101011 +100010101001 +111001100101 +011010010101 +111010010000 +001010110111 +100111101101 +001011001001 +111100110001 +000000000100 +011010110111 +100101011111 +011000001110 +100111010010 +111111000111 +011110110110 +011000110011 +000101101011 +011011101101 +001110111010 +111011110100 +001100101001 +111001111011 +111111101111 +011011000101 +011000110010 +100011110001 +111101110001 +100010101100 +010111111001 +001001100100 +100010111110 +110111100111 +100110000100 +111110011000 +110000100011 +011000010010 +011110011001 +101011011111 +010111001001 +010001011101 +111000100101 +001011000111 +010011111110 +011110011110 +011011111100 +011000110000 +101100111110 +010101111110 +110111100010 +000111001101 +111100000101 +001001010010 +001101111111 +101000011001 +000111010110 +010101100011 +011101110110 +101100110010 +100001010001 +010110001010 +010101011110 +111110101101 +000011111110 +010010000100 +010111000111 +000110001011 +010000101001 +000000010000 +100011100000 +000001100110 +101100011000 +011001110001 +010011011001 +110000000111 +000101101110 +010011000111 +110100011010 +001110100111 +000100010101 +000110110111 +100001010000 +001110001000 +111101100100 +001001100000 +100011111010 +101111001110 +100010111011 +011110000110 +101110100000 +001100001100 +011010010000 +011001111100 +110011011110 +110110001111 +001100010111 +101110001100 +101110101010 +000010000111 +101010000101 +010011011010 +010010011000 +010100101011 +001110000101 +000110111111 +011011110110 +111111100110 +100011110101 +001011011110 +111001111100 +110011101001 +011010001011 +110001100001 +101100101100 +011100001001 +010101011000 +111111111011 +110001111011 +110111110011 +111100001010 +110011011000 +100011001000 +011100100000 +110110110010 +101011110101 +001101100101 +111001010000 +111010100110 +011011011101 +001000011001 +000111011100 +111001001100 +101111010000 +100101101101 +000010010110 +101110111010 +010011000011 +001000011010 +000101000010 +100000110100 +100000011101 +110001001111 +010100101100 +100100010111 +010111100001 +010000111101 +001000100101 +101101111100 +111001110101 +111101010110 +010101001100 +100101011011 +100001100101 +001100101011 +111101011110 +001100100100 +000000101100 +010000101100 +011010011011 +111101101001 +110010110111 +010101111111 +011001101000 +011000010001 +001111011011 +001001011111 +110101000000 +110101000010 +100111110100 +111010011010 +000011001111 +101010110010 +100101111001 +100110100110 +111001110011 +011000100010 +001110010011 +001110001111 +100101000101 +111000011101 +101111101010 +011001100111 +001000000011 +010000010101 +100111111100 +011011011111 +010110100110 +010111101110 +010001101010 +000001101000 +101011000111 +111011000010 +101101100000 +000100010100 +000010011011 +111000101101 +001100110000 +111010100011 +101100000110 +100011100011 +100011100001 +111011110111 +100111100100 +111001110000 +010111100000 +111001011110 +110011100101 +111101010000 +010101001010 +100110101011 +100101101001 +101000001001 +011110111101 +010000110110 +011111000000 +111100101101 +011100110011 +011011000011 +111000000001 +001010101010 +111011111101 +110001110000 +000001010001 +101000100100 +000001001100 +101010010011 +001000000100 +010001111000 +011100000001 +111100111100 +100011000011 +111011110110 +100000101111 +111101000001 +100100110100 +010110111111 +111100010011 +100001000001 +111101000000 +110010001101 +011110010011 +110010111010 +011011001111 +110111101100 +000000111001 +010111111100 +100110101000 +110000110110 +010010110100 +101101001101 +101100000010 +000101000001 +001001010100 +000110111101 +100100001011 +100100110001 +110001000001 +111101011010 +001010110001 +110111000111 +010101000011 +111101101110 +101011000000 +010101100101 +110010100110 +001010001000 +110000101001 +010100100111 +111110010111 +000010110010 +001101010001 +101111110101 +100011001010 +110011111001 +111001011010 +100011101010 +100111100101 +001101110111 +111000110101 +010000100001 +000000110110 +010111110100 +000111110011 +000101000101 +010101000101 +001101101101 +111000010111 +000100000011 +111011000001 +001111001110 +101110010101 +010011010001 +110111111110 +001011100001 +101010011110 +110010010101 +101100110001 +001111111010 +001111011111 +010110111001 +100101010010 +110101110111 +000110111011 +100000010010 +111000110100 +000100111001 +011011001010 +110000000000 +101001010011 +100100001001 +011111101100 +011101110011 +011101000101 +000100100001 +001011001011 +110101101111 +110010111111 +100100100110 +110110001000 +001001101111 +111001000000 +001101111000 +101110011010 +110001101111 +000000101010 +110001001010 +000110111110 +101111010110 +010000001000 +111011110010 +110111011110 +010011000001 +010101001111 +001101110110 +001000011111 +010110110011 +111000001101 +010000110111 +000101110110 +000011011110 +111110010010 +100100110000 +000100001111 +011100101101 +001101010000 +100101001001 +001001010000 +000100001001 +011100011101 +101000111010 +100111011110 +000000001111 +100001100100 +111101100011 +111001111010 +110110100011 +101110101111 +101000001110 +000011010101 +010001000110 +010110100101 +010000110010 +100110011001 +100100101111 +110110001001 +111111010111 +011010011111 +111110011010 +011011011011 +111101001110 +110111100001 +011111101010 +010010111000 +000010010000 +111110101010 +011011101100 +100100111000 +110100000110 +110111010100 +101101011110 +100100111011 +101001011011 +011000111001 +111100111011 +110101011010 +101010101010 +001010010010 +101001110111 +001001100010 +001010101110 +000001001111 +011010001111 +010110100111 +011110110000 +111001000101 +001110100110 +111001010010 +101001010010 +001011000010 +111010101010 +101010110000 +111100010100 +000111100111 +110111100100 diff --git a/AdventOfCode/Problems/AOC2021/Day3/test.txt b/AdventOfCode/Problems/AOC2021/Day3/test.txt new file mode 100644 index 0000000..5ca60e4 --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day3/test.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2021/Day6/LanternFish.cs b/AdventOfCode/Problems/AOC2021/Day6/LanternFish.cs new file mode 100644 index 0000000..410b5bd --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day6/LanternFish.cs @@ -0,0 +1,60 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AdventOfCode.Problems.AOC2021.Day6; + +[ProblemInfo(2021, 6, "Lanternfish")] +internal class LanternFish : Problem +{ + public int[] Data { get; private set; } = Array.Empty(); + + public override void LoadInput() + { + var input = ReadInputLines(); + Data = input.First().Split(",").Select(v => int.Parse(v)).ToArray(); + } + public override void CalculatePart1() + { + var lifetimes = PrepareLifetimes(); + Part1 = Simulate(lifetimes, 80); + + } + + public override void CalculatePart2() + { + var lifetimes = PrepareLifetimes(); + Part2 = Simulate(lifetimes, 256); + } + + private long[] PrepareLifetimes() + { + var lifetimes = new long[9]; + foreach (var life in Data) + lifetimes[life] += 1; + + return lifetimes; + } + + private static long Simulate(long[] lifetimes, int days) + { + for (int i = 0; i < days; i++) + { + var day0Count = lifetimes[0]; + for (int j = 1; j < lifetimes.Length; j++) + lifetimes[j - 1] = lifetimes[j]; + lifetimes[6] += day0Count; + lifetimes[^1] = day0Count; + } + + return lifetimes.Sum(); + } + + + + +} diff --git a/AdventOfCode/Problems/AOC2021/Day6/input.txt b/AdventOfCode/Problems/AOC2021/Day6/input.txt new file mode 100644 index 0000000..22e0632 --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day6/input.txt @@ -0,0 +1 @@ +1,3,4,1,1,1,1,1,1,1,1,2,2,1,4,2,4,1,1,1,1,1,5,4,1,1,2,1,1,1,1,4,1,1,1,4,4,1,1,1,1,1,1,1,2,4,1,3,1,1,2,1,2,1,1,4,1,1,1,4,3,1,3,1,5,1,1,3,4,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,5,2,5,5,3,2,1,5,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,1,1,1,1,5,1,1,1,1,1,4,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,2,4,1,5,5,1,1,5,3,4,4,4,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,5,3,1,4,1,1,2,2,1,2,2,5,1,1,1,2,1,1,1,1,3,4,5,1,2,1,1,1,1,1,5,2,1,1,1,1,1,1,5,1,1,1,1,1,1,1,5,1,4,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,4,5,1,1,1,1,1,1,1,5,1,1,3,1,1,1,3,1,4,2,1,5,1,3,5,5,2,1,3,1,1,1,1,1,3,1,3,1,1,2,4,3,1,4,2,2,1,1,1,1,1,1,1,5,2,1,1,1,2 \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2021/Day6/test.txt b/AdventOfCode/Problems/AOC2021/Day6/test.txt new file mode 100644 index 0000000..72bfd5d --- /dev/null +++ b/AdventOfCode/Problems/AOC2021/Day6/test.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file diff --git a/AdventOfCode/Problems/AOC2022/Day0/TestProblem.cs b/AdventOfCode/Problems/AOC2022/Day0/TestProblem.cs index 923797f..6fd2c18 100644 --- a/AdventOfCode/Problems/AOC2022/Day0/TestProblem.cs +++ b/AdventOfCode/Problems/AOC2022/Day0/TestProblem.cs @@ -2,7 +2,7 @@ using AdventOfCode.Runner.Attributes; namespace AdventOfCode.Problems.AOC2022.Day0; -[ProblemInfo("2022", 0, "Fancy Test")] +[ProblemInfo(2022, 0, "Fancy Test")] public class TestProblem : Problem { public override void LoadInput() diff --git a/AdventOfCode/Problems/AOC2022/Day1/CalorieCounting.cs b/AdventOfCode/Problems/AOC2022/Day1/CalorieCounting.cs index f686477..8093753 100644 --- a/AdventOfCode/Problems/AOC2022/Day1/CalorieCounting.cs +++ b/AdventOfCode/Problems/AOC2022/Day1/CalorieCounting.cs @@ -8,7 +8,7 @@ using System.Text; using System.Threading.Tasks; namespace AdventOfCode.Problems.AOC2022.Day1; -[ProblemInfo("2022", 1, "Calorie Counting")] +[ProblemInfo(2022, 1, "Calorie Counting")] internal class CalorieCounting : Problem { public List> FlaresFood { get; set; } diff --git a/AdventOfCode/Problems/AOC2022/Day2/RockPaperScissors.cs b/AdventOfCode/Problems/AOC2022/Day2/RockPaperScissors.cs index 9434ee9..d635557 100644 --- a/AdventOfCode/Problems/AOC2022/Day2/RockPaperScissors.cs +++ b/AdventOfCode/Problems/AOC2022/Day2/RockPaperScissors.cs @@ -3,7 +3,7 @@ using AdventOfCode.Runner.Attributes; namespace AdventOfCode.Problems.AOC2022.Day2; -[ProblemInfo("2022", 2, "Rock Paper Scissors")] +[ProblemInfo(2022, 2, "Rock Paper Scissors")] internal class RockPaperScissors : Problem { private string[] _lines; diff --git a/AdventOfCode/Problems/AOC2022/Day3/RucksackReorganization.cs b/AdventOfCode/Problems/AOC2022/Day3/RucksackReorganization.cs index e4ce902..454bcb5 100644 --- a/AdventOfCode/Problems/AOC2022/Day3/RucksackReorganization.cs +++ b/AdventOfCode/Problems/AOC2022/Day3/RucksackReorganization.cs @@ -8,7 +8,7 @@ using System.Text; using System.Threading.Tasks; namespace AdventOfCode.Problems.AOC2022.Day3; -[ProblemInfo("2022", 3, "Rucksack Reorganization")] +[ProblemInfo(2022, 3, "Rucksack Reorganization")] internal class RucksackReorganization : Problem { private string[] _sacks; diff --git a/AdventOfCode/Problems/AOC2023/Day0/TestProblem.cs b/AdventOfCode/Problems/AOC2023/Day0/TestProblem.cs index 215fd17..16a85fd 100644 --- a/AdventOfCode/Problems/AOC2023/Day0/TestProblem.cs +++ b/AdventOfCode/Problems/AOC2023/Day0/TestProblem.cs @@ -2,7 +2,7 @@ using AdventOfCode.Runner.Attributes; namespace AdventOfCode.Problems.AOC2023.Day0; -[ProblemInfo("2023", 0, "Test")] +[ProblemInfo(2023, 0, "Test")] public class TestProblem : Problem { public override void LoadInput() diff --git a/AdventOfCode/Program.cs b/AdventOfCode/Program.cs index 10b5ce5..95aa677 100644 --- a/AdventOfCode/Program.cs +++ b/AdventOfCode/Program.cs @@ -1,4 +1,5 @@ -using AdventOfCode.Runner; + +global using AdventOfCode.Runner; namespace AdventOfCode; diff --git a/AdventOfCode/Runner/AOCRunner.cs b/AdventOfCode/Runner/AOCRunner.cs index df0dfa5..cd80819 100644 --- a/AdventOfCode/Runner/AOCRunner.cs +++ b/AdventOfCode/Runner/AOCRunner.cs @@ -8,12 +8,13 @@ namespace AdventOfCode.Runner; public class AOCRunner { - private Dictionary> _loadedProblems; - private List _years; - private string _selectedYear; + private Dictionary> _loadedProblems; + private List _years; + private int _selectedYear; private int _selectedDay; private int _scollOffset = 0; private int _maxProblemCount; + private bool _isQuiting; private bool _isProblemMode = false; @@ -21,8 +22,8 @@ public class AOCRunner { Console.OutputEncoding = Encoding.UTF8; _loadedProblems = new(); - _years = new List(); - _selectedYear = DateTime.Now.Year.ToString(); + _years = new List(); + _selectedYear = DateTime.Now.Year; FindProblemClasses(); InitSizing(); @@ -50,7 +51,7 @@ public class AOCRunner private void FindProblemClasses() { - var types = Assembly.GetExecutingAssembly().DefinedTypes.Where(t => t.IsAssignableTo(typeof(Problem)) && !t.IsInterface); + var types = Assembly.GetExecutingAssembly().DefinedTypes.Where(t => !t.IsAbstract); if (types == null) return; foreach (var type in types) @@ -68,7 +69,7 @@ public class AOCRunner _years = _loadedProblems.Keys.OrderDescending().ToList(); } - private void RunDay(string year, int dayIndex) + private void RunDay(int year, int dayIndex) { var yearList = _loadedProblems[year]; if (yearList.Count <= dayIndex || dayIndex < 0) @@ -84,7 +85,7 @@ public class AOCRunner Console.ForegroundColor = ConsoleColor.Gray; - if (Activator.CreateInstance(problemType) is not Problem problem) + if (Activator.CreateInstance(problemType) is not IProblem problem) { Console.WriteLine("Failed to create problem isntance"); return; @@ -122,6 +123,11 @@ public class AOCRunner Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"{sw.ElapsedMilliseconds}ms"); } + catch (NotImplementedException) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Not Implemented"); + } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; @@ -141,7 +147,7 @@ public class AOCRunner Console.ForegroundColor = ConsoleColor.Gray; Console.CursorVisible = false; Console.Clear(); - while (true) + while (!_isQuiting) { InitSizing(); RenderTopBar(); @@ -205,6 +211,9 @@ public class AOCRunner case ConsoleKey.Enter: _isProblemMode = true; break; + case ConsoleKey.Escape: + _isQuiting = true; + break; } ConstrainListScroll(); } @@ -232,9 +241,9 @@ public class AOCRunner if (end >= tabMaxPos) break; if(year == _selectedYear) - DrawSelectedButton(year, 2, col, buttonWidth, 1, ConsoleColor.Red, ConsoleColor.Blue); + DrawSelectedButton(year.ToString(), 2, col, buttonWidth, 1, ConsoleColor.Red, ConsoleColor.Blue); else - DrawButton(year, 2, col, buttonWidth, 1, ConsoleColor.Gray, Console.BackgroundColor); + DrawButton(year.ToString(), 2, col, buttonWidth, 1, ConsoleColor.Gray, Console.BackgroundColor); } @@ -290,7 +299,6 @@ public class AOCRunner private void DrawSelectedButton(string text, int row, int col, int width, int height, ConsoleColor color = ConsoleColor.Gray, ConsoleColor background = ConsoleColor.Black, bool centered = true, int padding = 0) { //text = $"\ue0c7{text}\ue0c6"; - Console.SetCursorPosition(row, col); var origBg = Console.BackgroundColor; Console.BackgroundColor = background; for (int y = row; y < row + height; y++) @@ -318,7 +326,6 @@ public class AOCRunner private void DrawButton(string text, int row, int col, int width, int height, ConsoleColor color = ConsoleColor.Gray, ConsoleColor background = ConsoleColor.Black, bool centered = true, int padding = 0) { - Console.SetCursorPosition(row, col); var origBg = Console.BackgroundColor; Console.BackgroundColor = background; for (int y = row; y < row + height; y++) diff --git a/AdventOfCode/Runner/Attributes/ProblemInfoAttribute.cs b/AdventOfCode/Runner/Attributes/ProblemInfoAttribute.cs index d99d754..3fdb609 100644 --- a/AdventOfCode/Runner/Attributes/ProblemInfoAttribute.cs +++ b/AdventOfCode/Runner/Attributes/ProblemInfoAttribute.cs @@ -2,9 +2,9 @@ public class ProblemInfoAttribute : Attribute { public int Day { get; init; } - public string Year { get; init; } + public int Year { get; init; } public string Name { get; init; } - public ProblemInfoAttribute(string year, int day, string name) + public ProblemInfoAttribute(int year, int day, string name) { Year = year; Day = day; diff --git a/AdventOfCode/Runner/Problem.cs b/AdventOfCode/Runner/Problem.cs index ccd794c..13d7487 100644 --- a/AdventOfCode/Runner/Problem.cs +++ b/AdventOfCode/Runner/Problem.cs @@ -4,16 +4,34 @@ using System.Reflection; namespace AdventOfCode.Runner; -public abstract class Problem +public interface IProblem { - protected string? Part1 { get; set; } - protected string? Part2 { get; set; } + void LoadInput(); + + void CalculatePart1(); + + void PrintPart1(); + + void CalculatePart2(); + + void PrintPart2(); +} + +public abstract class Problem : Problem +{ +} + +public abstract class Problem : IProblem +{ + protected TPart1? Part1 { get; set; } + protected TPart2? Part2 { get; set; } public abstract void LoadInput(); public abstract void CalculatePart1(); - public virtual void PrintPart1() { + public virtual void PrintPart1() + { Console.ForegroundColor = ConsoleColor.Gray; if (Part1 == null) {