├── .gitignore ├── Gonduls ├── d01 │ ├── input.txt │ └── results.cpp ├── d02 │ ├── input.txt │ └── results.cpp ├── d03 │ ├── input.txt │ ├── part1.cpp │ └── part2.cpp ├── d04 │ ├── input.txt │ └── results.cpp ├── d05 │ ├── input.txt │ ├── part1.cpp │ └── part2.cpp ├── d06 │ ├── input.txt │ ├── part1.cpp │ └── part2.cpp ├── d07 │ ├── input.txt │ └── results.cpp ├── d08 │ ├── input.txt │ └── results.cpp ├── d09 │ ├── input.txt │ ├── part1.cpp │ └── part2.cpp ├── d10 │ ├── input.txt │ ├── part1.cpp │ └── part2.cpp └── d11 │ ├── input.txt │ ├── part2.py │ ├── problem1.asm │ └── problem2.asm ├── LICENSE ├── MatteoBlack ├── d01 │ ├── day01.py │ └── input.txt ├── d02 │ ├── day02.py │ └── input.txt ├── d03 │ ├── day03.py │ └── input.txt ├── d04 │ ├── day04.py │ └── input.txt ├── d05 │ ├── day05.py │ └── input.txt ├── d06 │ ├── day06.py │ └── input.txt ├── d07 │ ├── day07.py │ └── input.txt ├── d08 │ ├── day08.py │ └── input.txt ├── d09 │ ├── day09.py │ └── input.txt ├── d10 │ ├── day10.py │ └── input.txt ├── d11 │ ├── day11.py │ └── input.txt └── d12 │ ├── day12.py │ ├── input.txt │ └── test.txt ├── Puricelli └── d01 │ ├── input.txt │ └── puzzle1.py ├── README.md ├── Tommimon ├── CMakeLists.txt ├── d01 │ ├── input │ └── part1and2.cpp ├── d02 │ ├── input │ └── part1and2.cpp ├── d03 │ ├── input │ └── part1and2.cpp ├── d04 │ ├── input │ └── part1and2.cpp ├── d05 │ ├── input │ └── part1and2.cpp ├── d06 │ ├── input │ └── part1and2.cpp ├── d07 │ ├── input │ └── part1and2.cpp ├── d08 │ ├── input │ ├── part1.cpp │ └── part2.cpp ├── d09 │ ├── input │ └── part1and2.cpp ├── d10 │ ├── input │ └── part1and2.cpp ├── d11 │ ├── input │ ├── part1.cpp │ └── part2.cpp ├── d12 │ ├── input │ └── part1and2.cpp ├── d13 │ ├── input │ └── part1and2.cpp ├── d14 │ ├── input │ └── part1and2.cpp ├── d15 │ ├── input │ ├── part1.cpp │ └── part2.cpp ├── d16 │ ├── input │ └── part1and2.cpp ├── d17 │ ├── input │ └── part1and2.cpp ├── d18 │ ├── input │ ├── part1.cpp │ └── part2.cpp ├── d19 │ ├── input │ └── part1and2.cpp ├── d20 │ ├── input │ └── part1and2.cpp ├── d21 │ ├── input │ └── part1and2.cpp ├── d22 │ ├── input │ ├── part1.cpp │ └── part2.cpp ├── d23 │ ├── input │ └── part1and2.cpp ├── d24 │ ├── input │ └── part1and2.cpp ├── d25 │ ├── input │ └── part1and2.cpp ├── input-downloader.py ├── py++.cpp ├── py++.hpp └── template │ ├── input │ └── part1and2.cpp ├── leaderboard.py ├── marcomole00 ├── d01 │ ├── d1.raku │ ├── input │ └── p1.py ├── d02 │ ├── input.txt │ └── sol.py ├── d03 │ ├── input │ ├── sol.py │ └── test.py ├── d04 │ ├── input │ └── sol.py ├── d05 │ ├── input │ └── sol.py ├── d06 │ ├── input │ └── sol.py ├── d07 │ ├── input │ └── sol.py ├── d08 │ ├── input │ └── sol.py ├── d09 │ ├── input │ └── sol.py ├── d10 │ ├── input │ └── sol.py ├── d11 │ ├── input.txt │ └── sol.py ├── d12 │ ├── input │ └── sol.py └── d13 │ ├── input.txt │ └── sol.py ├── marcoparadina ├── d01 │ ├── d01.py │ └── input.txt └── d02 │ ├── d02.py │ ├── input.txt │ └── out.txt ├── mynam3isg00d ├── d01 │ ├── input.txt │ └── src │ │ └── Main.scala ├── d02 │ ├── README.txt │ ├── d02.json │ └── input.txt ├── d04 │ ├── input.txt │ └── src │ │ └── main.rs ├── d07 │ ├── d07.py │ └── input.txt └── d09 │ ├── d09.py │ └── input.txt ├── riccardo-negri ├── .gitignore ├── d01 │ ├── input.txt │ └── x.go ├── d02 │ ├── input.txt │ └── x.go ├── d03 │ ├── input.txt │ └── x.go ├── d04 │ ├── input.txt │ └── x.go ├── d05 │ ├── Cargo.toml │ ├── input.txt │ └── src │ │ └── main.rs └── d06 │ ├── Cargo.toml │ ├── input.txt │ └── src │ └── main.rs ├── riccardoinghilleri ├── d01 │ ├── input.txt │ └── main.py ├── d02 │ ├── input.txt │ └── main.py ├── d03 │ ├── input.txt │ └── main.py ├── d04 │ ├── input.txt │ └── main.py ├── d05 │ ├── input.txt │ └── main.py ├── d06 │ ├── input.txt │ └── main.py ├── d07 │ ├── input.txt │ └── main.py └── d08 │ ├── input.txt │ ├── main1.py │ └── main2.py ├── setup.py └── zoythum └── d01 ├── input.txt └── solve.py /.gitignore: -------------------------------------------------------------------------------- 1 | # JetBrains related 2 | .idea/ 3 | advent-of-code-2021.iml 4 | 5 | # VS Code related 6 | .vscode/ 7 | .vs/ 8 | 9 | # Private user files 10 | private/ 11 | cookies 12 | 13 | # Mac files 14 | .DS_Store 15 | /out/ 16 | 17 | # Python 18 | __pycache__/ 19 | venv/ 20 | 21 | # C/C++ executable files 22 | *.exe -------------------------------------------------------------------------------- /Gonduls/d01/results.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d01/input.txt" 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | char letter; 10 | ifstream input(INPUT); 11 | bool groupEnd = false; 12 | int maxG[3] = {0,0,0}, currentG = 0, currentInt = 0; 13 | while (input.get(letter)){ 14 | if (letter == '\n'){ 15 | if (groupEnd){ 16 | 17 | // swap elements if greater, continue to keep consistent sort 18 | for(int i = 0; i< 3; i++){ 19 | if(currentG > maxG[i]) 20 | swap(currentG, maxG[i]); 21 | } 22 | currentG = 0; 23 | } 24 | else 25 | currentG += currentInt; 26 | 27 | groupEnd = !groupEnd; 28 | currentInt = 0; 29 | continue; 30 | } 31 | 32 | groupEnd = false; 33 | currentInt = currentInt * 10 + letter - '0'; 34 | 35 | } 36 | currentG += currentInt; 37 | for(int i = 0; i< 3; i++) 38 | if(currentG > maxG[i]) 39 | swap(currentG, maxG[i]); 40 | cout << "result part 1: " << maxG[0] << endl; 41 | cout << "result part 2: " << maxG[0] + maxG[1] + maxG[2] << endl; 42 | } -------------------------------------------------------------------------------- /Gonduls/d02/results.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d02/input.txt" 5 | using namespace std; 6 | 7 | int main() { 8 | ifstream file(INPUT); 9 | string str; 10 | int result1 = 0, result2 = 0; 11 | while (getline(file, str)){ 12 | int sec = str[2] - 'X' + 1; // x=1, y=2, z=3 13 | int first = str[0] - 'A' + 1; // a=1, b=2, c=3 14 | 15 | // part 1 16 | result1 += sec; 17 | if (first == sec) 18 | result1 += 3; 19 | else if (sec == first +1 or sec == first -2) 20 | result1 += 6; 21 | 22 | // part 2 23 | result2 += (sec - 1) * 3; 24 | int toAdd = 0; 25 | switch (sec){ 26 | case 1: //lost 27 | toAdd = first - 1; 28 | toAdd = toAdd > 0 ? toAdd : toAdd +3; 29 | break; 30 | case 2: //tied 31 | toAdd = first; 32 | break; 33 | case 3: //won 34 | toAdd = first + 1; 35 | toAdd = toAdd < 4 ? toAdd : toAdd -3; 36 | break; 37 | } 38 | result2 += toAdd; 39 | } 40 | cout << "Result part1 = " << result1 << endl; 41 | cout << "Result part2 = " << result2 << endl; 42 | } -------------------------------------------------------------------------------- /Gonduls/d03/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d03/input.txt" 5 | using namespace std; 6 | 7 | int index(char letter){ 8 | if (letter <= 'z' && letter >= 'a') 9 | return letter - 'a'; 10 | 11 | return letter - 'A' + 26; 12 | } 13 | 14 | int main(){ 15 | ifstream file(INPUT); 16 | string str; 17 | int result = 0; 18 | while (getline(file, str)){ 19 | int letters[56]; 20 | for (int i = 0; i < 56; i++) 21 | letters[i] = 0; 22 | 23 | for (int i = 0; i < str.size() / 2; i++){ 24 | char a = str[i]; 25 | letters[index(a)]++; 26 | } 27 | 28 | for (int i = str.size() / 2; i < str.size(); i++) 29 | if (letters[index(str[i])] > 0){ 30 | result += index(str[i]) + 1; 31 | break; 32 | } 33 | } 34 | cout << "Result part1 = " << result << endl; 35 | } -------------------------------------------------------------------------------- /Gonduls/d03/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d03/input.txt" 5 | using namespace std; 6 | 7 | int index(char letter){ 8 | if (letter <= 'z' && letter >= 'a') 9 | return letter - 'a'; 10 | 11 | return letter - 'A' + 26; 12 | } 13 | 14 | int main(){ 15 | ifstream file(INPUT); 16 | string str; 17 | int result = 0; 18 | int line = 0; 19 | while (getline(file, str)){ 20 | int letters[3][56]; 21 | 22 | for (int i = 0; i < 56; i++) 23 | letters[line][i] = 0; 24 | 25 | for (int i = 0; i < str.size(); i++) 26 | letters[line][index(str[i])]++; 27 | 28 | if(line == 2){ 29 | for (int i = 0; i < 56; i++){ 30 | int a = letters[0][i]; 31 | int b = letters[1][i]; 32 | int c = letters[2][i]; 33 | if (a*b*c!= 0){ 34 | result += i + 1; 35 | break; 36 | } 37 | } 38 | line = -1; 39 | } 40 | line ++; 41 | } 42 | cout << "Result part2 = " << result << endl; 43 | } -------------------------------------------------------------------------------- /Gonduls/d04/results.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d04/input.txt" 5 | using namespace std; 6 | 7 | int main(){ 8 | ifstream file(INPUT); 9 | string str; 10 | int result1 = 0; 11 | int result2 = 0; 12 | while (file >> str){ 13 | int numbers[4] = {0, 0, 0, 0}; 14 | int index = 0; 15 | for (auto &ch : str) { 16 | if (ch == '-' || ch == ','){ 17 | index ++; 18 | continue; 19 | } 20 | numbers[index] = numbers[index] * 10 + ch - '0'; 21 | } 22 | 23 | // part 1 24 | if(numbers[0] <= numbers[2] && numbers[3] <= numbers[1]) 25 | result1 ++; 26 | else if(numbers[0] >= numbers[2] && numbers[3] >= numbers[1]) 27 | result1 ++; 28 | 29 | //part 2 30 | if(numbers[0] <= numbers[2] && numbers[2] <= numbers[1]) 31 | result2 ++; 32 | else if(numbers[2] <= numbers[0] && numbers[0] <= numbers[3]) 33 | result2 ++; 34 | } 35 | cout << "Result part1 = " << result1 << endl; 36 | cout << "Result part2 = " << result2 << endl; 37 | } -------------------------------------------------------------------------------- /Gonduls/d05/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d05/input.txt" 5 | #define STACK_N 9 6 | #define MAX_CRATES 50 7 | using namespace std; 8 | 9 | int indexMod(int index){ 10 | return index >= 0 ? index >= MAX_CRATES ? index - MAX_CRATES : index : index + MAX_CRATES; 11 | } 12 | 13 | int main(){ 14 | char letter; 15 | ifstream input(INPUT); 16 | int starts[STACK_N], ends[STACK_N]; 17 | char stacks[STACK_N][MAX_CRATES]; 18 | FILE * file = fopen(INPUT, "r"); 19 | 20 | // initialization 21 | for(int i = 0; i < STACK_N; i++){ 22 | starts[i] = 0; 23 | ends[i] = 0; 24 | } 25 | 26 | // input parse 27 | int letterNum = -1; 28 | while (fscanf(file, "%c", &letter)){ 29 | letterNum ++; 30 | if (letter == ' ' || letter == '[' || letter == ']') 31 | continue; 32 | if (letter == '\n'){ 33 | letterNum = -1; 34 | continue; 35 | } 36 | if (letterNum % 4 == 1){ 37 | int stack = (int) (letterNum - 1) / 4; 38 | if(letter <= 'Z' && letter >= 'A'){ 39 | stacks[stack][starts[stack]] = letter; 40 | starts[stack] = indexMod(starts[stack] - 1); 41 | } 42 | else if (letter == '0' + STACK_N) 43 | break; 44 | } 45 | } 46 | 47 | fscanf(file, "%c", &letter); 48 | fscanf(file, "%c", &letter); 49 | fscanf(file, "%c", &letter); 50 | 51 | int a, b, c; 52 | 53 | while(fscanf(file, "move %d from %d to %d\n", &a, &b, &c) != EOF){ 54 | b--; c--; 55 | for(int i = 0; i < a; i++){ 56 | letter = stacks[b][ends[b]]; 57 | ends[b] = indexMod(ends[b] - 1); 58 | ends[c] = indexMod(ends[c] + 1); 59 | stacks[c][ends[c]] = letter; 60 | } 61 | } 62 | cout << "result part 1: "; 63 | for(int i = 0; i< STACK_N; i++) 64 | cout << stacks[i][ends[i]]; 65 | cout << endl; 66 | } -------------------------------------------------------------------------------- /Gonduls/d05/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d05/input.txt" 5 | #define STACK_N 9 6 | #define MAX_CRATES 50 7 | using namespace std; 8 | 9 | int indexMod(int index){ 10 | return index >= 0 ? index >= MAX_CRATES ? index - MAX_CRATES : index : index + MAX_CRATES; 11 | } 12 | 13 | int main(){ 14 | char letter; 15 | ifstream input(INPUT); 16 | int starts[STACK_N], ends[STACK_N]; 17 | char stacks[STACK_N][MAX_CRATES]; 18 | FILE * file = fopen(INPUT, "r"); 19 | 20 | // initialization 21 | for(int i = 0; i < STACK_N; i++){ 22 | starts[i] = 0; 23 | ends[i] = 0; 24 | } 25 | 26 | // input parse 27 | int letterNum = -1; 28 | while (fscanf(file, "%c", &letter)){ 29 | letterNum ++; 30 | if (letter == ' ' || letter == '[' || letter == ']') 31 | continue; 32 | if (letter == '\n'){ 33 | letterNum = -1; 34 | continue; 35 | } 36 | if (letterNum % 4 == 1){ 37 | int stack = (int) (letterNum - 1) / 4; 38 | if(letter <= 'Z' && letter >= 'A'){ 39 | stacks[stack][starts[stack]] = letter; 40 | starts[stack] = indexMod(starts[stack] - 1); 41 | } 42 | else if (letter == '0' + STACK_N) 43 | break; 44 | } 45 | } 46 | 47 | fscanf(file, "%c", &letter); 48 | fscanf(file, "%c", &letter); 49 | fscanf(file, "%c", &letter); 50 | 51 | int a, b, c; 52 | 53 | while(fscanf(file, "move %d from %d to %d\n", &a, &b, &c) != EOF){ 54 | b--; c--; 55 | for(int i = 0; i < a; i++){ 56 | letter = stacks[b][indexMod(ends[b] - a +i + 1)]; 57 | stacks[c][indexMod(ends[c] + i + 1)] = letter; 58 | } 59 | ends[b] = indexMod(ends[b] - a); 60 | ends[c] = indexMod(ends[c] + a); 61 | 62 | } 63 | cout << "result part 2: "; 64 | for(int i = 0; i< STACK_N; i++) 65 | cout << stacks[i][ends[i]]; 66 | cout << endl; 67 | } -------------------------------------------------------------------------------- /Gonduls/d06/input.txt: -------------------------------------------------------------------------------- 1 | plhlsssjsrscspsffmrffwvfvrvvmbbnjnrnrfnndlnlznlznlnccdbbhvbvgvmvzmzvmzmbbrcclsslzslzzsztzftzzhggnjgnjnhnmnqnqqfdfnnrwnwsnwwvgvqqgpptvtrvvfmmzjzmmjssmwsmmhzzvvwzwcwrcrllpbppdgdvvwqvvsnsrnsncnwntnggwqgqhqrqprqrddjvddqsqhshchfffzddswdsshhcnhnqqfjffvlvwvssdqqwrwvrvhrvrzzgwggcjcgclcwchhzvzmzrrjwjqwwvbwwrmmvmpmzpzgpgsghgrrtmmlfmmnzzmpzzvmvjjsqqshqshsqqgtqggpvvrtttwbbhnbnlbnlnhnthtjhjshhrmrjjlclpprmrnrsrwrbwrwwjnwnbwbhbmbggdbgghllcvlvddzbdbdvdwvdvjvqvcvczvcvclvvrggrngrrwcwbcczgghnhznhnttbcbvcvttrrpbrppndppvvvgvtgvvhfvhhttppjmjllznlnldnldlwlnwlnlttgzzcfcggwmgmqgmmshmhqqdfqfpfqppprzrhrnrhnrrtsrrgpgnpngnqnmmrtrvtrrfccszszffvlfvlvssvdvvpggvcvscvvpmpgpqpfqqhttrhhsbhbqhhzggzrgrqrfqffwllggrgqqjzqzzsgglvgllsgllptltblbggvrggctczzllvsvcscrrzjrzzjnnbvvtntpntnvtntcntcnnwsnnnvjnnsccsddcvvgzgwggbnbwnwbwmwttzzsgzzjpzztwztwwhhzggplgplggwwwphpmmhchsccmwmttvjtjftffzbfffjljbbqvvstsggbbqrbqrbrhbbrmbmppvrpphptpggqgddtmdtmdmbbbcdbbgssmzzthhtjjrgjrgrzzchzhttgddjnjrjmjllrcrqqsvsjjjhvvphhgjhgjhjccwmcmjcmjmhmzznmmcbcscddcsssnppsnpnmnqmmtztftqtjtqqgvgvjjfnjffqbqbfqqwfqfcfsfttvccnssjvvpfvpvttvpvccwhwfwlwlclljqqststllgqgzqqfpflpplpssnntstqqpnnsrnsrrsvrsvsrvsrrtztmtptpcpssgnnfvflffscsqszzsppfnftfptfpfnfbbwzbzfbbtggrzrnntztnznhnnlbnbrbdddjhjchhdshhnzhnnsrnrrqwrrdsdlsstdsslqsqdqvqzvqvwqqlmmwzmwzwztzlzczhqfrclvgvnlchrggsrjhntctdbpfdcffwjngdvdrjmgvwvptlvlhvhshqphmrdznqbtchcvrwfrpvwhzmrwlcjwnrsgrqcbsgpwjthstvqzlwjjmqbvhhdfdsmqnmnswmwjtpgjhdpgcnvmlcjwjzrjhmrqmqnrqrmgdnbdgznwhgzncmcbzpntcdflvrbfdzwgpnqjqmrcqpbrzwhwdgtgshhmrwvhnrwslvcswjvdgglfrdvmqdspppwmvfzvdbvpcnhmvgfqwnvjvvzrvttwvbjrbjlllmwtlcltvqmwshnqsdtjrptvqjvdjgzwgzzhcdbwjzhdgsptfrtmmqvhsnsnpgwbncbnnvwmmrrjgfccbzcpcjmqvqsbvjrstzblsrngphwndfdswjnnnfdgpcbslvbjglqqnbbtjljsmdgcslmwlvgwpsqthlmmqfgpgmcvrpvvtzcjdrwcjgrbwthblwpwpbzvjvhzsphmfhfwvsthlfnhhfcmpsnmgrvrntlzpdvqwtrghnslnfhcjwrsvrngqqtwvcsfhbjwmsnmsmgvdhnzjgljtchbtwlfppvbtdclbdjdmwzcntvgfjlcwdplnjwqnzqhnfgcnbrgftqpdmqzrrhglbzzvjcdnbtfnvmsrbjdzhbqmhnsmprgvjzzgvllhqnzgpstqzcnlgsrcwzlhwqvcjlwnnjslmdtwqcpbrntrmrmmtscjwwtzjhghtqvvpldzvhtmspzlnlfgrfhmsndbdgpgvphwwgqhrtlwztgqqsrwnrnqphqfsrtbztqbrgmfbtpjwhhrglhbzmmjptppnfdzlpbqfcbdwzdtqbrvfmtdzdjlnzvqfnzmttpqgzgmrqwmdtmdzrttffpdlgwdhnlrhnnztphvrbzcrlvcpswlngcjhdzqwwwpzdmhhwpnzwgjsdsdbdvpfsrwmwvsggpcqbchwjpgljnbtpvjzbbvgsbsbjmtwtbjtzzsfvrmfvnmcngvvdsvljvjbrlfgstjrhtjplttzhjfbmphvbqdsmwfwspqpcvmgzgjnqlphshlgdwcvtmpwcfgdcbqwpnshfgrfjvlrtqbffwwtbnwtvjlsdgwgfmhlrsmfrjzcwccdzfwwdwhwdsjbllhjsqmnqvngvdmbvbssfjtjfbtngrdwgldcrtpmvrbrmpvwwsfrlnbqsqzfnftpbhslqccnbqwgbdbfpblpmwbgjzmnptnhdjzqjhdrhqbtfhsrdqwlmwzqlsmmslgfzpvtgtsrlszvqrhrzclbqhzzwwfmhrrvsrnsjbdjsqqlsmgdmgbdtmvfjmsbwjqmqrvrhqbchpqrwvlvwpvhjlbdzfjvrwmchccrsrfvhzmpfjqwnrbvqmgwjcbndjdtfgnrzrqwgzhrdvghdrvgtplcrthgchmvwtdrchfwpdszzzhqpmrlzvfdnfnlwghmmwvsscbrdbchhgttsnbzdbqgddqfvcgvqwltnqtcwrmhtftnlwvlglsvvctccnntznsjnmmgqlzmplsdspnjtmzlbvrfzfclnhgjzmvntdwhspqwtpgndspnjqjqwrpwhjhpvjwfptpndnwvcjdwsvdtcrtwpsprgmrspgmcsdgtjbbgsgvcjhrcldlvgvqwqwthplzgwbzmszjfrlnznvgphnqzcwsztvvljlrlzrndbzccstprhntnlmshhclnrsmsvvvsbmpfdjsmspwcqtmlrrdmfzjjjhmsmdfqddzpgtbzbsnhhhpsfrdrdvpvpnjmvnhdrzrqggrpqdcmctvqfrtfmjjqjwgzzbrdfplhzjbnqlmjlcgvmsbpgdlfjmbgqtrvzzdgtlmbqthjrdtlstqtzqvfjvmmstsmtsbnjvstjjvrrjqcbjvhfpslpvjmdznrcnsvlpbpzmslqtpczmvhdwzrhwbwtfvrrmbszvrhwsjrclcscgngwvblbbrqprgshwzhlqgwmpfsmqsvpjbdccdmtnnhqfwvlgjlszmmmdmtmpzwhplzsjztrnwngbvspqqbmghwzgvfjdrblfmtwcvnczsrflmsjmrsvzldmttjwcmnvwcbjfvznhgntnqfbfchcqqshhjldgltqhdlqldlpfjnjvtbvbntzdjzstcqbzdnmsvcdgvjmmvtdfvdplqfndqqlzlspmjgdfbgsvwzqzsvvbbldltbtzwzpqrzfmfzgdbpqnwtrfcgwmlbpzrgscbjvfdwnjzjdfzltsbppnljzrgggplmttpmgwnhdlwfhwzsrcflnrqqzwsbqllwjqlrgwbhvcvqdvjvpbzgnfbbbtccvplzggplbrsbldllwmttwtvltsfljfbbprtvlfshhwdhdgvzfzjttvnphpnjnzsbvfwflfpqwnhvwjdsrtbwsjzqhgfldnssfbbzzqqrwtjvwjschmndgqzjtpsbfhwtmqbtfstrbghgtnldjqtshdnrwzvwddchhvdbsfjnqzfjdpvhvwwjspftgbtgwzdfgzzhpvjpdlrrdfnpftshthwzjzmzdghnfdqcbmjhfdgzrcgrzbrjtmhwbjhcpgjdsmnqzncdlwhqqzqblgdbbdsmrqgbdbmdczvvpnbbjdwrlmrwgnnbbzpcnsjgcmshgzwnjzwjlrdmvmhvjrzphgpczppqvwjthcdphprhhrggjdpzmgtpjjfvpczzrvsssfrrptnzlstrhmbhvzmwjnddshrrtgspbllvqlsptrtvtldsgnjjbwtfmtbjdvmgbptjlzhpttjmvpgnjphswhtdq -------------------------------------------------------------------------------- /Gonduls/d06/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d06/input.txt" 5 | using namespace std; 6 | 7 | 8 | int main(){ 9 | ifstream file(INPUT); 10 | string str; 11 | int result = 4; 12 | getline(file, str); 13 | for (int j = 0; j < size(str); j++){ 14 | if (str[j] == str[j+1] || str[j] == str[j+2] || str[j] == str[j+3] || str[j+1] == str[j+2] || str[j+1] == str[j+3] || str[j+2] == str[j+3]){ 15 | result ++; 16 | } else break; 17 | } 18 | cout << "Result part1 = " << result << endl; 19 | } -------------------------------------------------------------------------------- /Gonduls/d06/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define INPUT "./Gonduls/d06/input.txt" 5 | using namespace std; 6 | 7 | bool check(int* array){ 8 | for(int i = 0; i < 26; i++){ 9 | if (*(array + i) > 1) 10 | return false; 11 | } 12 | return true; 13 | } 14 | 15 | int main(){ 16 | ifstream file(INPUT); 17 | string str; 18 | int result = 15; 19 | getline(file, str); 20 | int all14[26]; 21 | for (int i = 0; i<26; i++){ 22 | all14[i] = 0; 23 | } 24 | for (int i = 0; i<14; i++){ 25 | all14[str[i] - 'a'] ++; 26 | } 27 | 28 | for(int i = 14; i< size(str); i++){ 29 | all14[str[i - 14] - 'a'] --; 30 | all14[str[i] - 'a'] ++; 31 | if(check(all14)) 32 | break; 33 | result ++; 34 | } 35 | cout << "Result part2 = " << result << endl; 36 | } -------------------------------------------------------------------------------- /Gonduls/d07/results.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define INPUT "./Gonduls/d07/input.txt" 7 | using namespace std; 8 | 9 | int result1 = 0; 10 | int result2; 11 | int threshold = 0; 12 | 13 | class Dir{ 14 | list subDirs; 15 | int filesSize; 16 | Dir* father; 17 | bool added = false; 18 | 19 | public: 20 | string name; 21 | Dir(string name, Dir* father){ 22 | this -> father = father; 23 | this -> name = name; 24 | subDirs = {}; 25 | filesSize = 0; 26 | } 27 | 28 | void addFile(int size){ 29 | added = true; 30 | this -> filesSize += size; 31 | } 32 | 33 | void addDir(string name){ 34 | added = true; 35 | subDirs.push_back(Dir(name, this)); 36 | } 37 | 38 | Dir * getDir(string name){ 39 | list::iterator it = subDirs.begin(); 40 | while(true){ 41 | advance(it, 1); 42 | if (it -> name.compare(name) == 0){ 43 | return &(*it); 44 | } 45 | } 46 | throw invalid_argument("No such dir present"); 47 | } 48 | 49 | Dir * getFather(){ 50 | return father; 51 | } 52 | 53 | bool alreadyAdded(){ 54 | return added; 55 | } 56 | 57 | list getSubDirs(){ 58 | return subDirs; 59 | } 60 | 61 | int getSize(){ 62 | return filesSize; 63 | } 64 | 65 | }; 66 | 67 | int traverse(Dir * root){ 68 | int result = root -> getSize(); 69 | for (Dir d : root -> getSubDirs()) 70 | result += traverse(&d); 71 | 72 | if(result < 100000) 73 | result1 += result; 74 | 75 | if(result > threshold && result < result2) 76 | result2 = result; 77 | 78 | return result; 79 | } 80 | 81 | int main(){ 82 | ifstream file(INPUT); 83 | string line; 84 | Dir root = Dir("/", NULL); 85 | Dir* current = &root; 86 | bool alreadyAdded = false; 87 | getline(file, line); 88 | 89 | // populate 90 | while(getline(file, line)){ 91 | if(line.find("$ cd ", 0) == 0){ 92 | string end = line.substr(5); 93 | 94 | if(end.compare("..") == 0){ 95 | current = current -> getFather(); 96 | continue; 97 | } 98 | current = current -> getDir(end); 99 | continue; 100 | } 101 | 102 | if(line.find("$ ls", 0) == 0){ 103 | alreadyAdded = current -> alreadyAdded(); 104 | continue; 105 | } 106 | 107 | if(alreadyAdded){ 108 | continue; 109 | } 110 | 111 | if(line.find("dir ", 0) == 0){ 112 | current -> addDir(line.substr(4)); 113 | } else { 114 | stringstream ss; 115 | string integer; 116 | ss << line; 117 | ss >> integer; 118 | current -> addFile(stoi(integer)); 119 | } 120 | } 121 | 122 | int total = traverse(&root); 123 | cout << "Result part 1= " << result1 << endl; 124 | threshold = 30000000 - (70000000 - total); 125 | result2 = total; 126 | traverse(&root); 127 | cout << "Result part 2= " << result2 << endl; 128 | } -------------------------------------------------------------------------------- /Gonduls/d08/results.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define INPUT "./Gonduls/d08/input.txt" 6 | using namespace std; 7 | 8 | int calculateScenic(int size, int x, int y, const string* strings); 9 | 10 | int main(){ 11 | ifstream file(INPUT); 12 | string line; 13 | 14 | getline(file, line); 15 | int size = line.size(); 16 | string strings[size]; 17 | bool matrix[size][size]; 18 | 19 | for(int j = 0; j < size; j++) 20 | matrix[0][j] = false; 21 | 22 | strings[0] = line; 23 | int i = 1; 24 | while (getline(file, line)){ 25 | strings[i] = line; 26 | 27 | for(int j = 0; j < size; j++) 28 | matrix[i][j] = false; 29 | 30 | i++; 31 | } 32 | 33 | for(int direction = 0; direction < 2; direction ++){ 34 | 35 | for(i = 0; i < size; i++){ 36 | char directMax = '0' -1, reverseMax = '0' -1; 37 | for(int j = 0; j < size; j++){ 38 | int dx = j, rx = size - 1 -j; 39 | int dy = i, ry = size - 1 -i; 40 | 41 | if(direction){ 42 | swap(dx, dy); 43 | swap(rx, ry); 44 | } 45 | 46 | if(strings[dy][dx] > directMax){ 47 | matrix[dy][dx] = true; 48 | directMax = strings[dy][dx]; 49 | } 50 | 51 | if(strings[ry][rx] > reverseMax){ 52 | matrix[ry][rx] = true; 53 | reverseMax = strings[ry][rx]; 54 | } 55 | 56 | if(directMax == '9' && reverseMax == '9') 57 | break; 58 | } 59 | } 60 | } 61 | 62 | int result1 = 0; 63 | int result2 = 0; 64 | for(i = 0; i < size; i++) 65 | for(int j = 0; j < size; j++){ 66 | if(matrix[i][j]) 67 | result1 ++; 68 | 69 | int scenic = calculateScenic(size, i, j, strings); 70 | result2 = scenic > result2? scenic : result2; 71 | } 72 | 73 | cout << "Result part 1 = " << result1 << endl; 74 | cout << "Result part 2 = " << result2 << endl; 75 | 76 | 77 | } 78 | 79 | int calculateScenic(int size, int x, int y, const string* strings){ 80 | int result = 1; 81 | int partial = 0; 82 | int i; 83 | char letter = strings[y][x]; 84 | partial = 0; 85 | for(i = x + 1; i < size; i++){ 86 | partial ++; 87 | if(strings[y][i] >= letter){ 88 | break; 89 | } 90 | } 91 | result *= partial; 92 | partial = 0; 93 | for(i = x -1; i >= 0; i--){ 94 | partial ++; 95 | if(strings[y][i] >= letter){ 96 | break; 97 | } 98 | } 99 | result *= partial; 100 | partial = 0; 101 | for(i = y + 1; i < size; i++){ 102 | partial ++; 103 | if(strings[i][x] >= letter){ 104 | break; 105 | } 106 | } 107 | result *= partial; 108 | partial = 0; 109 | for(i = y - 1; i >= 0; i--){ 110 | partial ++; 111 | if(strings[i][x] >= letter){ 112 | break; 113 | } 114 | } 115 | result *= partial; 116 | return result; 117 | } -------------------------------------------------------------------------------- /Gonduls/d09/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define INPUT "./Gonduls/d09/input.txt" 7 | using namespace std; 8 | 9 | void adjust(int *H, int *T); 10 | 11 | int main(){ 12 | ifstream file(INPUT); 13 | char direction; 14 | int steps; 15 | int H[2], T[2]; 16 | H[0] = 0; H[1] = 0; T[0] = 0; T[1] = 0; 17 | 18 | set positions; 19 | 20 | while(file >> direction >> steps){ 21 | 22 | int x = 0, y = 0; 23 | switch (direction){ 24 | case 'U': 25 | y++; 26 | break; 27 | case 'D': 28 | y--; 29 | break; 30 | case 'L': 31 | x--; 32 | break; 33 | case 'R': 34 | x++; 35 | break; 36 | } 37 | 38 | for(int i = 0; i < steps; i++){ 39 | H[0] += x; 40 | H[1] += y; 41 | 42 | adjust(H, T); 43 | 44 | // 10000 * x + y is enough of a hash function for this problem 45 | positions.insert(10000*T[0] + T[1]); 46 | } 47 | } 48 | cout << "Result part 1: " << positions.size() << endl; 49 | } 50 | 51 | void adjust(int *H, int *T){ 52 | // x and y = how many steps away from H is T in a direction 53 | int x = T[0] - H[0]; 54 | int y = T[1] - H[1]; 55 | 56 | // missing edge case where H is diagonally 2 steps away from T 57 | 58 | // if T is horizontally two steps away from H: 59 | // T ends vertically alligned with H 60 | if (x > 1 || x < -1){ 61 | y = 0; 62 | } 63 | // like before, swap horizontally with vertically 64 | if (y > 1 || y < -1){ 65 | x = 0; 66 | } 67 | 68 | // at the end only values -1, 0, 1 allowed for x and y 69 | x = (x > 1 || x < -1) ? x/2 : x; 70 | y = (y > 1 || y < -1) ? y/2 : y; 71 | 72 | T[0] = H[0] + x; 73 | T[1] = H[1] + y; 74 | } -------------------------------------------------------------------------------- /Gonduls/d09/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define INPUT "./Gonduls/d09/input.txt" 7 | using namespace std; 8 | 9 | void adjust(int *H, int *T); 10 | 11 | int main(){ 12 | ifstream file(INPUT); 13 | char direction; 14 | int steps; 15 | int rope[10][2]; 16 | 17 | for(int i = 0; i < 10; i++){ 18 | rope[i][0] = 0; 19 | rope[i][1] = 0; 20 | } 21 | 22 | set positions; 23 | 24 | while(file >> direction >> steps){ 25 | 26 | int x = 0, y = 0; 27 | switch (direction){ 28 | case 'U': 29 | y++; 30 | break; 31 | case 'D': 32 | y--; 33 | break; 34 | case 'L': 35 | x--; 36 | break; 37 | case 'R': 38 | x++; 39 | break; 40 | } 41 | 42 | for(int i = 0; i < steps; i++){ 43 | rope[0][0] += x; 44 | rope[0][1] += y; 45 | 46 | for(int j = 1; j < 10; j++) 47 | adjust(rope[j-1], rope[j]); 48 | 49 | // 10000 * x + y is enough of a hash function for this problem 50 | positions.insert(10000*rope[9][0] + rope[9][1]); 51 | } 52 | } 53 | cout << "Result part 2: " << positions.size() << endl; 54 | } 55 | 56 | void adjust(int *H, int *T){ 57 | // x and y = how many steps away from H is T in a direction 58 | int x = T[0] - H[0]; // horizontally 59 | int y = T[1] - H[1]; // vertically 60 | 61 | if((x > 1 || x < -1) && (y > 1 || y < -1)){ 62 | // edge case not present in part 1: 63 | // if a knot is diagonally 2 steps away: 64 | // just move one step diagonally and end up diagonally adjacent. 65 | // taken care of by istructions after ifs 66 | } 67 | // if T is horizontally two steps away from H, but not also vertically: 68 | // T ends vertically alligned with H 69 | else if (x > 1 || x < -1){ 70 | y = 0; 71 | } 72 | // like before, swap horizontally with vertically 73 | else if (y > 1 || y < -1){ 74 | x = 0; 75 | } 76 | 77 | // at the end only values -1, 0, 1 allowed for x and y 78 | x = (x > 1 || x < -1) ? x/2 : x; 79 | y = (y > 1 || y < -1) ? y/2 : y; 80 | 81 | T[0] = H[0] + x; 82 | T[1] = H[1] + y; 83 | } -------------------------------------------------------------------------------- /Gonduls/d10/input.txt: -------------------------------------------------------------------------------- 1 | addx 2 2 | addx 4 3 | noop 4 | noop 5 | addx 17 6 | noop 7 | addx -11 8 | addx -1 9 | addx 4 10 | noop 11 | noop 12 | addx 6 13 | noop 14 | noop 15 | addx -14 16 | addx 19 17 | noop 18 | addx 4 19 | noop 20 | noop 21 | addx 1 22 | addx 4 23 | addx -20 24 | addx 21 25 | addx -38 26 | noop 27 | addx 7 28 | noop 29 | addx 3 30 | noop 31 | addx 22 32 | noop 33 | addx -17 34 | addx 2 35 | addx 3 36 | noop 37 | addx 2 38 | addx 3 39 | noop 40 | addx 2 41 | addx -8 42 | addx 9 43 | addx 2 44 | noop 45 | noop 46 | addx 7 47 | addx 2 48 | addx -27 49 | addx -10 50 | noop 51 | addx 37 52 | addx -34 53 | addx 30 54 | addx -29 55 | addx 9 56 | noop 57 | addx 2 58 | noop 59 | noop 60 | noop 61 | addx 5 62 | addx -4 63 | addx 9 64 | addx -2 65 | addx 7 66 | noop 67 | noop 68 | addx 1 69 | addx 4 70 | addx -1 71 | noop 72 | addx -19 73 | addx -17 74 | noop 75 | addx 1 76 | addx 4 77 | addx 3 78 | addx 11 79 | addx 17 80 | addx -23 81 | addx 2 82 | noop 83 | addx 3 84 | addx 2 85 | addx 3 86 | addx 4 87 | addx -22 88 | noop 89 | addx 27 90 | addx -32 91 | addx 14 92 | addx 21 93 | addx 2 94 | noop 95 | addx -37 96 | noop 97 | addx 31 98 | addx -26 99 | addx 5 100 | addx 2 101 | addx 3 102 | addx -2 103 | addx 2 104 | addx 5 105 | addx 2 106 | addx 3 107 | noop 108 | addx 2 109 | addx 9 110 | addx -8 111 | addx 2 112 | addx 11 113 | addx -4 114 | addx 2 115 | addx -15 116 | addx -22 117 | addx 1 118 | addx 5 119 | noop 120 | noop 121 | noop 122 | noop 123 | noop 124 | addx 4 125 | addx 19 126 | addx -15 127 | addx 1 128 | noop 129 | noop 130 | addx 6 131 | noop 132 | noop 133 | addx 5 134 | addx -1 135 | addx 5 136 | addx -14 137 | addx -13 138 | addx 30 139 | noop 140 | addx 3 141 | noop 142 | noop -------------------------------------------------------------------------------- /Gonduls/d10/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define INPUT "./Gonduls/d10/input.txt" 7 | using namespace std; 8 | 9 | bool checkCycle(int cycle){ 10 | return ((cycle - 20)%40 == 0); 11 | } 12 | 13 | int main(){ 14 | ifstream file(INPUT); 15 | int reg = 1, result = 0, cycle = 0; 16 | bool check; 17 | string word; 18 | 19 | while(file >> word){ 20 | check = false; 21 | 22 | if(word == "addx"){ 23 | cycle += 2; 24 | check = checkCycle((cycle / 2) * 2); 25 | } 26 | else if(word == "noop"){ 27 | cycle += 1; 28 | check = checkCycle(cycle); 29 | } 30 | else 31 | reg += stoi(word); 32 | 33 | 34 | if(check) 35 | result += reg * (cycle / 2) * 2; 36 | } 37 | cout << "Result part 1: " << result << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Gonduls/d10/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define INPUT "./Gonduls/d10/input.txt" 7 | using namespace std; 8 | 9 | bool checkPosition(int cycle, int x){ 10 | return (cycle == x || cycle == x-1 || cycle == x+1); 11 | } 12 | 13 | int main(){ 14 | ifstream file(INPUT); 15 | int reg = 1, cycle = 0, row, col; 16 | string word; 17 | char matrix[6][40]; 18 | 19 | while(file >> word){ 20 | 21 | row = cycle / 40; 22 | col = cycle % 40; 23 | 24 | if(word == "addx"){ 25 | matrix[row][col] = checkPosition(cycle % 40, reg) ? '#' : ' '; 26 | cycle ++; 27 | row = cycle / 40; 28 | col = cycle % 40; 29 | matrix[row][col] = checkPosition(cycle % 40, reg) ? '#' : ' '; 30 | cycle ++; 31 | } 32 | else if(word == "noop"){ 33 | matrix[row][col] = checkPosition(cycle % 40, reg) ? '#' : ' '; 34 | cycle ++; 35 | } 36 | else 37 | reg += stoi(word); 38 | } 39 | 40 | for(int i = 0; i < 6; i++){ 41 | for(int j = 0; j < 40; j++){ 42 | cout << matrix[i][j]; 43 | } 44 | cout << endl; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Gonduls/d11/input.txt: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 65, 58, 93, 57, 66 3 | Operation: new = old * 7 4 | Test: divisible by 19 5 | If true: throw to monkey 6 6 | If false: throw to monkey 4 7 | 8 | Monkey 1: 9 | Starting items: 76, 97, 58, 72, 57, 92, 82 10 | Operation: new = old + 4 11 | Test: divisible by 3 12 | If true: throw to monkey 7 13 | If false: throw to monkey 5 14 | 15 | Monkey 2: 16 | Starting items: 90, 89, 96 17 | Operation: new = old * 5 18 | Test: divisible by 13 19 | If true: throw to monkey 5 20 | If false: throw to monkey 1 21 | 22 | Monkey 3: 23 | Starting items: 72, 63, 72, 99 24 | Operation: new = old * old 25 | Test: divisible by 17 26 | If true: throw to monkey 0 27 | If false: throw to monkey 4 28 | 29 | Monkey 4: 30 | Starting items: 65 31 | Operation: new = old + 1 32 | Test: divisible by 2 33 | If true: throw to monkey 6 34 | If false: throw to monkey 2 35 | 36 | Monkey 5: 37 | Starting items: 97, 71 38 | Operation: new = old + 8 39 | Test: divisible by 11 40 | If true: throw to monkey 7 41 | If false: throw to monkey 3 42 | 43 | Monkey 6: 44 | Starting items: 83, 68, 88, 55, 87, 67 45 | Operation: new = old + 2 46 | Test: divisible by 5 47 | If true: throw to monkey 2 48 | If false: throw to monkey 1 49 | 50 | Monkey 7: 51 | Starting items: 64, 81, 50, 96, 82, 53, 62, 92 52 | Operation: new = old + 5 53 | Test: divisible by 7 54 | If true: throw to monkey 3 55 | If false: throw to monkey 0 -------------------------------------------------------------------------------- /Gonduls/d11/part2.py: -------------------------------------------------------------------------------- 1 | monkeys = [[79, 98], [54, 65, 75, 74], [79, 60, 97], [74]] 2 | operations = '*+^+' 3 | arguments = [19, 6, -1, 3] 4 | tests = [23, 19, 13, 17] 5 | true_next = [2, 2, 1, 0] 6 | false_next = [3, 0, 3, 1] 7 | 8 | 9 | divisor = 23 * 19 * 13 * 17 10 | print(divisor) 11 | inspections = [0, 0, 0, 0] 12 | 13 | N_ROUNDS = 10000 14 | 15 | f = open('output.txt', 'w') 16 | 17 | for _ in range(N_ROUNDS): 18 | for m_index, el in enumerate(monkeys): 19 | for wl in el: 20 | inspections[m_index] += 1 21 | oper = operations[m_index] 22 | 23 | if oper == '*': 24 | wl *= arguments[m_index] 25 | elif oper == '+': 26 | wl += arguments[m_index] 27 | else: 28 | wl = wl** 2 29 | print(wl%divisor, file=f) 30 | 31 | wl %= divisor 32 | 33 | if wl % tests[m_index] == 0: 34 | monkeys[true_next[m_index]].append(wl) 35 | else: 36 | monkeys[false_next[m_index]].append(wl) 37 | monkeys[m_index] = [] 38 | 39 | inspections.sort(reverse=True) 40 | print(inspections) 41 | print(inspections[0] * inspections[1]) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Tommimon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MatteoBlack/d01/day01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def execute1(input: list) -> int: 5 | res1 = 0 6 | tmp1 = 0 7 | for el in input: 8 | if el == '': 9 | if res1 < tmp1: 10 | res1 = tmp1 11 | tmp1 = 0 12 | else: 13 | tmp1 += int(el) 14 | return res1 15 | 16 | 17 | def execute2(input: list) -> int: 18 | tmp = 0 19 | top3 = np.array([0, 0, 0]) 20 | for el in input: 21 | if el == '': 22 | if top3[0] < tmp: 23 | top3[0] = tmp 24 | top3.sort() 25 | tmp = 0 26 | else: 27 | tmp += int(el) 28 | return np.sum(top3) 29 | 30 | 31 | with open('input.txt') as f: 32 | lines = f.readlines() 33 | cleanLines = [] 34 | for line in lines: 35 | cleanLines.append(line.strip()) 36 | cleanLines.append('') 37 | 38 | print(f'DAY01_1 RESULT: {execute1(cleanLines)}') 39 | print(f'DAY01_2 RESULT: {execute2(cleanLines)}') -------------------------------------------------------------------------------- /MatteoBlack/d02/day02.py: -------------------------------------------------------------------------------- 1 | # ROCK PAPER SCISSOR 2 | def execute1(input: list) -> int: 3 | result = 0 4 | for el in input: 5 | op, own = el.split(' ') 6 | if own == 'X': 7 | scoreInLine = 1 8 | elif own == 'Y': 9 | scoreInLine = 2 10 | elif own == 'Z': 11 | scoreInLine = 3 12 | if (own == 'X' and op == 'A') or (own == 'Y' and op == 'B') or (own == 'Z' and op == 'C'): 13 | scoreInLine += 3 14 | elif (own == 'X' and op == 'C') or (own == 'Y' and op == 'A') or (own == 'Z' and op == 'B'): 15 | scoreInLine += 6 16 | result += scoreInLine 17 | return result 18 | 19 | 20 | def execute2(input: list) -> int: 21 | result = 0 22 | for el in input: 23 | op, own = el.split(' ') 24 | if own == 'X': 25 | scoreInLine = 0 26 | elif own == 'Y': 27 | scoreInLine = 3 28 | elif own == 'Z': 29 | scoreInLine = 6 30 | if (own == 'X' and op == 'B') or (own == 'Y' and op == 'A') or (own == 'Z' and op == 'C'): 31 | scoreInLine += 1 32 | elif (own == 'X' and op == 'C') or (own == 'Y' and op == 'B') or (own == 'Z' and op == 'A'): 33 | scoreInLine += 2 34 | elif (own == 'X' and op == 'A') or (own == 'Y' and op == 'C') or (own == 'Z' and op == 'B'): 35 | scoreInLine += 3 36 | result += scoreInLine 37 | return result 38 | 39 | 40 | with open('input.txt') as f: 41 | lines = f.readlines() 42 | cleanLines = [] 43 | for line in lines: 44 | cleanLines.append(line.strip()) 45 | 46 | print(f'DAY02_1 RESULT: {execute1(cleanLines)}') 47 | print(f'DAY02_2 RESULT: {execute2(cleanLines)}') 48 | -------------------------------------------------------------------------------- /MatteoBlack/d03/day03.py: -------------------------------------------------------------------------------- 1 | def execute1(input: list) -> int: 2 | result = 0 3 | for i, el in enumerate(input): 4 | size = len(el) 5 | first = el[:size // 2] 6 | second = el[size // 2:] 7 | for c in first: 8 | if c in second: 9 | result += ((ord(c) - ord('a') + 1) if c.islower() else (ord(c) - ord('A') + 27)) 10 | break 11 | return result 12 | 13 | 14 | def execute2(input: list) -> int: 15 | result = 0 16 | for i in range(0, len(input), 3): 17 | if i == len(input) - 2: 18 | break 19 | first = input[i] 20 | second = input[i + 1] 21 | third = input[i + 2] 22 | for c in first: 23 | if c in second and c in third: 24 | result += ((ord(c) - ord('a') + 1) if c.islower() else (ord(c) - ord('A') + 27)) 25 | break 26 | return result 27 | 28 | 29 | with open('input.txt') as f: 30 | lines = f.readlines() 31 | cleanLines = [] 32 | for line in lines: 33 | cleanLines.append(line.strip()) 34 | 35 | print(f'DAY03_1 RESULT: {execute1(cleanLines)}') 36 | print(f'DAY03_2 RESULT: {execute2(cleanLines)}') 37 | -------------------------------------------------------------------------------- /MatteoBlack/d04/day04.py: -------------------------------------------------------------------------------- 1 | def execute1(input: list) -> int: 2 | result = 0 3 | for el in input: 4 | first, second = el.split(',') 5 | first_st, first_en = first.split('-') 6 | second_st, second_en = second.split('-') 7 | if (int(first_st) >= int(second_st) and int(first_en) <= int(second_en)) or ( 8 | int(first_st) <= int(second_st) and int(first_en) >= int(second_en)): 9 | result += 1 10 | return result 11 | 12 | 13 | def execute2(input: list) -> int: 14 | result = 0 15 | for el in input: 16 | first, second = el.split(',') 17 | first_st, first_en = first.split('-') 18 | second_st, second_en = second.split('-') 19 | if (int(first_en) <= int(second_st) and int(first_st) >= int(second_en)) or ( 20 | int(first_en) >= int(second_st) and int(first_st) <= int(second_en)): 21 | result += 1 22 | return result 23 | 24 | 25 | with open('input.txt') as f: 26 | lines = f.readlines() 27 | cleanLines = [] 28 | for line in lines: 29 | cleanLines.append(line.strip()) 30 | 31 | print(f'DAY04_1 RESULT: {execute1(cleanLines)}') 32 | print(f'DAY04_2 RESULT: {execute2(cleanLines)}') -------------------------------------------------------------------------------- /MatteoBlack/d05/day05.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import re 3 | 4 | 5 | def execute1(state_1: list, input: list) -> str: 6 | result = '' 7 | for el in input: 8 | qt, fr, to = (re.sub('move\\s|from\\s|to\\s', '', el)).split(' ') 9 | 10 | for _ in range(int(qt)): 11 | if len(state_1[int(fr) - 1]) > 0: 12 | state_1[int(to) - 1].insert(0, state_1[int(fr) - 1].pop(0)) 13 | for crate in state_1: 14 | if len(crate) > 0: 15 | result += crate.pop(0) 16 | return result 17 | 18 | 19 | def execute2(state_2, input: list) -> str: 20 | result = '' 21 | for el in input: 22 | qt, fr, to = (re.sub('move\\s|from\\s|to\\s', '', el)).split(' ') 23 | multCarry = [] 24 | for _ in range(int(qt)): 25 | if len(state_2[int(fr) - 1]) > 0: 26 | multCarry.append(state_2[int(fr) - 1].pop(0)) 27 | for i in range(len(multCarry)): 28 | state_2[int(to) - 1].insert(i, multCarry[i]) 29 | for crate in state_2: 30 | if len(crate) > 0: 31 | result += crate.pop(0) 32 | return result 33 | 34 | 35 | with open('input.txt') as f: 36 | lines = f.readlines() 37 | moves = [] 38 | tmpState = [] 39 | mov = False 40 | for line in lines: 41 | if line.strip() == '': 42 | mov = True 43 | elif mov: 44 | moves.append(line.strip()) 45 | else: 46 | tmpState.append(line[:-1]) 47 | 48 | state = [[] for _ in range(int(tmpState[-1][-1]))] 49 | for i, s in enumerate(tmpState): 50 | for j in range(1, len(s), 4): 51 | focus = ([*s])[j] 52 | if focus != ' ' and (ord('A') <= ord(focus) <= ord('Z')): 53 | state[j // 4].append(focus) 54 | 55 | print(f'DAY05_1 RESULT: {execute1(copy.deepcopy(state), moves)}') 56 | print(f'DAY05_2 RESULT: {execute2(copy.deepcopy(state), moves)}') 57 | -------------------------------------------------------------------------------- /MatteoBlack/d06/day06.py: -------------------------------------------------------------------------------- 1 | def execute(input: list, size=4) -> int: 2 | message = [*input[0]] 3 | for i in range(size - 1, len(message)): 4 | setChar = set([message[i - j] for j in range(size)]) 5 | if len(setChar) == size: 6 | return i + 1 7 | return -1 8 | 9 | 10 | with open('input.txt') as f: 11 | lines = f.readlines() 12 | cleanLines = [] 13 | for line in lines: 14 | cleanLines.append(line.strip()) 15 | 16 | print(f'DAY06_1 RESULT: {execute(cleanLines)}') 17 | print(f'DAY06_2 RESULT: {execute(cleanLines, 14)}') 18 | -------------------------------------------------------------------------------- /MatteoBlack/d06/input.txt: -------------------------------------------------------------------------------- 1 | vmsvstvtzvzqvzvssvddsgdddrqrsqqwnnffrwrswwztwzzbssgtstvsshrsrrlvrvzrrsqsvqsqbssqrsrjrsrddqnnfqnnrfnrnffjsjnnqddjjbvvbzzshhsffmtftrrpzznlltclltmltmllzclcwwgzzzqvqhqllcrllqdqpqnqbqggcrcsclssrfsssqhhfzzprrjvrjvvblvbllwccztzggspgggqrqcqjcqjcqcmmffmtmltlwwcpplpqqwhwvhvnnqhqsqlqvvgfgqqddlhhstthzhgzgbzbdzbzlzwlzlpljlgjjqlltrrbmrrvvcttsddfjddmbdbffcrfftvvzppncpcvcdvcvvjjjlttcgtcctrrrgmrgmgwwsgscsffswfwqwnnzmzffpwptpntppbddggsbsddmjmssnzsnstsztttjztjjpddwlwrrlnnzsnnfllqrrqmqhhfddtmmpfmfcfgfllngnvnjjmffwbffsqsmsdstdddjssmlssfpprrgzzzwvvftffczfftccwrrdsrsjrssfnsnttqptqtnqtnqtttjqtjqjzjpzpvzvbzvvjqqvcqcwwgwllbwbjjhsjhhchffgdfgffvddrndrdwdwlwqqfqzzntzzhwzzndnjnsjjmvvlqqqqshhmzzjvvcscqqjjhphnppcprrhnnbsspcscbssnsmmznzsnnszsrzrwrswsgwglwlzznpnwpnnjlnlrnntllhvvdrvvprpptqqlzqzrrgfgfhfvhhcscjjfmmjpmjjvwvtvhvqhqvvqvwvpvsschhlmlrlmljjqnqggbwbmwbwgbwwgllqzllrjlrjjfjzffthhpvvsddvjvvzsvvhghlhqqbnqqlnngsngnccnpccmttmrtmmlmccqbbjssbsqbqddnqqdvdvbvgbvvjfjhjzzfhfzffgccdjcjtjpttcrrffvddwgglrgllbttcqtctqtvtwtggrppzvvbzvzgzsznnsqnsqqnwwzrwwnwbwnnhznhzhchwwvwhwlwlnnmwmfmfrmmnvndnffjgjmgmfmhmtmstthjhbjhjddrqrllnflnnwzwjjthjhwhmmflmlfmfddqgdqgqqwbbdsstssrvvbpbgppfwfvvglvvzgvgmvgvzvdvcctddjqddffrprbrggpgpghhwzhzggqrqsrstrssnttrgrsswsrstsdtdzttzvznzjnzzqssjwswcswsgghqghqgqvggtsswwmbbfrrdcctjtjsttrccqbbtcctcscqsssvmmdhmdmvdmvmsvsrvsrvsszvvrpvrpprvppctclcrrchhtmmbddswddglgrrdcdppcbpbgpbggbssnrnbrbllqwqbwbmmcwmmrjrdjjpsjppblbjbsbsgswsmwmgwgmmqpplzlssvcscnngwgvvffvlvtvtddgqgttjljggfzggmfgfqqtrrdbdbbnjnnbjbwwjjqljlnjjvzvhvlhhhsfhsfscsjcjsjbsjjdbdbpbqqsggcnczcchrhjhphlppnwwhthghhfvfrrbzrbrrcsszfmhrjflswthfrlmjbnhblmsldjpnrdgfbcrftnbcltlctbdthhjzsqvbmppcbhctqbtfhdrtrbzjpnzzvtfjtpqgbtmdcnjgtblhmvrpbrcpqfhpzqvbfqhsrjqwqqnggrlgvwndqrhvpzhswnglngwjdgwcnngrvhtcsblpdshqcwfpzcpmrbzqjfpllbbvlcfjtdqchjsgsqgqzsbfnnqcsvpwfmzdhzmcjfjnczldclvpfpntppqbhfqzqdtfwsvdzhphwpzhqqfflswhtldlrmrqpfdlwwszphrwjqvhpqmrzvblvtvsfpgzdhnlmsgpqnqdtbjqccbtnvqmtdggdsvvfwjzbdpnztmsblpgcrmppblmtfnjvjwmhnwqdmdrvbvsvhgbhjwzjfcqzwtvzlfmpzvpwsdsqphmqlzwfrqdjdbsmfdzllqqfhmcmhchjpbqtjbczhbcllmtqgczdfnjsjhhcsfdhwwbzzjdqtlvgfgfwbqztqftfqhsfvbzjcbmtdzszfsgzpqpvtqnmjhlrtbvfphvhwbnqsnwgpznnwcbbvdqvvzllqpjbqhqwtfzlhqgvmrpjmlvczqgmffsjvgzvnvccgnmdmbcqjqvbnzvdgnbdwbszldjwjdsnplgqnjmlvrlzcvlgtrdndhjmrtczqjqrzzzclrdmdmrgvljstqfhldfzgvhcdtmncqjjnghgwdgnhbjztfgpqnvfhzngqcftpbqsnvgrqpczwptghbssljnftzmbrqmnbvcbsshvmmmczjgnbjwltflndtntmfznmqhzjjtmmtwbflhmqcmlmzrnvfqzmzhhszqmmqwgzcnncjwvdzvczsgbpcscjptfpwhvvvstvqzhtptrmbhttcrdzhljpmclrbnzmvmtbmmdhfgpjjwsldsgdjflhmfrgvmgzvntgmlglfsntpjjwgnpwsjvsnbctwlsqvprlfmsqfdmzdsdvbtsnqzflvtlgrmdhfgfmwrcqjvbwqsblnwlmtqfhdwwlchqljbljwzmmqgqpwfnmjlvhmppzsdjlbvqwmwqwqqsrgzmpzzzzstrtrbwttzjcmlssstpntnrwtgthrfmthbjjmmtcvjnwsdpdndccgncjbjdqbwtlbfcmgvmlbrdzzlzcpctjvldbpvvmsrwrhhtdsrwljtmntcftwvppjvtttgslrdvpglzdnhrgnrzjsczsmtcfclhvncrhsfvbppqrbwsmjbhjmllwwlqvhbljrqpzshqvllzqtjfhdlnghwblzjldcmfgfjgcbfwqldvdppjmsqqqmznwtcgpmlpqmdpzctwjpnstvgjsbzspfbvfcrgzzcdbzfthgbjrnjwpprblnhdcfgpdcmljpqqzchslpvlwqjvsqtnvnfjhpvwjnjzghnbslhmrhwdppgtvzzmhzzzsvldphjlhrfjwrctfcmnzpvvwqrczcsmznflcwzsplrsdvmfghmnbbjdzclnzpbtjllcszzjcqbbczqhbnfpcctbmhfglsdtnzdjrjbjqczqflznfwzsrmpcgvgrjmhtpgllcpdnlrzvqljprnpvcglvmbtbzjwqfrhdngwsrfjsqnhfncnvprrcngjzfdvcnphcchbqhqpbbbzgmbfndlffnrpzvplptnrvjlzfczvsbctmdqrzdnpvrtgnsgdjwqshglspdzbhflfbsfvbrqgfzmhzhshdlcnqhzbnhbhmsgffgsmvglhscqsrsvmszjfgdhsglbqgwwjndscsqvpccwhlvjbtvftdppcscjblwtmpvvchpzwmblmbbrrvwlglfqwtgcffnzljnmtpbcrszvblwfqdslpqlrmnvrhvrzwnprnmntzrpsdcnwfgljldpvjwwzzbpcltnvghqvqnvmmrfqsnbstdctqqgtpzqttnrrdstrtlfzmvvbzzwwchrscqlnpzdmphdwdqdwbszlhwsbfscthndrdvhtgbpsmznlfjpwjchvzbmrflcphhgjfwstjnzlllztgzjmnwcglmhrbztzdnbcbrgrlmpprrbthbhslfrsjsrrrqvwmqghcgdvvsmrqdwwbdnzmnsqfflpbbjvzjdgsfjdjmbhptmrnbqpqhcdwtvbdlrdzsrchqlsrccjfbfrnnrctdsqbnjzzvrlcwphsscppdmsbrqrzddrdvjwfrldccmzwrhrbpcqpnvnbgpsrhvbchmrdtwqrlvpwhqgnppmsdvqdldlrjrzntlsfwmwhjsghddsppchqlltrhlwrccvflwjbvptclqvwdmrmghbngbflfspsjmglzcqjdtdtldmmngljwfqvfwnmfdtrsmlqzszhzccgvbnnwtpbgssrcqlqgrsjqfhhwpvdtsclgsntwvcsfpvwfqnmwhmtldfswqrsvzshfzwlnwhqhwsrqqzlsdhvqfwnhjcvmplcljmlhbtqtrpjfjfdfmlhtfpnszptfnjbldscjgvjhpzflhm 2 | -------------------------------------------------------------------------------- /MatteoBlack/d07/day07.py: -------------------------------------------------------------------------------- 1 | REQUIRED_SPACE = 30000000 2 | TOTAL_SAPCE = 70000000 3 | 4 | 5 | def execute(input: list) -> (int, int): 6 | result1 = 0 7 | fileSystem = {} 8 | currDir = '' 9 | currPath = '' 10 | for i, cmd in enumerate(input): 11 | if '$ ls' in cmd: 12 | j = i + 1 13 | while '$' not in input[j] and j < len(input) - 1: 14 | if 'dir ' in input[j]: 15 | fileSystem[currPath + (currDir if currPath == '' or currPath == '/' else ('/' + currDir))][3] += 1 16 | j += 1 17 | elif '$ cd ..' in cmd: 18 | currDir = '/' if currPath == '/' else (list(filter("".__ne__, currPath.split('/')))[-1]) 19 | currPath = ('/' + '/'.join( 20 | map(str, list(filter("".__ne__, currPath.split('/')))[:-1]))) if currPath != '/' else '' 21 | elif '$ cd' in cmd: 22 | if cmd.split()[2] == '/': 23 | fileSystem['/'] = (['/', '', 0, 0]) 24 | currDir = '/' 25 | currPath = '' 26 | continue 27 | currPath += currDir if currPath == '' or currPath == '/' else '/' + currDir 28 | currDir = cmd.split()[2] 29 | fileSystem[currPath + (currDir if currPath == '' or currPath == '/' else ('/' + currDir))] = ( 30 | [cmd.split()[2], currPath, 0, 0]) 31 | elif 'dir ' in cmd: 32 | ... 33 | else: 34 | fileSystem[currPath + (currDir if currPath == '' or currPath == '/' else ('/' + currDir))][2] += int( 35 | cmd.split()[0]) 36 | 37 | while True: 38 | change = False 39 | for k in fileSystem.keys(): 40 | if fileSystem[k][3] == 0 and fileSystem[k][1] != '': 41 | change = True 42 | fileSystem[fileSystem[k][1]][2] += fileSystem[k][2] 43 | fileSystem[fileSystem[k][1]][3] -= 1 44 | fileSystem[k][3] = 1 45 | break 46 | if not change: 47 | break 48 | 49 | size = [] 50 | for k in fileSystem.keys(): 51 | size.append(fileSystem[k][2]) 52 | if fileSystem[k][2] < 100000: 53 | result1 += fileSystem[k][2] 54 | 55 | neededSpace = REQUIRED_SPACE - (TOTAL_SAPCE - fileSystem['/'][2]) 56 | result2 = 0 57 | for s in sorted(size): 58 | if s > neededSpace: 59 | result2 = s 60 | break 61 | return result1, result2 62 | 63 | 64 | with open('input.txt') as f: 65 | lines = f.readlines() 66 | cleanLines = [] 67 | for line in lines: 68 | cleanLines.append(line.strip()) 69 | res1, res2 = execute(cleanLines) 70 | print(f'DAY07_1 RESULT: {res1}') 71 | print(f'DAY07_2 RESULT: {res2}') 72 | -------------------------------------------------------------------------------- /MatteoBlack/d08/day08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def execute1(input: list) -> int: 5 | result = 2 * len(input) + 2 * len(input[0]) - 4 6 | for r in range(1, len(input) - 1): 7 | for c in range(1, len(input[r]) - 1): 8 | if input[r][c] > np.max(input[r + 1:, c]) or input[r][c] > np.max(input[:r, c]) or \ 9 | input[r][c] > np.max(input[r, c + 1:]) or input[r][c] > np.max(input[r, :c]): 10 | result += 1 11 | return result 12 | 13 | 14 | def execute2(input: list) -> int: 15 | result = [] 16 | for r in range(1, len(input) - 1): 17 | for c in range(1, len(input[r]) - 1): 18 | score = 1 19 | for i in range(r + 1, len(input)): 20 | if input[r, c] <= input[i, c] or i == len(input[r]) - 1: 21 | score *= (i - r) 22 | break 23 | for i in range(c + 1, len(input[r])): 24 | if input[r, c] <= input[r, i] or i == len(input[r]) - 1: 25 | score *= (i - c) 26 | break 27 | for i in range(r - 1, -1, -1): 28 | if input[r, c] <= input[i, c] or i == 0: 29 | score *= (r - i) 30 | break 31 | for i in range(c - 1, -1, -1): 32 | if input[r, c] <= input[r, i] or i == 0: 33 | score *= (c - i) 34 | break 35 | result.append(score) 36 | return np.max(result) 37 | 38 | 39 | with open('input.txt') as f: 40 | lines = f.readlines() 41 | treeMap = [] 42 | for line in lines: 43 | treeMap.append([*line.strip()]) 44 | treeMap = np.array(treeMap, dtype=np.int64) 45 | 46 | print(f'DAY08_1 RESULT: {execute1(treeMap)}') 47 | print(f'DAY08_2 RESULT: {execute2(treeMap)}') 48 | -------------------------------------------------------------------------------- /MatteoBlack/d10/day10.py: -------------------------------------------------------------------------------- 1 | def execute1(input: list) -> int: 2 | result = 0 3 | cycle = 1 4 | x = 1 5 | increments = [] 6 | check = 20 7 | for el in input: 8 | if 'addx' in el: 9 | cmd, inc = el.split() 10 | increments.append(0) 11 | increments.append(int(inc)) 12 | else: 13 | increments.append(0) 14 | 15 | for action in increments: 16 | if cycle == check: 17 | result += (cycle * x) 18 | check += 40 19 | x += action 20 | cycle += 1 21 | return result 22 | 23 | 24 | def execute2(input: list) -> list: 25 | increments = [] 26 | cycle = 1 27 | x = 2 28 | screen = [] 29 | lineCRT = '' 30 | for el in input: 31 | if 'addx' in el: 32 | cmd, inc = el.split() 33 | increments.append(0) 34 | increments.append(int(inc)) 35 | else: 36 | increments.append(0) 37 | 38 | for action in increments: 39 | pixel = cycle % 40 40 | if (x - 1 <= pixel <= x + 1 and cycle % 40 != 0) or (x - 1 <= 40 <= x + 1 and cycle % 40 == 0): 41 | lineCRT += '#' 42 | else: 43 | lineCRT += '.' 44 | if cycle % 40 == 0: 45 | screen.append(lineCRT) 46 | lineCRT = '' 47 | x += action 48 | cycle += 1 49 | return screen 50 | 51 | 52 | with open('input.txt') as f: 53 | lines = f.readlines() 54 | cleanLines = [] 55 | for line in lines: 56 | cleanLines.append(line.strip()) 57 | 58 | print(f'DAY10_1 RESULT: {execute1(cleanLines)}') 59 | print(f'DAY10_2 RESULT:') 60 | for ln in execute2(cleanLines): 61 | print(ln) 62 | -------------------------------------------------------------------------------- /MatteoBlack/d10/input.txt: -------------------------------------------------------------------------------- 1 | noop 2 | noop 3 | noop 4 | addx 5 5 | addx 1 6 | addx 4 7 | addx 1 8 | noop 9 | addx 4 10 | noop 11 | addx 1 12 | addx 4 13 | addx 8 14 | addx -7 15 | addx 3 16 | addx 1 17 | noop 18 | addx 4 19 | addx 2 20 | addx 5 21 | addx -1 22 | noop 23 | addx -37 24 | noop 25 | noop 26 | addx 3 27 | addx 2 28 | addx 13 29 | addx 12 30 | addx -15 31 | addx -2 32 | addx 2 33 | addx -11 34 | addx 18 35 | addx 2 36 | addx -15 37 | addx 16 38 | addx 5 39 | addx 2 40 | addx 5 41 | noop 42 | noop 43 | noop 44 | addx 3 45 | addx -2 46 | addx -38 47 | noop 48 | addx 3 49 | addx 4 50 | noop 51 | noop 52 | noop 53 | noop 54 | noop 55 | addx 5 56 | addx 5 57 | noop 58 | noop 59 | addx 21 60 | addx -17 61 | addx 6 62 | noop 63 | noop 64 | noop 65 | noop 66 | addx 5 67 | noop 68 | noop 69 | noop 70 | noop 71 | noop 72 | addx 3 73 | addx 5 74 | addx -38 75 | noop 76 | noop 77 | addx 5 78 | addx -2 79 | addx 1 80 | addx 7 81 | noop 82 | addx 22 83 | addx -18 84 | addx -11 85 | addx 27 86 | addx -13 87 | addx 2 88 | addx 5 89 | addx -8 90 | addx 9 91 | addx 2 92 | noop 93 | addx 7 94 | noop 95 | addx 1 96 | noop 97 | addx -38 98 | noop 99 | addx 2 100 | addx 5 101 | addx -3 102 | noop 103 | addx 8 104 | addx 11 105 | addx -6 106 | noop 107 | addx 24 108 | addx -31 109 | addx 10 110 | addx 2 111 | addx 5 112 | addx 3 113 | noop 114 | addx 2 115 | addx -29 116 | addx 21 117 | addx 11 118 | addx 5 119 | addx -39 120 | addx 4 121 | addx -2 122 | addx 2 123 | addx 7 124 | noop 125 | addx -1 126 | addx 2 127 | noop 128 | addx 4 129 | noop 130 | addx 1 131 | addx 2 132 | addx 5 133 | addx 2 134 | noop 135 | noop 136 | addx -6 137 | addx 9 138 | addx -18 139 | addx 25 140 | addx 3 141 | noop 142 | addx -17 143 | noop 144 | -------------------------------------------------------------------------------- /MatteoBlack/d11/day11.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import numpy as np 3 | 4 | 5 | def execute1(input: list) -> int: 6 | inspectingCounter = np.zeros(len(monkeyList)) 7 | for r in range(20): 8 | for monkeyId in range(len(input)): 9 | for obj in input[monkeyId][0]: 10 | inspectingCounter[monkeyId] += 1 11 | if input[monkeyId][1] == '*': 12 | obj *= int(input[monkeyId][2]) if input[monkeyId][2].isdigit() else obj 13 | elif input[monkeyId][1] == '+': 14 | obj += int(input[monkeyId][2]) if input[monkeyId][2].isdigit() else obj 15 | obj = obj // 3 16 | input[monkeyId][0] = np.delete(input[monkeyId][0], 0) 17 | 18 | if obj % input[monkeyId][3] == 0: 19 | input[input[monkeyId][4]][0] = np.append(input[input[monkeyId][4]][0], obj) 20 | else: 21 | input[input[monkeyId][5]][0] = np.append(input[input[monkeyId][5]][0], obj) 22 | inspectingCounter = sorted(inspectingCounter, reverse=True) 23 | return int(inspectingCounter[0] * inspectingCounter[1]) 24 | 25 | 26 | def execute2(input: list) -> int: 27 | inspectingCounter = np.zeros(len(monkeyList)) 28 | 29 | worryLevel = 1 30 | for m in input: 31 | worryLevel *= m[3] 32 | for r in range(10000): 33 | for monkeyId in range(len(input)): 34 | for obj in input[monkeyId][0]: 35 | inspectingCounter[monkeyId] += 1 36 | if input[monkeyId][1] == '*': 37 | obj *= int(input[monkeyId][2]) if input[monkeyId][2].isdigit() else obj 38 | elif input[monkeyId][1] == '+': 39 | obj += int(input[monkeyId][2]) if input[monkeyId][2].isdigit() else obj 40 | input[monkeyId][0] = np.delete(input[monkeyId][0], 0) 41 | obj = obj % worryLevel 42 | if obj % input[monkeyId][3] == 0: 43 | input[input[monkeyId][4]][0] = np.append(input[input[monkeyId][4]][0], obj) 44 | else: 45 | input[input[monkeyId][5]][0] = np.append(input[input[monkeyId][5]][0], obj) 46 | inspectingCounter = sorted(inspectingCounter, reverse=True) 47 | return int(inspectingCounter[0] * inspectingCounter[1]) 48 | #(1*2)+3 = 5 -> 1+3 * 3+2 = 20 49 | #(1+2)*3 = 9 -> 3 + 6 = 9 50 | 51 | with open('input.txt') as f: 52 | lines = f.readlines() 53 | monkeyList = [] 54 | for line in lines: 55 | line = line.strip() 56 | if line == '': 57 | continue 58 | elif 'Monkey' in line: 59 | monkeyList.append([]) 60 | elif 'Starting' in line: 61 | tmp = line.split(': ') 62 | listObj = tmp[1].split(', ') 63 | listObj = np.array(listObj, dtype=np.longlong) 64 | monkeyList[len(monkeyList) - 1].append(listObj) 65 | elif 'Operation' in line: 66 | monkeyList[len(monkeyList) - 1].append(line.split()[4]) 67 | monkeyList[len(monkeyList) - 1].append(line.split()[5]) 68 | elif 'Test' in line: 69 | monkeyList[len(monkeyList) - 1].append(int(line.split()[3])) 70 | elif 'true' in line: 71 | monkeyList[len(monkeyList) - 1].append(int(line.split()[5])) 72 | elif 'false' in line: 73 | monkeyList[len(monkeyList) - 1].append(int(line.split()[5])) 74 | 75 | print(f'DAY11_1 RESULT: {execute1(copy.deepcopy(monkeyList))}') 76 | print(f'DAY11_2 RESULT: {execute2(copy.deepcopy(monkeyList))}') 77 | -------------------------------------------------------------------------------- /MatteoBlack/d11/input.txt: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 57 3 | Operation: new = old * 13 4 | Test: divisible by 11 5 | If true: throw to monkey 3 6 | If false: throw to monkey 2 7 | 8 | Monkey 1: 9 | Starting items: 58, 93, 88, 81, 72, 73, 65 10 | Operation: new = old + 2 11 | Test: divisible by 7 12 | If true: throw to monkey 6 13 | If false: throw to monkey 7 14 | 15 | Monkey 2: 16 | Starting items: 65, 95 17 | Operation: new = old + 6 18 | Test: divisible by 13 19 | If true: throw to monkey 3 20 | If false: throw to monkey 5 21 | 22 | Monkey 3: 23 | Starting items: 58, 80, 81, 83 24 | Operation: new = old * old 25 | Test: divisible by 5 26 | If true: throw to monkey 4 27 | If false: throw to monkey 5 28 | 29 | Monkey 4: 30 | Starting items: 58, 89, 90, 96, 55 31 | Operation: new = old + 3 32 | Test: divisible by 3 33 | If true: throw to monkey 1 34 | If false: throw to monkey 7 35 | 36 | Monkey 5: 37 | Starting items: 66, 73, 87, 58, 62, 67 38 | Operation: new = old * 7 39 | Test: divisible by 17 40 | If true: throw to monkey 4 41 | If false: throw to monkey 1 42 | 43 | Monkey 6: 44 | Starting items: 85, 55, 89 45 | Operation: new = old + 4 46 | Test: divisible by 2 47 | If true: throw to monkey 2 48 | If false: throw to monkey 0 49 | 50 | Monkey 7: 51 | Starting items: 73, 80, 54, 94, 90, 52, 69, 58 52 | Operation: new = old + 7 53 | Test: divisible by 19 54 | If true: throw to monkey 6 55 | If false: throw to monkey 0 56 | -------------------------------------------------------------------------------- /MatteoBlack/d12/day12.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def execute1(input: list, st: list, en: list) -> int: 5 | input = np.array(input) 6 | s = [st[0], st[1], 0, 0] 7 | queue = [s] 8 | closed_list = [st[0], st[1]] 9 | while len(queue) != 0: 10 | s = queue.pop(0) 11 | if s[0] == en[0] and s[1] == en[1]: 12 | return s[3] 13 | height = ord(input[s[0]][s[1]]) if input[s[0]][s[1]] != 'S' else ord('a') 14 | if s[0] + 1 < input.shape[0] and ( 15 | (input[s[0] + 1][s[1]] != 'E' and ord(input[s[0] + 1][s[1]]) <= height + 1) or ( 16 | input[s[0] + 1][s[1]] == 'E' and ord('z') <= height + 1)): 17 | if [s[0] + 1, s[1]] not in closed_list: 18 | cost = h([s[0] + 1, s[1]], en) + s[2] + 1 19 | queue.append([s[0] + 1, s[1], cost, s[3] + 1]) 20 | closed_list.append([s[0] + 1, s[1]]) 21 | if s[0] - 1 >= 0 and ((input[s[0] - 1][s[1]] != 'E' and ord(input[s[0] - 1][s[1]]) <= height + 1) or ( 22 | input[s[0] - 1][s[1]] == 'E' and ord('z') <= height + 1)): 23 | if [s[0] - 1, s[1]] not in closed_list: 24 | cost = h([s[0] - 1, s[1]], en) + s[2] + 1 25 | queue.append([s[0] - 1, s[1], cost, s[3] + 1]) 26 | closed_list.append([s[0] - 1, s[1]]) 27 | if s[1] + 1 < input.shape[1] and ( 28 | (input[s[0]][s[1] + 1] != 'E' and ord(input[s[0]][s[1] + 1]) <= height + 1) or ( 29 | input[s[0]][s[1] + 1] == 'E' and ord('z') <= height + 1)): 30 | if [s[0], s[1] + 1] not in closed_list: 31 | cost = h([s[0], s[1] + 1], en) + s[2] + 1 32 | queue.append([s[0], s[1] + 1, cost, s[3] + 1]) 33 | closed_list.append([s[0], s[1] + 1]) 34 | if s[1] - 1 >= 0 and ((input[s[0]][s[1] - 1] != 'E' and ord(input[s[0]][s[1] - 1]) <= height + 1) or ( 35 | input[s[0]][s[1] - 1] == 'E' and ord('z') <= height + 1)): 36 | if [s[0], s[1] - 1] not in closed_list: 37 | cost = h([s[0], s[1] - 1], en) + s[2] + 1 38 | queue.append([s[0], s[1] - 1, cost, s[3] + 1]) 39 | closed_list.append([s[0], s[1] - 1]) 40 | queue.sort(key=costSort) 41 | return np.inf 42 | 43 | 44 | def execute2(input: list, st: list, en: list) -> int: 45 | input[st[0]][st[1]] = 'a' 46 | input = np.array(input) 47 | min = np.inf 48 | for r in range(input.shape[0]): 49 | for c in range(input.shape[1]): 50 | if input[r][c] == 'a': 51 | tmp = execute1(list(input), [r, c], en) 52 | if tmp < min: 53 | min = tmp 54 | return min 55 | 56 | 57 | def h(s: list, en: list) -> int: 58 | return np.abs((en[0] - s[0])) + np.abs((en[1] - s[1])) 59 | 60 | 61 | def costSort(val): 62 | return val[2] 63 | 64 | 65 | with open('input.txt') as f: 66 | lines = f.readlines() 67 | map = [] 68 | start = [] 69 | end = [] 70 | for line in lines: 71 | map.append([*line.strip()]) 72 | if 'S' in map[len(map) - 1]: 73 | start = [len(map) - 1, map[len(map) - 1].index('S')] 74 | if 'E' in map[len(map) - 1]: 75 | end = [len(map) - 1, map[len(map) - 1].index('E')] 76 | 77 | print(f'DAY12_1 RESULT: {execute1(map, start, end)}') 78 | print(f'DAY12_2 RESULT: {execute2(map, start, end)}') 79 | -------------------------------------------------------------------------------- /MatteoBlack/d12/input.txt: -------------------------------------------------------------------------------- 1 | abaaaaaccccccccccccccccccaaaaaaaaaaaaaccccaaaaaaaccccccccccccccccccccccccccccaaaaaa 2 | abaaaaaaccaaaacccccccccccaaaaaaaaacaaaacaaaaaaaaaacccccccccccccccccccccccccccaaaaaa 3 | abaaaaaacaaaaaccccccccccaaaaaaaaaaaaaaacaaaaaaaaaacccccccccccccaacccccccccccccaaaaa 4 | abaaaaaacaaaaaacccccccccaaaaaaaaaaaaaaccaaacaaaccccccccccccccccaacccccccccccccccaaa 5 | abccaaaccaaaaaacccaaaaccaaaaaaaaaaaaaccccaacaaacccccccccaacaccccacccccccccccccccaaa 6 | abcccccccaaaaaccccaaaacccccaaaaacccaaaccaaaaaaccccccccccaaaaccccccccccccccccccccaac 7 | abcccccccccaaaccccaaaacccccaaaaacccccccccaaaaaccccccccccklllllccccccccccccccccccccc 8 | abcccccccccccccccccaaccccccccaaccccccccaaaaaaaccccccccckklllllllcccccddccccaacccccc 9 | abaccccccccccccccccccccccccccaaccccccccaaaaaaaaccccccckkkklslllllcccddddddaaacccccc 10 | abacccccccccccccccccccccccccccccccaaaccaaaaaaaaccccccckkkssssslllllcddddddddacccccc 11 | abaccccccccccccccccccccccccccccccccaaaaccaaacaccccccckkksssssssslllmmmmmdddddaacccc 12 | abcccccccccccccccaaacccccccccccccaaaaaaccaacccccccccckkkssssusssslmmmmmmmdddddacccc 13 | abcccccccaaccccaaaaacccccccccccccaaaaaccccccaaaaaccckkkrssuuuussssqmmmmmmmmdddccccc 14 | abcccccccaaccccaaaaaacccccccaaccccaaaaacccccaaaaacckkkkrruuuuuussqqqqqqmmmmdddccccc 15 | abccccaaaaaaaacaaaaaacccccccaaaaccaaccaccccaaaaaacjkkkrrruuuxuuusqqqqqqqmmmmeeccccc 16 | abcaaaaaaaaaaacaaaaaccccccaaaaaacccccaaccccaaaaajjjjrrrrruuuxxuvvvvvvvqqqmmmeeccccc 17 | abcaacccaaaaccccaaaaaaacccaaaaacccacaaaccccaaaajjjjrrrrruuuxxxxvvvvvvvqqqmmeeeccccc 18 | abaaaaccaaaaacccccccaaaccccaaaaacaaaaaaaacccaajjjjrrrrtuuuuxxxyvyyyvvvqqqnneeeccccc 19 | abaaaaaaaaaaacccaaaaaaaccccaacaacaaaaaaaacccccjjjrrrttttuxxxxxyyyyyvvvqqnnneeeccccc 20 | abaaaaaaaccaacccaaaaaaaaacccccccccaaaaaaccccccjjjrrrtttxxxxxxxyyyyyvvvqqnnneeeccccc 21 | SbaaaaaacccccccccaaaaaaaaaccccccccaaaaacccccccjjjrrrtttxxxEzzzzyyyvvrrrnnneeecccccc 22 | abaaaaacccccccccccaaaaaaacccccccccaaaaaaccccccjjjqqqtttxxxxxyyyyyvvvrrrnnneeecccccc 23 | abaaacccccccccccaaaaaaaccaaccccccccccaaccaaaaajjjqqqttttxxxxyyyyyyvvrrrnnneeecccccc 24 | abaaacccccccccccaaaaaaaccaaacaaacccccccccaaaaajjjjqqqtttttxxyywyyyywvrrnnnfeecccccc 25 | abcaaacccccccaaaaaaaaaaacaaaaaaaccccccccaaaaaaciiiiqqqqtttxwyywwyywwwrrrnnfffcccccc 26 | abcccccccccccaaaaaaaaaaccaaaaaacccccccccaaaaaacciiiiqqqqttwwywwwwwwwwrrrnnfffcccccc 27 | abccccccccccccaaaaaacccaaaaaaaacccccccccaaaaaaccciiiiqqqttwwwwwswwwwrrrrnnfffcccccc 28 | abccccccccccccaaaaaacccaaaaaaaaacccccccccaaacccccciiiqqqtswwwwssssrrrrrroofffcccccc 29 | abccccccaaaaacaaaaaacccaaaaaaaaaaccccccccccccccccciiiqqqssswsssssssrrrrooofffaccccc 30 | abccccccaaaaacaaccaaccccccaaacaaacccccccccccccccccciiiqqssssssspoorrrooooofffaacccc 31 | abcccccaaaaaacccccccccccccaaacccccccccccccccccccccciiiqppssssspppooooooooffffaacccc 32 | abcccccaaaaaacccccccccccccaacccccccccccccccccccccccciipppppppppppoooooooffffaaccccc 33 | abcccccaaaaaaccccccccccccccccccccccccccccccccccccccciihppppppppgggggggggfffaaaccccc 34 | abccccccaaacccccccccccccccccccccccaccccccccccccccccchhhhpppppphggggggggggfaaaaccccc 35 | abaaaccccccccccccccccccccccaccccaaacccccccccccccccccchhhhhhhhhhgggggggggcaacccccccc 36 | abaaccaaaccaccccccccccccccaaacccaaacaacccaaaaacccccccchhhhhhhhhgaaccccccccccccccccc 37 | abaaacaaacaacccccccccaaaccaaaacaaaaaaaaccaaaaaccccccccchhhhhhaaaaacccccccccccccccca 38 | abaaaccaaaaaccccccccccaaacaaaaaaaacaaaaccaaaaaaccccccccccaaacccaaaacccccccccccaccca 39 | abcccaaaaaaccccccccccaaaaaaaaaaaaacaaaaccaaaaaaccccccccccaaaccccaaaccccccccccaaaaaa 40 | abcccaaaaaaaacccccccaaaaaaaaaaaaaaaaaccccaaaaaacccccccccccccccccccccccccccccccaaaaa 41 | abcccaacaaaaaccccccaaaaaaaaaaaaaaaaaaacccccaacccccccccccccccccccccccccccccccccaaaaa 42 | -------------------------------------------------------------------------------- /MatteoBlack/d12/test.txt: -------------------------------------------------------------------------------- 1 | Sabqponm 2 | abcryxxl 3 | accszExk 4 | acctuvwj 5 | abdefghi -------------------------------------------------------------------------------- /Puricelli/d01/puzzle1.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | file1 = open("/Users/Puricelli/Desktop/puzzle1.txt", 'r') 4 | fruits = [0, 0, 0] 5 | 6 | moment_act = 0 7 | 8 | while True: 9 | 10 | line = file1.readline() 11 | 12 | if not line: 13 | break 14 | 15 | if line == "\n": 16 | fruits.sort() 17 | for i in range(len(fruits)): 18 | if fruits[i] < moment_act: 19 | fruits[i] = moment_act 20 | break 21 | moment_act = 0 22 | else: 23 | moment_act = moment_act + int(line) 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | file1.close() 32 | 33 | tot = 0 34 | 35 | print("MAXIMUS") 36 | 37 | for x in fruits: 38 | tot = tot + x 39 | print(x) 40 | 41 | 42 | print("totTOP3 -> ", tot) 43 | 44 | print("\n") 45 | print("DONE") -------------------------------------------------------------------------------- /Tommimon/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.23) 2 | project(advent-of-code-2022) 3 | 4 | set(CMAKE_CXX_STANDARD 23) 5 | 6 | add_executable(day01 d01/part1and2.cpp py++.cpp) 7 | add_executable(day02 d02/part1and2.cpp py++.cpp) 8 | add_executable(day03 d03/part1and2.cpp py++.cpp) 9 | add_executable(day04 d04/part1and2.cpp py++.cpp) 10 | add_executable(day05 d05/part1and2.cpp py++.cpp) 11 | add_executable(day06 d06/part1and2.cpp py++.cpp) 12 | add_executable(day07 d07/part1and2.cpp py++.cpp) 13 | add_executable(day08p1 d08/part1.cpp py++.cpp) 14 | add_executable(day08p2 d08/part2.cpp py++.cpp) 15 | add_executable(day09 d09/part1and2.cpp py++.cpp) 16 | add_executable(day10 d10/part1and2.cpp py++.cpp) 17 | add_executable(day11p1 d11/part1.cpp py++.cpp) 18 | add_executable(day11p2 d11/part2.cpp py++.cpp) 19 | add_executable(day12 d12/part1and2.cpp py++.cpp) 20 | add_executable(day13 d13/part1and2.cpp py++.cpp) 21 | add_executable(day14 d14/part1and2.cpp py++.cpp) 22 | add_executable(day15p1 d15/part1.cpp py++.cpp) 23 | add_executable(day15p2 d15/part2.cpp py++.cpp) 24 | add_executable(day16 d16/part1and2.cpp py++.cpp) 25 | add_executable(day17 d17/part1and2.cpp py++.cpp) 26 | add_executable(day18p1 d18/part1.cpp py++.cpp) 27 | add_executable(day18p2 d18/part2.cpp py++.cpp) 28 | add_executable(day19 d19/part1and2.cpp py++.cpp) 29 | add_executable(day20 d20/part1and2.cpp py++.cpp) 30 | add_executable(day21 d21/part1and2.cpp py++.cpp) 31 | add_executable(day22p1 d22/part1.cpp py++.cpp) 32 | add_executable(day22p2 d22/part2.cpp py++.cpp) 33 | add_executable(day23 d23/part1and2.cpp py++.cpp) 34 | add_executable(day24 d24/part1and2.cpp py++.cpp) 35 | add_executable(day25 d25/part1and2.cpp py++.cpp) 36 | 37 | include_directories(.) 38 | -------------------------------------------------------------------------------- /Tommimon/d01/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "py++.hpp" 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | ifstream ifs("input"); 11 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 12 | 13 | vector packs(split(input, "\n\n")); 14 | vector pack_values; 15 | for (const auto& p: packs) { 16 | vector elfs(split(p, "\n")); 17 | vector numbers(map([](const string& e) {return e.empty() ? 0 : stoi(e);}, elfs)); 18 | pack_values.emplace_back(sum(numbers)); 19 | } 20 | 21 | int largest = max(pack_values, [](const int e) {return e;}); 22 | int tot = largest; 23 | std::cout << largest << std::endl; 24 | remove(pack_values, largest); 25 | 26 | largest = max(pack_values, [](const int e) {return e;}); 27 | tot += largest; 28 | remove(pack_values, largest); 29 | 30 | largest = max(pack_values, [](const int e) {return e;}); 31 | tot += largest; 32 | std::cout << tot << std::endl; 33 | } 34 | -------------------------------------------------------------------------------- /Tommimon/d02/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int game_score(int opponent, int me) { 9 | int partial = (me) + 1; 10 | if (opponent == me) 11 | partial += 3; 12 | else if (me == (opponent+1)%3) 13 | partial += 6; 14 | return partial; 15 | } 16 | 17 | int main() { 18 | ifstream ifs("input"); 19 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 20 | 21 | vector lines(split(input, "\n")); 22 | int score1 = 0; 23 | int score2 = 0; 24 | for (auto l : lines) { 25 | char opponent = l[0] - 'A'; 26 | char me1 = l[2] - 'X'; 27 | char me2 = (l[0] + l[2] - 1) % 3; 28 | if (me2 == -1) me2 = 2; 29 | score1 += game_score(opponent, me1); 30 | score2 += game_score(opponent, me2); 31 | } 32 | cout << score1 << endl; 33 | cout << score2 << endl; 34 | } 35 | -------------------------------------------------------------------------------- /Tommimon/d03/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | int score1 = 0; 14 | 15 | for (auto l : lines) { 16 | string part1 = l.substr(0, l.size()/2); 17 | string part2 = l.substr(l.size()/2, l.size()/2); 18 | char both = -1; 19 | for (auto c : part1) { 20 | if (part2.contains(c)) 21 | both = c; 22 | } 23 | if (both >= 'A' && both <= 'Z') 24 | score1 += both-'A'+27; 25 | else 26 | score1 += both-'a'+1; 27 | } 28 | 29 | int score2 = 0; 30 | for (int i = 0; i < lines.size(); i += 3) { 31 | string part1 = lines[i]; 32 | string part2 = lines[i+1]; 33 | string part3 = lines[i+2]; 34 | char all = -1; 35 | for (auto c : part1) { 36 | if (part2.contains(c) && part3.contains(c)) 37 | all = c; 38 | } 39 | if (all >= 'A' && all <= 'Z') 40 | score2 += all - 'A' + 27; 41 | else 42 | score2 += all - 'a' + 1; 43 | } 44 | 45 | cout << score1 << endl; 46 | cout << score2 << endl; 47 | } 48 | -------------------------------------------------------------------------------- /Tommimon/d04/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | int score1 = 0; 14 | int score2 = 0; 15 | for(auto l: lines) { 16 | vector nums = map([](string s) { return stoi(s); }, split(replace(l, ",", "-"), "-")); 17 | score1 += ((nums[2] >= nums[0] && nums[3] <= nums[1]) || (nums[2] <= nums[0] && nums[3] >= nums[1])); 18 | score2 += ((nums[2] >= nums[0] && nums[2] <= nums[1]) || (nums[0] >= nums[2] && nums[0] <= nums[3])); 19 | } 20 | cout << score1 << endl; 21 | cout << score2 << endl; 22 | } 23 | -------------------------------------------------------------------------------- /Tommimon/d05/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | auto separator = std::find_if(lines.begin(), lines.end(), [&](const auto &item) { 14 | return item == ""; 15 | }); 16 | 17 | vector nums = split(*(separator-1), " "); 18 | vector> stacks1, stacks2; 19 | stacks1.resize(nums.size()); 20 | stacks2.resize(nums.size()); 21 | for (auto i = separator-2; i >= lines.begin(); i--) { 22 | for (int j = 0; j < stacks1.size(); ++j) { 23 | if ((*i).length() > j*4) { 24 | char c = (*i).substr(j * 4 + 1, 1)[0]; 25 | if (c != ' ') { 26 | stacks1[j].push_back(c); 27 | stacks2[j].push_back(c); 28 | } 29 | } 30 | } 31 | } 32 | 33 | for (auto i = separator+1; i < lines.end(); i++) { 34 | int count = stoi(split(split(*i, " from ")[0], "move ")[1]); 35 | int from = stoi(split(split(*i, " from ")[1], " to ")[0])-1; 36 | int to = stoi(split(split(*i, " from ")[1], " to ")[1])-1; 37 | for (int j = 0; j < count; ++j) { 38 | stacks1[to].push_back(stacks1[from][stacks1[from].size() - 1]); 39 | stacks1[from].erase(stacks1[from].end() - 1); 40 | } 41 | } 42 | 43 | for (auto s: stacks1) 44 | cout << s[s.size()-1]; 45 | cout << endl; 46 | 47 | for (auto i = separator+1; i < lines.end(); i++) { 48 | int count = stoi(split(split(*i, " from ")[0], "move ")[1]); 49 | int from = stoi(split(split(*i, " from ")[1], " to ")[0])-1; 50 | int to = stoi(split(split(*i, " from ")[1], " to ")[1])-1; 51 | for (int j = 0; j < count; ++j) { 52 | stacks2[to].push_back(stacks2[from][stacks2[from].size() - (count-j)]); 53 | stacks2[from].erase(stacks2[from].end() - (count - j)); 54 | } 55 | } 56 | 57 | for (auto s: stacks2) 58 | cout << s[s.size()-1]; 59 | cout << endl; 60 | } 61 | -------------------------------------------------------------------------------- /Tommimon/d06/input: -------------------------------------------------------------------------------- 1 | rnttlvtttmnmpmhpmmzvmmhpmmnrntnnsnrnndvnddmbbtptssjcczmmbwmbmwwmmflfggwzzhjjgppwrwdwqwbbbmwbwgggqccmlmdmgdgqgpqgpgzzndddgdbbsvvfsfppwjwzjjcnjccwrwrprgppbpddnccjggfrggqngqgdqgddvsshqhmqqfvqvlvwvnwwmccrpcczvcczzgsgwwlggqsggmdgglblpblltbbzrbzzcscgccwssbddsmddzzvvhjjwjwrjwrwfftmttplldnllqttdhddmvdmmhsmsmqsqwqpqbpqppqdppbnpnhhppjbbrzrbrggdbddnrdrzrdrpdppzzfzrzgzllwlwzlllwtltslttlmtmqqbbzmzhmmjjwrjrrnzrzhzmmtbbrppmmfddjhdjdsdllrbbpfphpzpwwjvwwdpwpzwwzrrfnfwwfbbpttzjtjptpvvsfvvljjrzzmdmsddpvpgvvdcdwcwhwghwggrjjhdjhddzhhwbhwbwwbrwwrfrmfrfvrfftzzqrzqrqtrqtrqtrqttgctclcwlwwvqqnlnqlnnndmnmggznzhzqzggzrzbbrwbrwrvvscvvmvhmvvbsbjsjfjpfpqpbbbbjtjptpprnndpnnrsswffmnnjhhpqhqwhhbwhbhgbbfrbrjjpmjmzzdzjdjffcvvtwttrrcscvcncqcfcwwpgpfptpcpspllcfcssgbglgdllcdcmcpcqclctcvtvvbhbbdhhczhzshzhbhchzchchlcllwswqqmpmbbqwwzgzhggczgzffsgggjjlcjjpnjjfqjfjvfvrfrlrvvpfpcfcbffnmfmbmbgggplldblddvcvjcvvmbmmmrvrvvnhhqttgdgvdgdqgddcsclljplpbbsvbsbfsffshsgslsllzztggttfccctwtswwrvwvnnfbnnvjnvvlgvvfppmvpmmvpmmcttjffgsffcllncntcncfclcgcppvdvpvrvbrrnnvrvhrvrzvrvnrvrdrnrqrnngsshqhcqqbfqqzmmmzjzqztznttjffqzfqfbqqsggclgclcddtqqdppbjppqvpqvvmqmqnqcnqcqcqvqjqzqbzzbrrgfgddgtgnnpjjbzjjbbtstzszwswggfffmnnpllfvvnwnpwpqwqwlllqlmmzcmcrcmrcmrcrffnrnssjjrdjjwwqgwqgqdggwzwjzzbfbpptctchthbbbqsqggrllldwdffvwwlrwwljwjnncmmjmvjmmqnnmrrgjgvvpqqbppqlpqllqnqdndwwlppcjcdjjcnnmddffgjjrwjrwjjhvhqvhhfssrtsrsgrrwjwhhbqqpzzrdrhrqhrqrprqqmllsszhzllcdllpmpbbmsmjjmwmssvlslwwmtwmmgvgcvggzcgzgddsjsjfsssftfcchhfmfcmcpcpvcppqggjddljjbttwrrhghqggpghgngzngznnzqnnssqlqmqbmmmqdqqhttnqqjhhphrhccbggmjjpwpbwpprdrvvjzvvtctzzmpphwpwdpdvvhbvvdvsdsjdjbdjdljjstswttnhhbfffmnngddlglqggvzvlzvzqqvbqbvbjbnjjsvjjwpjjzshrdtrjttvqnbltrfpvnztrwzrtgjpzgqdjfglqgjrgdzbhqpghdbfhlfjhbfjfjppfgljmgwljlsbmltgztthnzvdrgqlgddvqhzctdgcphfqvnpjjgzwqfvnhvzdrwtpgfdjpqnfshslqmplcprdntnhqqbqptwzvdddhcjcqrfhjqnjvpnhttblwgjwlfwntdchgfjmdbgtqtdgnzbqwzzcltwtmtqtdbvjtfvlzpcvgmrfqwfhwqmhvwhftzgmhshffnjwqbvztszsrrglqvhfpqmbnqjsfnwdwgdtmztbvqrmztfctmvptbwnfzfgdztjgnqsrsqqqnrpgzsqszzwwwgqnnnrdzhzdbqjgbvncprzcjqchzfgnclbrmphbsdwwpvwjwlbshhgjfbhjjtdqrmrcjfnrrhqrpsbglthzpvfglqspttdpwlljhnlrjpzchbrqcgtmcscjnwvpztfjdcwbnbgmbpgdthgnhbrtwftnscbsrndghbslflpcpjwbcjnhzcwdcslmzqbtrlnzmntlpjcsctnsqwtbffqlhfgcsflvfwnmczvsbflnnnzpfjfrcwhhcbtbjcbghtcwcgdbrwrgfgvpwtcwlwcmnmrtcrjbwtwlrfstztsghfvrfjzzpswpqfqpvqstvbhqfjlgmtdlhqrhwzqpnqpllnlgzwptbgftmblqcwfcllbwfzdhrndfrvdvwzqvhnghlzvhldnnvrgqvlpfdnpmcgddjmstzsqfvzwftflrwtzqwjbbqhjpfbdztdfsgsztvvrvslgspgpdcmwszdfsddqhpzpsjgqmgzqvhchlgrmcmzwzbtwfphvgcdmhfdczhffgmqpncdjszzgwfvwsqddvbcgngbjwhmphjsmjthvbthhfwdtqmjctcmdpqpsdrnrzdgzgzctbhwsgvtjgwjbsnnjmpmqgwrnqfqbpnrpddjsrsvmcshhthwfrwmqsrjhlsrgfzvwmdzhwrvchppqldghgzflrnwqnvntmtdwmrpgbdbzvcmnqstzntvllcgzsnvrhqzsfncznhgrggmvrmgsqmhbdsjbsqqhzppfcwdrgdvfjdscrvpwtsdmcnczwbbjhvddprwtzfwslcfdcrqfszcgmhtdfvlqzqtvwngzvmmqcrqpzwzhggjnphsrmnctnfhtppglspnvzrsqfgzdfrrwbzbqwvbvbnzgmdrqrnsvdpvlgcmnggsbmbtfwrvdjrtgtgcqscnfpgswgsngdqnnscffdcnlrcpdpcbpzvqcrtjhlwvgnfhhqmprthrtcvcjjwgprqqdwfbgmzlwttjpvcjzfwbdhvngsjpgtqsvbldbcvhjhzbjzblqtqhlnbzzqfcpnzdhbplztcgvzhbgshqbccgwzhftqvtwzbwmnfrsgphhhgtsmwlqhlcchtbtggqwmbdthhmqqjtfdvfpddfdrtfjbpmwtcbfnrhwcnpdqrdtsfdmjfzdwwgnftnwpssgqtlpdbwhnzcnfmppclsswbhcdghpnslwjznqszgdtrnpncsqsnbrplrfwpbnfnvttlzcjtvhzcpzmhfsfzfjlzqqnprpdvwbfthmrswqrcqqwrnwzmgjqqsnqdblssmhngjjvprmqbswtgzzvprwhrgjqshvmwzgrgfmzlgrtzbmdlzncwqdftfsndvdfmmplswdbjtbcbvcvtpjvrqpghczpqvvpqwfbfhllbpvrrfsmsjhqbldcrwvzvcvzfffqvplbfbdbwctbjsljlfwtbcnpsbtpmcqgdvltmztvrcfsprbnvwplwhncgsdnrdmqnmcvpvvrlmlwtgvrnwvzsfctdlcfvtgqmpnbwcbwvfpmqnbjvwffpjtvvgflhrnlngrzhhttdtdbvscftsqtvbrgzfgsjvwhzjcbsqcttlgwmhhvjhwhgmmdtflfdbvnhgcblqmwjfsqnngjqfbvdnsfbgwjfhpgdgvhpbvlrtpcpvhrbtqpbffzcqrvbqwvqmmrcwtnvcgwvsqzvrwdbcnjshbnbftmmvrmjvgfdwbsjvqfdwnnvqqhbmshcrclrwhfhbtnwqvmrrvdwgcwcsrhdbqndsthmrmbjhttjtzmlflbrmhlcgsbdjcjcvwcjffnqrntpflrgfcngpchtrzpnflwjvcgbwtsnjfqsggwmwhdvbzdpjmtwlmrslnjsndjtgjmmwmdgtnfrztppzqvqhbfzqpsdhvsshddlzwcmsndrpqhndsrjnngnmgmzrvchwlqgdnbssbhpbpgwpsrcnbphpslvqplhpgdhmrnwwjmhvnsfjmrfwtvjjrmgptvjffhbgpmfgmgrcjrwqhccssrqjpljbpwcvsfdtmbhzsmsjsgblgpcqszsttfclrjcnsslmngmbmwqfhddbvmbvwmrmvglsl 2 | -------------------------------------------------------------------------------- /Tommimon/d06/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | ifstream ifs("input"); 9 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 10 | 11 | int score1 = -1; 12 | int score2 = -1; 13 | string prev; 14 | for(int i = 0; i < input.size(); i++) { 15 | if (score1 == -1 && prev.size() == 4) { 16 | score1 = i; 17 | } 18 | if (score2 == -1 && prev.size() == 14) { 19 | score2 = i; 20 | } 21 | if (prev.contains(input[i])) { 22 | auto start = prev.find(input[i]) + 1; 23 | prev = prev.substr(start, prev.size() - start); 24 | } 25 | prev += input[i]; 26 | } 27 | cout << score1 << endl; 28 | cout << score2 << endl; 29 | } 30 | -------------------------------------------------------------------------------- /Tommimon/d07/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | int thisDir = 0; 14 | int res1 = 0; 15 | 16 | vector dirs; 17 | vector space; 18 | for(int j = lines.size()-1; j >= 0; j--) { 19 | string l= lines[j]; 20 | if (l.size() > 2 && l.substr(0,3) == "dir") { 21 | for (int i = dirs.size()-1; i >= 0; --i) { 22 | if (dirs[i] == l.substr(4, l.size()-4)) { 23 | thisDir += space[i]; 24 | break; 25 | } 26 | } 27 | } 28 | else if (l[0] == '$') { 29 | if (l[2] == 'c' && l[5] != '.') { 30 | bool found = false; 31 | for (int i = dirs.size()-1; i >= 0; --i) { 32 | if (dirs[i] == split(l, "$ cd ")[1]) { 33 | space[i] = thisDir; 34 | found = true; 35 | break; 36 | } 37 | } 38 | if (!found) { 39 | dirs.push_back(split(l, "$ cd ")[1]); 40 | space.push_back(thisDir); 41 | } 42 | if (thisDir <= 100000) 43 | res1 += thisDir; 44 | thisDir = 0; 45 | } 46 | } else { 47 | thisDir += stoi(split(l, " ")[0]); 48 | } 49 | } 50 | 51 | int toFree = space[space.size() - 1] - 40000000; 52 | int chosen = 70000000; 53 | for (int i = 0; i < dirs.size(); ++i) { 54 | if (space[i] >= toFree && space[i] < chosen) 55 | chosen = space[i]; 56 | } 57 | 58 | cout << res1 << endl; 59 | cout << chosen << endl; 60 | } 61 | -------------------------------------------------------------------------------- /Tommimon/d08/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | int res1 = 0; 14 | int l = lines[0].size(); 15 | 16 | for (int k = 0; k < 4; ++k) { 17 | for (int i = 0; i < l; ++i) { 18 | int max = -1; 19 | for (int j = 0; j < l; ++j) { 20 | char *letter; 21 | int b; 22 | if (k < 2) 23 | b = j; 24 | else 25 | b = l-j-1; 26 | if (k%2 == 0) 27 | letter = &lines[i][b]; 28 | else 29 | letter = &lines[b][i]; 30 | int val; 31 | if (*letter >= '0' && *letter <= '9') 32 | val = *letter - '0'; 33 | else 34 | val = *letter - 'A'; 35 | if (val > max) { 36 | max = val; 37 | if (*letter >= '0' && *letter <= '9') 38 | res1++; 39 | *letter = val + 'A'; 40 | } 41 | } 42 | } 43 | } 44 | 45 | cout << res1 << endl; 46 | } 47 | -------------------------------------------------------------------------------- /Tommimon/d08/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | int l = lines[0].size(); 14 | 15 | int maxScore = -1; 16 | for (int i = 0; i < l; ++i) { 17 | for (int j = 0; j < l; ++j) { 18 | int sceneScore = 1; 19 | for (int k = 0; k < 4; ++k) { 20 | int lineScore = 0; 21 | int startTree = lines[i][j] - '0'; 22 | for (int iter = 1; iter < l; ++iter) { 23 | char letter; 24 | int iterAlias; 25 | if (k < 2) 26 | iterAlias = iter; 27 | else 28 | iterAlias = -iter; 29 | if (k%2 == 0) 30 | if (j + iterAlias < 0 || j + iterAlias >= l) 31 | break; 32 | else 33 | letter = lines[i][j + iterAlias]; 34 | else 35 | if (i + iterAlias < 0 || i + iterAlias >= l) 36 | break; 37 | else 38 | letter = lines[i + iterAlias][j]; 39 | int val = letter - '0'; 40 | lineScore++; 41 | if (val >= startTree) { 42 | break; 43 | } 44 | } 45 | sceneScore *= lineScore; 46 | } 47 | 48 | if (sceneScore > maxScore) { 49 | maxScore = sceneScore; 50 | } 51 | } 52 | } 53 | 54 | cout << maxScore << endl; 55 | } 56 | -------------------------------------------------------------------------------- /Tommimon/d09/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "py++.hpp" 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | ifstream ifs("input"); 11 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 12 | 13 | vector lines(split(input, "\n")); 14 | 15 | vector knotsX, knotsY; 16 | set history; 17 | int lengthArray[] = {2, 10}; 18 | knotsX.resize(10); 19 | knotsY.resize(10); 20 | 21 | for (auto length: lengthArray) { 22 | history.clear(); 23 | for (int i = 0; i < length; ++i) { 24 | knotsX[i] = 0; 25 | knotsY[i] = 0; 26 | } 27 | 28 | for (auto l: lines) { 29 | for (int j = 0; j < stoi(l.substr(2, l.size() - 2)); ++j) { 30 | if (l[0] == 'U') knotsY[0]++; 31 | if (l[0] == 'R') knotsX[0]++; 32 | if (l[0] == 'D') knotsY[0]--; 33 | if (l[0] == 'L') knotsX[0]--; 34 | 35 | for (int i = 1; i < length; ++i) { 36 | int x = knotsX[i], y = knotsY[i], prevX = knotsX[i - 1], prevY = knotsY[i - 1]; 37 | 38 | if ((x - prevX) * (x - prevX) + (y - prevY) * (y - prevY) > 2) { 39 | knotsX[i] = abs(prevX - x) <= 1 ? prevX : (prevX + x) / 2; 40 | knotsY[i] = abs(prevY - y) <= 1 ? prevY : (prevY + y) / 2; 41 | } 42 | } 43 | history.insert(knotsX[length - 1] * 10000 + knotsY[length - 1]); 44 | } 45 | } 46 | cout << history.size() << endl; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Tommimon/d10/input: -------------------------------------------------------------------------------- 1 | noop 2 | noop 3 | noop 4 | addx 5 5 | addx 1 6 | addx 4 7 | addx 1 8 | noop 9 | addx 4 10 | noop 11 | addx 1 12 | addx 4 13 | addx 8 14 | addx -7 15 | addx 3 16 | addx 1 17 | noop 18 | addx 4 19 | addx 2 20 | addx 5 21 | addx -1 22 | noop 23 | addx -37 24 | noop 25 | noop 26 | addx 3 27 | addx 2 28 | addx 13 29 | addx 12 30 | addx -15 31 | addx -2 32 | addx 2 33 | addx -11 34 | addx 18 35 | addx 2 36 | addx -15 37 | addx 16 38 | addx 5 39 | addx 2 40 | addx 5 41 | noop 42 | noop 43 | noop 44 | addx 3 45 | addx -2 46 | addx -38 47 | noop 48 | addx 3 49 | addx 4 50 | noop 51 | noop 52 | noop 53 | noop 54 | noop 55 | addx 5 56 | addx 5 57 | noop 58 | noop 59 | addx 21 60 | addx -17 61 | addx 6 62 | noop 63 | noop 64 | noop 65 | noop 66 | addx 5 67 | noop 68 | noop 69 | noop 70 | noop 71 | noop 72 | addx 3 73 | addx 5 74 | addx -38 75 | noop 76 | noop 77 | addx 5 78 | addx -2 79 | addx 1 80 | addx 7 81 | noop 82 | addx 22 83 | addx -18 84 | addx -11 85 | addx 27 86 | addx -13 87 | addx 2 88 | addx 5 89 | addx -8 90 | addx 9 91 | addx 2 92 | noop 93 | addx 7 94 | noop 95 | addx 1 96 | noop 97 | addx -38 98 | noop 99 | addx 2 100 | addx 5 101 | addx -3 102 | noop 103 | addx 8 104 | addx 11 105 | addx -6 106 | noop 107 | addx 24 108 | addx -31 109 | addx 10 110 | addx 2 111 | addx 5 112 | addx 3 113 | noop 114 | addx 2 115 | addx -29 116 | addx 21 117 | addx 11 118 | addx 5 119 | addx -39 120 | addx 4 121 | addx -2 122 | addx 2 123 | addx 7 124 | noop 125 | addx -1 126 | addx 2 127 | noop 128 | addx 4 129 | noop 130 | addx 1 131 | addx 2 132 | addx 5 133 | addx 2 134 | noop 135 | noop 136 | addx -6 137 | addx 9 138 | addx -18 139 | addx 25 140 | addx 3 141 | noop 142 | addx -17 143 | noop 144 | -------------------------------------------------------------------------------- /Tommimon/d10/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | int score1 = 0; 14 | 15 | int cycles = 1; 16 | int x = 1; 17 | int subCycles = 0; 18 | for (int i = 0; i < lines.size(); ++i) { 19 | string l = lines[i]; 20 | int col = (cycles-1) % 40; 21 | if ((cycles - 20) % 40 == 0) 22 | score1 += cycles * x; 23 | if (col >= x-1 && col <= x+1) 24 | cout << '#'; 25 | else 26 | cout << '.'; 27 | if (col == 39) 28 | cout << endl; 29 | if (l[0] == 'a') { 30 | if (subCycles == 1) { 31 | x += stoi(split(l, "addx ")[1]); 32 | subCycles = 0; 33 | } 34 | else { 35 | i--; 36 | subCycles++; 37 | } 38 | cycles ++; 39 | } 40 | else if (l[0] == 'n'){ 41 | cycles ++; 42 | } 43 | } 44 | 45 | cout << score1 << endl; 46 | } 47 | -------------------------------------------------------------------------------- /Tommimon/d11/input: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 89, 84, 88, 78, 70 3 | Operation: new = old * 5 4 | Test: divisible by 7 5 | If true: throw to monkey 6 6 | If false: throw to monkey 7 7 | 8 | Monkey 1: 9 | Starting items: 76, 62, 61, 54, 69, 60, 85 10 | Operation: new = old + 1 11 | Test: divisible by 17 12 | If true: throw to monkey 0 13 | If false: throw to monkey 6 14 | 15 | Monkey 2: 16 | Starting items: 83, 89, 53 17 | Operation: new = old + 8 18 | Test: divisible by 11 19 | If true: throw to monkey 5 20 | If false: throw to monkey 3 21 | 22 | Monkey 3: 23 | Starting items: 95, 94, 85, 57 24 | Operation: new = old + 4 25 | Test: divisible by 13 26 | If true: throw to monkey 0 27 | If false: throw to monkey 1 28 | 29 | Monkey 4: 30 | Starting items: 82, 98 31 | Operation: new = old + 7 32 | Test: divisible by 19 33 | If true: throw to monkey 5 34 | If false: throw to monkey 2 35 | 36 | Monkey 5: 37 | Starting items: 69 38 | Operation: new = old + 2 39 | Test: divisible by 2 40 | If true: throw to monkey 1 41 | If false: throw to monkey 3 42 | 43 | Monkey 6: 44 | Starting items: 82, 70, 58, 87, 59, 99, 92, 65 45 | Operation: new = old * 11 46 | Test: divisible by 5 47 | If true: throw to monkey 7 48 | If false: throw to monkey 4 49 | 50 | Monkey 7: 51 | Starting items: 91, 53, 96, 98, 68, 82 52 | Operation: new = old * old 53 | Test: divisible by 3 54 | If true: throw to monkey 4 55 | If false: throw to monkey 2 56 | -------------------------------------------------------------------------------- /Tommimon/d11/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "py++.hpp" 6 | 7 | using namespace std; 8 | 9 | typedef struct { 10 | string operation; 11 | int test; 12 | int ifTrue; 13 | int ifFalse; 14 | queue items; 15 | int counter = 0; 16 | } Monkey; 17 | 18 | int main() { 19 | ifstream ifs("input"); 20 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 21 | 22 | vector blocks(split(input, "\n\n")); 23 | 24 | vector monkeys; 25 | monkeys.resize(blocks.size()); 26 | 27 | for (int i = 0; i < blocks.size(); ++i) { 28 | string b = blocks[i]; 29 | vector lines(split(b, "\n")); 30 | vector itemsStrings = split(lines[1].substr(18, lines[1].size()-18), ", "); 31 | for (auto item: itemsStrings) 32 | monkeys[i].items.push(stoi(item)); 33 | 34 | monkeys[i].operation = lines[2].substr(23, lines[2].size()-23); 35 | monkeys[i].test = stoi(lines[3].substr(21, lines[3].size()-21)); 36 | monkeys[i].ifTrue = stoi(lines[4].substr(29, lines[4].size()-29)); 37 | monkeys[i].ifFalse = stoi(lines[5].substr(30, lines[5].size()-30)); 38 | } 39 | 40 | for (int i = 0; i < 20; ++i) { 41 | for (auto &m: monkeys) { 42 | while (!m.items.empty()) { 43 | long int level = m.items.front(); 44 | m.items.pop(); 45 | 46 | string secondString = split(m.operation, " ")[1]; 47 | long int secondNum = level; 48 | if (secondString != "old") 49 | secondNum = stoi(secondString); 50 | if (m.operation[0] == '*') 51 | level *= secondNum; 52 | else 53 | level += secondNum; 54 | 55 | level /= 3; 56 | m.counter++; 57 | 58 | if (level % m.test == 0) 59 | monkeys[m.ifTrue].items.push(level); 60 | else 61 | monkeys[m.ifFalse].items.push(level); 62 | } 63 | } 64 | } 65 | 66 | int max1 = -1; 67 | int max2 = -2; 68 | for (auto m: monkeys) { 69 | if (m.counter > max2) { 70 | if (m.counter > max1) { 71 | max2 = max1; 72 | max1 = m.counter; 73 | } 74 | else 75 | max2 = m.counter; 76 | } 77 | } 78 | 79 | cout << max1 * max2 << endl; 80 | } 81 | -------------------------------------------------------------------------------- /Tommimon/d11/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "py++.hpp" 6 | 7 | using namespace std; 8 | 9 | typedef struct { 10 | string operation; 11 | int test; 12 | int ifTrue; 13 | int ifFalse; 14 | queue items; 15 | int counter = 0; 16 | } Monkey; 17 | 18 | int main() { 19 | ifstream ifs("input"); 20 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 21 | 22 | vector blocks(split(input, "\n\n")); 23 | 24 | vector monkeys; 25 | monkeys.resize(blocks.size()); 26 | 27 | for (int i = 0; i < blocks.size(); ++i) { 28 | string b = blocks[i]; 29 | vector lines(split(b, "\n")); 30 | vector itemsStrings = split(lines[1].substr(18, lines[1].size()-18), ", "); 31 | for (auto item: itemsStrings) 32 | monkeys[i].items.push(stoi(item)); 33 | 34 | monkeys[i].operation = lines[2].substr(23, lines[2].size()-23); 35 | monkeys[i].test = stoi(lines[3].substr(21, lines[3].size()-21)); 36 | monkeys[i].ifTrue = stoi(lines[4].substr(29, lines[4].size()-29)); 37 | monkeys[i].ifFalse = stoi(lines[5].substr(30, lines[5].size()-30)); 38 | } 39 | 40 | int divisor = 1; 41 | for (auto &m: monkeys) 42 | divisor *= m.test; 43 | 44 | for (int i = 0; i < 10000; ++i) { 45 | for (auto &m: monkeys) { 46 | while (!m.items.empty()) { 47 | long int level = m.items.front(); 48 | m.items.pop(); 49 | 50 | string secondString = split(m.operation, " ")[1]; 51 | long int secondNum = level; 52 | if (secondString != "old") 53 | secondNum = stoi(secondString); 54 | if (m.operation[0] == '*') 55 | level *= secondNum; 56 | else 57 | level += secondNum; 58 | 59 | level = level % divisor; 60 | m.counter++; 61 | 62 | if (level % m.test == 0) { 63 | monkeys[m.ifTrue].items.push(level); 64 | } 65 | else { 66 | monkeys[m.ifFalse].items.push(level); 67 | } 68 | } 69 | } 70 | } 71 | 72 | long int max1 = -1; 73 | long int max2 = -2; 74 | for (auto m: monkeys) { 75 | if (m.counter > max2) { 76 | if (m.counter > max1) { 77 | max2 = max1; 78 | max1 = m.counter; 79 | } 80 | else 81 | max2 = m.counter; 82 | } 83 | } 84 | 85 | cout << max1 * max2 << endl; 86 | } 87 | -------------------------------------------------------------------------------- /Tommimon/d12/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | 14 | int width = lines[0].size(); 15 | int height = lines.size(); 16 | int distance[height][width]; 17 | 18 | for (int i = 0; i < height; ++i) { 19 | for (int j = 0; j < width; ++j) { 20 | if (lines[i][j] == 'E') { 21 | distance[i][j] = 0; 22 | lines[i][j] = 'Z'; 23 | } 24 | else 25 | distance[i][j] = -1; 26 | 27 | if (lines[i][j] == 'S') 28 | lines[i][j] = 'A'; 29 | } 30 | } 31 | 32 | 33 | int result1 = -1; 34 | int result2 = -1; 35 | int step = 0; 36 | while (result1 == -1) { 37 | for (int i = 0; i < height; ++i) { 38 | for (int j = 0; j < width; ++j) { 39 | if (distance[i][j] == step) { 40 | if (lines[i][j] == 'A') 41 | result1 = step; 42 | if (tolower(lines[i][j]) == 'a' && result2 == -1) 43 | result2 = step; 44 | if (i > 0 && distance[i-1][j] == -1 && tolower(lines[i-1][j]) >= tolower(lines[i][j]-1)) 45 | distance[i-1][j] = step+1; 46 | if (j > 0 && distance[i][j-1] == -1 && tolower(lines[i][j-1]) >= tolower(lines[i][j]-1)) 47 | distance[i][j-1] = step+1; 48 | if (i < height - 1 && distance[i + 1][j] == -1 && tolower(lines[i+1][j]) >= tolower(lines[i][j]-1)) 49 | distance[i+1][j] = step+1; 50 | if (j < width - 1 && distance[i][j + 1] == -1 && tolower(lines[i][j+1]) >= tolower(lines[i][j]-1)) 51 | distance[i][j+1] = step+1; 52 | } 53 | } 54 | } 55 | step++; 56 | } 57 | 58 | cout << result1 << endl; 59 | cout << result2 << endl; 60 | } 61 | -------------------------------------------------------------------------------- /Tommimon/d13/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "py++.hpp" 6 | 7 | using namespace std; 8 | 9 | int compareArrays(char** string1, char** string2, int* counter) { 10 | int depth = 0; 11 | do { 12 | if (**string1 != **string2) { 13 | if (**string1 == ']') 14 | return 1; 15 | if (**string2 == ']') 16 | return -1; 17 | if (**string1 == '[') { 18 | char alias[strlen(*string2)+3]; 19 | alias[0] = '['; 20 | alias[1] = **string2; 21 | alias[2] = ']'; 22 | strcpy(alias+3, (*string2)+1); 23 | char* pAlias = alias; 24 | int subCounter = 0; 25 | int res = compareArrays(string1, &pAlias, &subCounter); 26 | if (res == 1) 27 | return 1; 28 | if (res == -1) 29 | return -1; 30 | subCounter -= 2; 31 | (*string2) += subCounter; 32 | } 33 | else if (**string2 == '[') { 34 | char alias[strlen(*string1)+3]; 35 | alias[0] = '['; 36 | alias[1] = **string1; 37 | alias[2] = ']'; 38 | strcpy(alias+3, (*string1)+1); 39 | char* pAlias = alias; 40 | int subCounter = 0; 41 | int res = compareArrays(&pAlias, string2, &subCounter); 42 | if (res == 1) 43 | return 1; 44 | if (res == -1) 45 | return -1; 46 | subCounter -= 2; 47 | (*string1) += subCounter; 48 | } 49 | else { 50 | return **string1 < **string2 ? 1 : -1; 51 | } 52 | } 53 | 54 | if (**string1 == '[' || **string2 == '[') 55 | depth++; 56 | if (**string1 == ']' || **string2 == ']') 57 | depth--; 58 | 59 | (*string1)++; 60 | (*string2)++; 61 | (*counter)++; 62 | } while (depth > 0); 63 | return 0; 64 | } 65 | 66 | bool compareStrings(const string& s1, const string& s2) { 67 | char buffer1[500], buffer2[500]; 68 | strcpy(buffer1, s1.c_str()); 69 | strcpy(buffer2, s2.c_str()); 70 | char *p1 = buffer1, *p2 = buffer2; 71 | int counter = 0; 72 | return compareArrays(&p1, &p2, &counter) != -1; 73 | } 74 | 75 | int main() { 76 | ifstream ifs("input"); 77 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 78 | input = replace(input, "10", "A"); 79 | 80 | vector packets(split(input, "\n\n")); 81 | int score1 = 0; 82 | for (int i=0; i lines(split(input,"\n")); 89 | std::sort(lines.begin(), lines.end(), [](const string &s1, const string &s2) {return compareStrings(s1, s2);}); 90 | auto pos1 = std::find_if(lines.begin(), lines.end(), [&](const auto &item) {return item == "[[2]]";}); 91 | auto pos2 = std::find_if(lines.begin(), lines.end(), [&](const auto &item) {return item == "[[6]]";}); 92 | cout << (pos1 - lines.begin() + 1) * (pos2 - lines.begin() + 1) << endl; 93 | } 94 | -------------------------------------------------------------------------------- /Tommimon/d14/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | ifstream ifs("input"); 10 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 11 | 12 | vector lines(split(input, "\n")); 13 | int maxY = 0; 14 | for (auto l: lines) { 15 | for (int i = 0; i < l.size(); ++i) { 16 | if (l[i] == ',') { 17 | string yStr = split(l.substr(i+1, l.size()-i-1), " ")[0]; 18 | int y = stoi(yStr); 19 | if (y > maxY) 20 | maxY = y; 21 | } 22 | } 23 | } 24 | lines.push_back("0," + to_string(maxY+2) + " -> 999," + to_string(maxY+2)); 25 | 26 | int matrix_size = 1000; 27 | char matrix[matrix_size][matrix_size]; 28 | for (int i = 0; i < matrix_size; ++i) { 29 | for (int j = 0; j < matrix_size; ++j) { 30 | matrix[i][j] = '.'; 31 | } 32 | } 33 | 34 | for (auto l : lines) { 35 | vector points = split(l, " -> "); 36 | for (int j = 0; j < points.size() - 1; ++j) { 37 | int startX = stoi(split(points[j], ",")[0]); 38 | int startY = stoi(split(points[j], ",")[1]); 39 | int endX = stoi(split(points[j+1], ",")[0]); 40 | int endY = stoi(split(points[j+1], ",")[1]); 41 | if (startX > endX) { 42 | int t = startX; 43 | startX = endX; 44 | endX = t; 45 | } 46 | if (startY > endY) { 47 | int t = startY; 48 | startY = endY; 49 | endY = t; 50 | } 51 | for (int x = startX; x <= endX; ++x) { 52 | for (int y = startY; y <= endY; ++y) { 53 | matrix[x][y] = '#'; 54 | } 55 | } 56 | } 57 | } 58 | 59 | int score = 0; 60 | bool blocked = false; 61 | bool dropped = false; 62 | while (matrix[500][0] == '.') { 63 | int x = 500, y = -1; 64 | while (!blocked) { 65 | if (matrix[x][y+1] == '.') 66 | y++; 67 | else if (matrix[x-1][y+1] == '.') { 68 | y++; 69 | x--; 70 | } 71 | else if (matrix[x+1][y+1] == '.') { 72 | y++; 73 | x++; 74 | } 75 | else { 76 | blocked = true; 77 | } 78 | if (!dropped && y > maxY) { 79 | dropped = true; 80 | cout << score << endl; 81 | } 82 | } 83 | blocked = false; 84 | matrix[x][y] = 'o'; 85 | score++; 86 | } 87 | 88 | cout << score << endl; 89 | } 90 | -------------------------------------------------------------------------------- /Tommimon/d15/input: -------------------------------------------------------------------------------- 1 | Sensor at x=2332081, y=2640840: closest beacon is at x=2094728, y=2887414 2 | Sensor at x=3048293, y=3598671: closest beacon is at x=3872908, y=3598272 3 | Sensor at x=2574256, y=3973583: closest beacon is at x=2520711, y=4005929 4 | Sensor at x=3011471, y=2514567: closest beacon is at x=2999559, y=2558817 5 | Sensor at x=3718881, y=2593817: closest beacon is at x=2999559, y=2558817 6 | Sensor at x=2388052, y=2201955: closest beacon is at x=2163809, y=1961540 7 | Sensor at x=3783125, y=3897169: closest beacon is at x=3872908, y=3598272 8 | Sensor at x=1864613, y=3918152: closest beacon is at x=2520711, y=4005929 9 | Sensor at x=2850099, y=689863: closest beacon is at x=3231146, y=2000000 10 | Sensor at x=3431652, y=2328669: closest beacon is at x=3231146, y=2000000 11 | Sensor at x=3480248, y=3999492: closest beacon is at x=3872908, y=3598272 12 | Sensor at x=455409, y=3347614: closest beacon is at x=-399822, y=4026621 13 | Sensor at x=2451938, y=2950107: closest beacon is at x=2094728, y=2887414 14 | Sensor at x=1917790, y=3194437: closest beacon is at x=2094728, y=2887414 15 | Sensor at x=3947393, y=3625984: closest beacon is at x=3872908, y=3598272 16 | Sensor at x=1615064, y=2655330: closest beacon is at x=2094728, y=2887414 17 | Sensor at x=3630338, y=1977851: closest beacon is at x=3231146, y=2000000 18 | Sensor at x=3878266, y=3019867: closest beacon is at x=3872908, y=3598272 19 | Sensor at x=2837803, y=2395749: closest beacon is at x=2999559, y=2558817 20 | Sensor at x=3979396, y=3697962: closest beacon is at x=3872908, y=3598272 21 | Sensor at x=109399, y=250528: closest beacon is at x=929496, y=-688981 22 | Sensor at x=2401381, y=3518884: closest beacon is at x=2520711, y=4005929 23 | Sensor at x=3962391, y=71053: closest beacon is at x=5368730, y=-488735 24 | Sensor at x=1751119, y=97658: closest beacon is at x=929496, y=-688981 25 | Sensor at x=2932155, y=2967347: closest beacon is at x=2999559, y=2558817 26 | Sensor at x=3326630, y=2845463: closest beacon is at x=2999559, y=2558817 27 | Sensor at x=3959042, y=1734156: closest beacon is at x=3231146, y=2000000 28 | Sensor at x=675279, y=1463916: closest beacon is at x=2163809, y=1961540 29 | Sensor at x=3989603, y=3500749: closest beacon is at x=3872908, y=3598272 30 | Sensor at x=1963470, y=2288355: closest beacon is at x=2163809, y=1961540 31 | -------------------------------------------------------------------------------- /Tommimon/d15/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | class Sensor { 9 | public: 10 | int sX, sY, bX, bY, dist; 11 | explicit Sensor(string &line) { 12 | sX = stoi(split(split(line, "x=")[1], ",")[0]); 13 | sY = stoi(split(split(line, "y=")[1], ":")[0]); 14 | bX = stoi(split(split(line, "x=")[2], ",")[0]); 15 | bY = stoi(split(split(line, "y=")[2], " ")[0]); 16 | dist = abs(sX-bX) + abs(sY-bY); 17 | } 18 | }; 19 | 20 | int main() { 21 | ifstream ifs("input"); 22 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 23 | 24 | int startY = 2000000; 25 | vector lines(split(input, "\n")); 26 | vector sensors = map([](string s) {return Sensor(s);}, lines); 27 | 28 | int minX = 9999999; 29 | int maxX = 0; 30 | for (auto s: sensors) { 31 | int beginX = s.sX - (s.dist - abs(startY-s.sY)) - 1; 32 | int endX = s.sX + (s.dist - abs(startY-s.sY)) + 1; 33 | if (beginX < minX) 34 | minX = beginX; 35 | if (endX > maxX) 36 | maxX = endX; 37 | } 38 | 39 | int score1 = 0; 40 | int y = startY; 41 | for (int x = minX; x <= maxX; x++) { 42 | bool can = true; 43 | for (auto s: sensors) { 44 | if (x == s.bX && y == s.bY) { 45 | can = true; 46 | break; 47 | } 48 | if (abs(x-s.sX) + abs(y-s.sY) <= s.dist) 49 | can = false; 50 | } 51 | if (!can) 52 | score1++; 53 | } 54 | 55 | cout << score1 << endl; 56 | } 57 | -------------------------------------------------------------------------------- /Tommimon/d15/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | 8 | class Sensor { 9 | public: 10 | int sX, sY, bX, bY, dist; 11 | explicit Sensor(string &line) { 12 | sX = stoi(split(split(line, "x=")[1], ",")[0]); 13 | sY = stoi(split(split(line, "y=")[1], ":")[0]); 14 | bX = stoi(split(split(line, "x=")[2], ",")[0]); 15 | bY = stoi(split(split(line, "y=")[2], " ")[0]); 16 | dist = abs(sX-bX) + abs(sY-bY); 17 | } 18 | }; 19 | 20 | int main() { 21 | ifstream ifs("input"); 22 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 23 | 24 | int startY = 2000000; 25 | vector lines(split(input, "\n")); 26 | vector sensors = map([](string s) {return Sensor(s);}, lines); 27 | 28 | int maxX = 4000000; 29 | int maxY = 4000000; 30 | for (int y = 0; y <= maxY; ++y) { 31 | for (int x = 0; x <= maxX; x++) { 32 | bool can = true; 33 | for (auto s: sensors) { 34 | if (abs(x - s.sX) + abs(y - s.sY) <= s.dist) { 35 | can = false; 36 | x = s.sX + (s.dist - abs(y-s.sY)); 37 | break; 38 | } 39 | } 40 | if (can) 41 | cout << (long) x * 4000000 + y << endl; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Tommimon/d16/input: -------------------------------------------------------------------------------- 1 | Valve OS has flow rate=0; tunnels lead to valves EE, CL 2 | Valve EN has flow rate=0; tunnels lead to valves CL, GV 3 | Valve RR has flow rate=24; tunnels lead to valves FS, YP 4 | Valve VB has flow rate=20; tunnels lead to valves UU, EY, SG, ZB 5 | Valve UU has flow rate=0; tunnels lead to valves OT, VB 6 | Valve WH has flow rate=0; tunnels lead to valves CS, JS 7 | Valve OF has flow rate=25; tunnel leads to valve YM 8 | Valve TY has flow rate=0; tunnels lead to valves AA, GQ 9 | Valve RV has flow rate=0; tunnels lead to valves BT, YX 10 | Valve GK has flow rate=0; tunnels lead to valves GD, AA 11 | Valve EL has flow rate=0; tunnels lead to valves EK, EE 12 | Valve OT has flow rate=9; tunnels lead to valves YR, BJ, OX, UU, HJ 13 | Valve DG has flow rate=11; tunnels lead to valves BN, QE 14 | Valve YR has flow rate=0; tunnels lead to valves OT, YX 15 | Valve GV has flow rate=0; tunnels lead to valves AA, EN 16 | Valve BN has flow rate=0; tunnels lead to valves DG, LU 17 | Valve FS has flow rate=0; tunnels lead to valves TI, RR 18 | Valve DW has flow rate=0; tunnels lead to valves SS, MS 19 | Valve DJ has flow rate=0; tunnels lead to valves KY, GD 20 | Valve BJ has flow rate=0; tunnels lead to valves OT, BT 21 | Valve KY has flow rate=0; tunnels lead to valves EE, DJ 22 | Valve YP has flow rate=0; tunnels lead to valves YM, RR 23 | Valve LU has flow rate=0; tunnels lead to valves BN, CS 24 | Valve OX has flow rate=0; tunnels lead to valves OT, XD 25 | Valve ZB has flow rate=0; tunnels lead to valves VB, PP 26 | Valve CL has flow rate=10; tunnels lead to valves KQ, EN, OS, MQ 27 | Valve XD has flow rate=0; tunnels lead to valves KR, OX 28 | Valve YM has flow rate=0; tunnels lead to valves OF, YP 29 | Valve EY has flow rate=0; tunnels lead to valves MS, VB 30 | Valve KQ has flow rate=0; tunnels lead to valves CS, CL 31 | Valve SS has flow rate=0; tunnels lead to valves AA, DW 32 | Valve SG has flow rate=0; tunnels lead to valves VB, KR 33 | Valve EE has flow rate=22; tunnels lead to valves XR, OS, KY, EL 34 | Valve OI has flow rate=0; tunnels lead to valves RE, MS 35 | Valve QE has flow rate=0; tunnels lead to valves DG, GD 36 | Valve GD has flow rate=3; tunnels lead to valves GK, DJ, MQ, QE, JS 37 | Valve EK has flow rate=23; tunnel leads to valve EL 38 | Valve GQ has flow rate=0; tunnels lead to valves CS, TY 39 | Valve CS has flow rate=7; tunnels lead to valves GQ, WH, KQ, LU 40 | Valve MS has flow rate=4; tunnels lead to valves HJ, EY, DW, OI 41 | Valve XR has flow rate=0; tunnels lead to valves EE, AA 42 | Valve RE has flow rate=6; tunnels lead to valves TI, PP, OI 43 | Valve KR has flow rate=17; tunnels lead to valves XD, SG 44 | Valve BT has flow rate=15; tunnels lead to valves BJ, RV 45 | Valve PP has flow rate=0; tunnels lead to valves RE, ZB 46 | Valve TI has flow rate=0; tunnels lead to valves RE, FS 47 | Valve HJ has flow rate=0; tunnels lead to valves OT, MS 48 | Valve AA has flow rate=0; tunnels lead to valves GK, GV, SS, XR, TY 49 | Valve MQ has flow rate=0; tunnels lead to valves GD, CL 50 | Valve JS has flow rate=0; tunnels lead to valves GD, WH 51 | Valve YX has flow rate=5; tunnels lead to valves YR, RV 52 | -------------------------------------------------------------------------------- /Tommimon/d17/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | #define ROCK_NUM 2022 10 | 11 | const int h = ROCK_NUM*100+10; 12 | 13 | bool collision(char grid[h][7], vector rock, int x, int y) { 14 | for (int i = 0; i < rock.size(); ++i) { 15 | for (int j = 0; j < rock[i].length(); ++j) { 16 | if (x + j < 0 || x + j >= 7 || y + i < 0 || y + i >= h) 17 | return true; 18 | if (rock[i][j] == '#' && grid[y + i][x + j] == '#') 19 | return true; 20 | } 21 | } 22 | return false; 23 | } 24 | 25 | int main() { 26 | ifstream ifs("input"); 27 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 28 | input = replace(input, "\n", ""); 29 | 30 | vector> rocks_list = {{"####"}, {".#.", "###", ".#."}, {"..#", "..#", "###"}, {"#", "#", "#", "#"}, {"##", "##"}}; 31 | 32 | vector history; 33 | 34 | char grid[h][7]; 35 | for (auto & r : grid) 36 | for (char & e : r) 37 | e = '.'; 38 | 39 | int top = h; 40 | int j = 0; 41 | long int target = 1000000000000; 42 | long int bias = 0; 43 | int streak = 0; 44 | int streak_offset = -1; 45 | for (int i = 0; i < ROCK_NUM || i < target; ++i) { 46 | vector rock = rocks_list[i % rocks_list.size()]; 47 | 48 | bool falling = true; 49 | int x = 2; 50 | int y = top - 3 - rock.size(); 51 | if (y < 0) { 52 | printf("Grid overflow"); 53 | return 0; 54 | } 55 | while (falling) { 56 | x += input[j] == '>' ? 1 : -1; 57 | if (collision(grid, rock, x, y)) 58 | x -= input[j] == '>' ? 1 : -1; 59 | y++; 60 | if (collision(grid, rock, x, y)) { 61 | y--; 62 | 63 | if (bias == 0) { 64 | string record = 65 | "i" + to_string(i % rocks_list.size()) + "j" + to_string(j) + "x" + to_string(x) + "y" + 66 | to_string(top - y) + "t" + 67 | to_string(top); 68 | for (int k = 0; k < history.size(); ++k) { 69 | if (split(history[k], "t")[0] == split(record, "t")[0]) { 70 | bool inStreak = true; 71 | for (int l = 1; l < 10; ++l) { 72 | if (split(history[k-l], "t")[0] != split(history[i-l], "t")[0]) { 73 | inStreak = false; 74 | break; 75 | } 76 | } 77 | if (inStreak) { 78 | int deltaI = i - k; 79 | int deltaT = stoi(split(history[k], "t")[1]) - top; 80 | long int q = (target - i) / deltaI - 1; 81 | target = target - (deltaI * q); 82 | bias = deltaT * q; 83 | } 84 | } 85 | } 86 | history.push_back(record); 87 | } 88 | 89 | if (y < top) 90 | top = y; 91 | falling = false; 92 | for (int k = 0; k < rock.size(); ++k) 93 | for (int l = 0; l < rock[k].length(); ++l) 94 | if (rock[k][l] == '#') 95 | grid[y + k][x + l] = '#'; 96 | } 97 | j = (j+1) % input.length(); 98 | } 99 | if (i == ROCK_NUM-1) 100 | cout << h-top << endl; 101 | if (i == target-1) 102 | cout << bias+h-top << endl; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Tommimon/d18/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | int main() { 10 | ifstream ifs("input"); 11 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 12 | 13 | vector lines(split(input, "\n")); 14 | int res = lines.size() * 6; 15 | 16 | for (const auto& a: lines) { 17 | for (const auto& b: lines) { 18 | vector coordsA = split(a, ","); 19 | vector coordsB = split(b, ","); 20 | int equal = 0; 21 | int close = 0; 22 | for (int i = 0; i < 3; ++i) { 23 | if (coordsA[i] == coordsB[i]) 24 | equal++; 25 | if (abs(stoi(coordsA[i]) - stoi(coordsB[i])) == 1) 26 | close++; 27 | } 28 | if (equal == 2 && close == 1) 29 | res--; 30 | } 31 | } 32 | 33 | cout << res << endl; 34 | } 35 | -------------------------------------------------------------------------------- /Tommimon/d18/part2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | #define GRID_SIZE 25 10 | 11 | #define AIR 1 12 | #define ROCK 2 13 | #define SHIFT 1 14 | 15 | int countExtern (int startX, int startY, int startZ, int grid[GRID_SIZE][GRID_SIZE][GRID_SIZE]) { 16 | vector> close = { 17 | {0, 0, 1}, 18 | {0, 0, -1}, 19 | {0, 1, 0}, 20 | {0, -1, 0}, 21 | {1, 0, 0}, 22 | {-1, 0, 0}, 23 | }; 24 | int count = 0; 25 | 26 | for (auto c: close) { 27 | int x = startX + c[0]; 28 | int y = startY + c[1]; 29 | int z = startZ + c[2]; 30 | 31 | if (x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE && z >= 0 && z < GRID_SIZE) { 32 | if (grid[x][y][z] > 0) { 33 | if (grid[x][y][z] == ROCK) { 34 | count++; 35 | } else { 36 | grid[x][y][z] *= -1; 37 | count += countExtern(x, y, z, grid); 38 | } 39 | } 40 | } 41 | } 42 | 43 | return count; 44 | } 45 | 46 | 47 | int main() { 48 | ifstream ifs("input"); 49 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 50 | int grid[GRID_SIZE][GRID_SIZE][GRID_SIZE]; 51 | 52 | for (auto & i : grid) 53 | for (auto & j : i) 54 | for (int & k : j) 55 | k = AIR; 56 | 57 | vector lines(split(input, "\n")); 58 | 59 | for (const auto& l: lines) { 60 | vector coords = split(l, ","); 61 | grid[stoi(coords[0])+SHIFT][stoi(coords[1])+SHIFT][stoi(coords[2])+SHIFT] = ROCK; 62 | } 63 | 64 | int res = countExtern(0, 0, 0, grid); 65 | 66 | cout << res << endl; 67 | } 68 | -------------------------------------------------------------------------------- /Tommimon/d19/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | #define SIZE 4 10 | #define GEODE 3 11 | 12 | int mostGeodes(const vector>& costs, vector robots, const vector& res, int timeLeft, vector& needs) { 13 | int max = 0, geodes; 14 | for (int choice = SIZE-1; choice >= -1; --choice) { 15 | geodes = robots[GEODE] * timeLeft; 16 | if (choice >= 0) { 17 | if (!needs[choice]) continue; 18 | int timeNeeded = 1; 19 | int missing = -1; 20 | for (int i = 0; i < SIZE; ++i) { 21 | int wait = robots[i] == 0 ? timeLeft * costs[choice][i] : (costs[choice][i] - res[i] + (robots[i]-1)) / robots[i] + 1; 22 | if (wait > timeNeeded) { 23 | timeNeeded = wait; 24 | missing = i; 25 | } 26 | } 27 | if (missing != -1) needs[missing] = true; 28 | if (timeNeeded >= timeLeft) continue; 29 | vector newRes; 30 | for (int i = 0; i < SIZE; ++i) 31 | newRes.push_back(res[i] + robots[i] * timeNeeded - costs[choice][i]); 32 | geodes = newRes[GEODE] - res[GEODE]; 33 | vector childNeeds = {false, false, false, true}; 34 | robots[choice]++; 35 | geodes += mostGeodes(costs, robots, newRes, timeLeft-timeNeeded, childNeeds); 36 | robots[choice]--; 37 | for (int i = 0; i < SIZE; ++i) 38 | if (childNeeds[i]) needs[i] = true; 39 | } 40 | if (geodes > max) max = geodes; 41 | } 42 | return max; 43 | } 44 | 45 | int main() { 46 | ifstream ifs("input"); 47 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 48 | 49 | vector>> costs; 50 | vector lines(split(input, "\n")); 51 | int score1 = 0; 52 | int score2 = 1; 53 | 54 | for (int i = 0; i < lines.size(); ++i) { 55 | string l = lines[i]; 56 | costs.emplace_back(); 57 | costs[i].push_back({stoi(split(split(l, "costs ")[1], " ")[0]), 0, 0, 0}); 58 | costs[i].push_back({stoi(split(split(l, "costs ")[2], " ")[0]), 0, 0, 0}); 59 | costs[i].push_back({stoi(split(split(l, "costs ")[3], " ")[0]), stoi(split(split(l, "and ")[1], " ")[0]), 0, 0}); 60 | costs[i].push_back({stoi(split(split(l, "costs ")[4], " ")[0]), 0, stoi(split(split(l, "and ")[2], " ")[0]), 0}); 61 | } 62 | 63 | for (int i = 0; i < costs.size(); ++i) { 64 | vector robots = {1, 0, 0, 0}; 65 | vector res = {0, 0, 0, 0}; 66 | vector needs = {false, false, false, true}; 67 | score1 += (i+1) * mostGeodes(costs[i], robots, res, 24, needs); 68 | } 69 | 70 | for (int i = 0; i < 3; ++i) { 71 | vector robots = {1, 0, 0, 0}; 72 | vector res = {0, 0, 0, 0}; 73 | vector needs = {false, false, false, true}; 74 | score2 *= mostGeodes(costs[i], robots, res, 32, needs); 75 | } 76 | 77 | cout << score1 << endl; 78 | cout << score2 << endl; 79 | } 80 | -------------------------------------------------------------------------------- /Tommimon/d20/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | #define KEY 811589153L 10 | 11 | long int list_size; 12 | 13 | long int pos(long int n) { 14 | if (n >= 0) 15 | return (n-1) % (list_size-1) + 1; 16 | return (n-list_size+2) % (list_size-1) + list_size - 2; 17 | } 18 | 19 | void mix(vector>& list) { 20 | for (int i = 0; i < list.size(); ++i) { 21 | long int start = list[i].second; 22 | long int end = pos(list[i].second + list[i].first); 23 | for (auto& e: list) { 24 | if (start < e.second && e.second <= end) 25 | e.second = pos(e.second - 1); 26 | if (end <= e.second && e.second < start) 27 | e.second = pos(e.second + 1); 28 | } 29 | list[i].second = end; 30 | } 31 | } 32 | 33 | long int coordinates(const vector> & list) { 34 | long int score = 0; 35 | long int zero; 36 | for (auto e: list) 37 | if (e.first == 0) 38 | zero = e.second; 39 | 40 | for (auto e: list) 41 | if (e.second == (zero + 1000)%list_size || e.second == (zero + 2000)%list_size || e.second == (zero + 3000)%list_size) 42 | score += e.first; 43 | 44 | return score; 45 | } 46 | 47 | int main() { 48 | ifstream ifs("input"); 49 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 50 | 51 | vector lines(split(input, "\n")); 52 | list_size = lines.size(); 53 | vector> list1; 54 | vector> list2; 55 | 56 | for (int i = 0; i < lines.size(); ++i) { 57 | list1.emplace_back(stoi(lines[i]), i); 58 | list2.emplace_back(KEY * stoi(lines[i]), i); 59 | } 60 | 61 | mix(list1); 62 | for (int i = 0; i < 10; ++i) { 63 | mix(list2); 64 | } 65 | 66 | cout << coordinates(list1) << endl; 67 | cout << coordinates(list2) << endl; 68 | } 69 | -------------------------------------------------------------------------------- /Tommimon/d21/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "py++.hpp" 6 | 7 | using namespace std; 8 | using namespace py; 9 | 10 | long int solve(const std::map& monkeys, const string& name, bool raise = false) { 11 | if (raise && name == "humn") 12 | throw std::invalid_argument( "can't process human expression" ); 13 | string expr = monkeys.find(name)->second; 14 | if (expr.length() == 11) { 15 | long int a = solve(monkeys, split(expr, " ")[0], raise); 16 | long int b = solve(monkeys, split(expr, " ")[2], raise); 17 | char op = expr[5]; 18 | if (op == '+') return a + b; 19 | if (op == '-') return a - b; 20 | if (op == '*') return a * b; 21 | if (op == '/') return a / b; 22 | } 23 | return stoi(expr); 24 | } 25 | 26 | long int require(const std::map& monkeys, const string& name, long int val) { 27 | if (name == "humn") 28 | return val; 29 | string expr = monkeys.find(name)->second; 30 | char op = expr[5]; 31 | string missing; 32 | long int required; 33 | try { 34 | long int a = solve(monkeys, split(expr, " ")[0], true); 35 | missing = split(expr, " ")[2]; 36 | if (op == '+') required = val - a; 37 | if (op == '-') required = a - val; 38 | if (op == '*') required = val / a; 39 | if (op == '/') required = a / val; 40 | } catch( const std::invalid_argument& e ) { 41 | long int b = solve(monkeys, split(expr, " ")[2], true); 42 | missing = split(expr, " ")[0]; 43 | if (op == '+') required = val - b; 44 | if (op == '-') required = val + b; 45 | if (op == '*') required = val / b; 46 | if (op == '/') required = val * b; 47 | } 48 | return require(monkeys, missing, required); 49 | } 50 | 51 | int main() { 52 | ifstream ifs("input"); 53 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 54 | 55 | vector lines(split(input, "\n")); 56 | std::map monkeys; 57 | 58 | for (const auto& l : lines) { 59 | monkeys.insert({split(l, ": ")[0], split(l, ": ")[1]}); 60 | } 61 | 62 | cout << solve(monkeys, "root") << endl; 63 | 64 | monkeys.find("root")->second[5] = '-'; 65 | 66 | cout << require(monkeys, "root", 0) << endl; 67 | } 68 | -------------------------------------------------------------------------------- /Tommimon/d22/part1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | int jump(vector> board, int i, int j, int dir) { 10 | while (j >= board[i].second && j < board[i].first.size() + board[i].second) 11 | i += dir; 12 | return i - dir; 13 | } 14 | 15 | int main() { 16 | ifstream ifs("input"); 17 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 18 | 19 | string mapInput = split(input, "\n\n")[0]; 20 | string directions = split(input, "\n\n")[1]; 21 | vector lines(split(mapInput, "\n")); 22 | 23 | vector> board = py::map>([](const string& s){ 24 | int spaces = 0; 25 | for (auto i : s) { 26 | if (i != ' ') 27 | break; 28 | spaces++; 29 | } 30 | return make_pair(s.substr(spaces, s.size()-spaces), spaces); 31 | }, lines); 32 | 33 | vector instructions = {""}; 34 | for (auto c: directions) { 35 | if (c == 'R' || c == 'L') { 36 | string s; 37 | s += c; 38 | instructions.push_back(s); 39 | instructions.emplace_back(""); 40 | } 41 | else if (c != '\n') 42 | instructions[instructions.size()-1] += c; 43 | } 44 | 45 | int i = 0, j = board[i].second, dir = 0; 46 | for (const auto& s: instructions) { 47 | if (s == "R") 48 | dir = (dir + 1) % 4; 49 | else if (s == "L") 50 | dir = dir == 0 ? 3 : (dir - 1); 51 | else { 52 | int delta = stoi(s); 53 | for (int k = 0; k < delta; ++k) { 54 | if (dir % 2 == 0) { 55 | int newJ = j + (dir >= 2 ? -1 : 1); 56 | if (newJ == board[i].first.size() + board[i].second) 57 | newJ = board[i].second; 58 | if (newJ == board[i].second-1) 59 | newJ = board[i].first.size() + board[i].second - 1; 60 | if (board[i].first[newJ - board[i].second] == '.') 61 | j = newJ; 62 | } 63 | else { 64 | int newI = i + (dir >= 2 ? -1 : 1); 65 | if (newI == -1) 66 | newI = jump(board, i, j, 1); 67 | if (newI == board.size()) 68 | newI = jump(board, i, j, -1); 69 | if (j < board[newI].second || j >= board[newI].first.size() + board[newI].second) 70 | newI = jump(board, i, j, (dir >= 2 ? 1 : -1)); 71 | if (board[newI].first[j - board[newI].second] == '.') 72 | i = newI; 73 | } 74 | } 75 | } 76 | } 77 | 78 | cout << (i+1) * 1000 + (j+1) * 4 + dir << endl; 79 | } 80 | -------------------------------------------------------------------------------- /Tommimon/d24/input: -------------------------------------------------------------------------------- 1 | #.######################################################################################################################## 2 | #><..<.<<^v><<^>>>^^^..^<<^<<^^<..<>^.<>^v>>>v^><.>.<<>>>^v<>vv^v^vvv>vv<<^<>>^^<><^^><.^v>.^>.<# 3 | #>>v<<>v.^>^<<<.<^.<<^<.^v^<^^>vv^<>^>>><<^v^^<v^><...^<<<><>>^v>^>><^<<.^v^>v>>>^>^v^.^v><<# 4 | #>^^<>^<<^>vv.>^.^vv<.^>>.v..^^^^.>.<<>.^^>^.>^.^^^..>>><^<^>><^<<<><.^v<^v.>^>>>v.^v<# 5 | #.>v<<<.vv^v>^v<.<^^>^.v>>v.<>v<>.^.v^v>^<>>>>vv>v<><<<<><>>>><<.v^>.v^vv^>^v<><<^># 6 | #^<v^>v<<><^vvv<<^.>vvv^>.vv^<^><<<<><^..v>vv<^>>^>.v^>..v>>.<^v^>.>>>>>v..^<<^<.vv<<># 7 | #<>>v>^<.^^^^<><<^^>^^.^^^^v<^^<>>.>vv^^v>v>vv^vv<.^.>v<<>^>^>^vv^>>v>v<^>^<^.^.^<.v>v.^<<<<>v.<<<..>.<>>^.^vv^v<^^v.vv<>>vvvv>.^^^vv<^v^v<.>^.^v<^vv><><<^^.<>.# 9 | #.<><>^^<^v^^>v<><><>^.<^<<.<^vvv^^.<.^.^<.^^><<.>><>>>>^^<>v>..v>^>vv.>v^>>v.>^..<>..vv>^>><^.vv^><>^^^^vvv^v^^<<# 10 | #<^>.<^^<^v<>^<^.>>>>^<.>vv^.v^<>v>^<vvvv<>^<<<<.v.v><<.^v^.<^<<^>>^<^^<^^^^vv^v^<# 11 | #>.>.v><<^>v^><.^<<>^v>>.<^^^<<<><<<^<^^v<.>^vv.>^vvv^v<vv<<.^v^vv>.v^v>^>>^>>^v^^^.><^v><>.>v^<<# 12 | #>>>>vv><^>^<>><^^^^>>><>>vvv<^.<^v>^.<vv^<^.^^^<.^vv..<<>><^^^^<^^v^>^>>v><><<.<# 13 | #>v>><>.v>^><<^.>>>vvv^<.v<>vv^<^^<v^<.^v>.v>^>^.v^.>><..^^^^<>^^.^.>vv^># 14 | #<>^^vvvv^><>^vv<><.<<<>^.<.v^<>v<^^v>^^v>^>^v<<.>.<>.^v.<^<<.^v^^><><^>>>.<<<>^vvv<^.><.# 15 | #<<^<>v>v^v.v>..^^<>vv<<^v>^<.^^.vv<.<.<<<<><..>v><>>>v.v<^^>v>>^v<^<^^>v>>>v^>^v><><<^<^.vvvv>>^>^..># 16 | #<><>^^.<^^.^v.v<>^^vvv<<<<^>^<><.vvv.^<.>^>.>.^<<<<<^^<^.^.^>.^>.>>vvv<.<>^><>><^<>.<^<# 17 | #<^^^v<>>>vvv><^<<<>^^^.<<.vv><><^<>>^><..>^<<>>>>^>v^.>.>>^^vv<>v.^^vv<>>^<^>v>vv><<>><^># 18 | #>v.<<><^<>.>><>><.^^v>^^v^>>vv>vv>>><^...^vv^v^.>vv..<^<>v.v^^^^v>^^<^<<^v>^>.v^>.<^<<>^^.<<<>^<^># 19 | #><^^>>^<>^<>>vv>^.^vvv^<>^^^<v<^>v<<^v^><^v<>v^.^><>v<^vv>v<>v<><>^>>.<^>^>.>^.<^<^vvv.v^v^^>.>v>.^<># 20 | #<<<^><>v^>v>.<.>v<^>>^^vv<^vv^^^<<<<>><<^<<^v>v.>..v>v<>vv.>v^^^v<^^^vvvvv^<.>vv^^.vv>v<.^^<<<^v^># 21 | #<<v^<^.^>.>>^^^v^<>>.^^><^<<.>v^v>.<>.^vv.^>^v.^^^^.<.vv^vv<<# 22 | #.vv>^.vv<<<<.v.>v><.<<<>.>v^^.^^>^<^v^^<^^v<>^^v>><>.><^<^^>>v<^^^^><>><<<>^<# 23 | #>vv^>.^<><<..>^<><.v^<^>^>v.v^^<<>>v<^v>.<v^.<.>v.>>v..<>>v^^v^<<^>>>v>>^v<^<>.<.# 24 | #>v^<^.v^>><<><^^^..><<>vv^^><<<.><.<^.<<<>v<.>vv<>>^v>.^>.^><^^vv<>v>><^>^^^vvv^v.v>v<<<>v<# 25 | #>^v<>.>.><^>>vv.>v^><>><<>v^^v>^^vv>v>^.<^^<>^>^v^vv.><<^>.<>^>v<^<.>>vvv>>>v^<>vv.<>v>^^>vv^>>vvvv.vv^>>v^v<<>^><<<<>v>^><^<..>.v^>>vv^>>>>v.v.>^^^^^<^<^<>^>>>>..^vv>.^<>v^>>v>.><# 27 | ########################################################################################################################.# 28 | -------------------------------------------------------------------------------- /Tommimon/d24/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | int sizeX, sizeY; 10 | const string dirMap = ">v<^"; 11 | const vector> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {0, 0}}; 12 | 13 | int calcTime(vector>>& blizzards, pair entrance, pair escape) { 14 | vector> reachable(sizeY, vector(sizeX)); 15 | int steps = 0; 16 | bool destination = false; 17 | while (true) { 18 | vector>> tempB(sizeY, vector>(sizeX, vector(0))); 19 | for (int i = 0; i < sizeY; ++i) 20 | for (int j = 0; j < sizeX; ++j) 21 | while (!blizzards[i][j].empty()) { 22 | int b = blizzards[i][j][blizzards[i][j].size()-1]; 23 | int y = (i + directions[b].first) % sizeY; 24 | int x = (j + directions[b].second) % sizeX; 25 | if (y == -1) y = sizeY-1; 26 | if (x == -1) x = sizeX-1; 27 | tempB[y][x].push_back(b); 28 | blizzards[i][j].pop_back(); 29 | } 30 | blizzards = tempB; 31 | vector> tempR(sizeY, vector(sizeX)); 32 | if (blizzards[entrance.first][entrance.second].empty()) 33 | tempR[entrance.first][entrance.second] = true; 34 | for (int i = 0; i < sizeY; ++i) 35 | for (int j = 0; j < sizeX; ++j) 36 | if (reachable[i][j]) 37 | for (auto d: directions) { 38 | int y = i + d.first; 39 | int x = j + d.second; 40 | if (y >= 0 && y < sizeY && x >= 0 && x < sizeX && blizzards[y][x].empty()) 41 | tempR[y][x] = true; 42 | } 43 | reachable = tempR; 44 | steps++; 45 | if (destination) // placed here to take do an additional blizzard cycle 46 | break; 47 | destination = reachable[escape.first][escape.second]; 48 | } 49 | 50 | return steps; 51 | } 52 | 53 | int main() { 54 | ifstream ifs("input"); 55 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 56 | 57 | vector lines(split(input, "\n")); 58 | sizeY = (int) lines.size()-2; 59 | sizeX = (int) lines[0].size()-2; 60 | 61 | vector>> blizzards(sizeY, vector>(sizeX, vector(0))); 62 | for (int i = 0; i < sizeY; ++i) 63 | for (int j = 0; j < sizeX; ++j) 64 | if (lines[i+1][j+1] != '.') 65 | blizzards[i][j].push_back((int) dirMap.find(lines[i + 1][j + 1])); 66 | 67 | int steps = calcTime(blizzards, {0, 0}, {sizeY-1, sizeX-1}); 68 | cout << steps << endl; 69 | steps += calcTime(blizzards, {sizeY-1, sizeX-1}, {0, 0}); 70 | steps += calcTime(blizzards, {0, 0}, {sizeY-1, sizeX-1}); 71 | cout << steps << endl; 72 | } 73 | -------------------------------------------------------------------------------- /Tommimon/d25/input: -------------------------------------------------------------------------------- 1 | 21-2-0=1- 2 | 2-11220-==01==2 3 | 1-2200=1=0-- 4 | 1-10--22=-=-2 5 | 21120 6 | 1=0== 7 | 10=022 8 | 222--0=10202-0=2 9 | 1=01==1 10 | 1021 11 | 20--221-222000 12 | 12-1=02--0221=-1 13 | 11-=-1===2- 14 | 1-101-= 15 | 11-21100-===0=- 16 | 1- 17 | 2-= 18 | 1=0=-0 19 | 10011001=0 20 | 2-- 21 | 2120=202121 22 | 1=0 23 | 10==- 24 | 22=02102=2- 25 | 11=0- 26 | 1==0-1-1=0 27 | 21===-=-0 28 | 11220-=2-=20=201 29 | 2=001 30 | 1=20--=- 31 | 1===00 32 | 12-22012012-21= 33 | 2=--=20-111=- 34 | 221 35 | 12=0-=-= 36 | 2---102 37 | 1=1 38 | 202011021= 39 | 21=0-0=--1102-1- 40 | 1=--2102220-2==221= 41 | 1=10021-=21 42 | 11=2=202120021-=11 43 | 1-1012=122= 44 | 12-01=0=1=0-11=0-- 45 | 100-20=1=- 46 | 110=0- 47 | 1=-102 48 | 2-221=2--010-01 49 | 12-01220 50 | 10--10====1-20=-00 51 | 110 52 | 1=00==22 53 | 212-1-222010=2==2=1 54 | 1=011=1=0 55 | 1-0 56 | 1-11221=000-=200221 57 | 221-00=2=-1- 58 | 122=1=2 59 | 101 60 | 1==2=11=-10 61 | 220-1--=21=2-02== 62 | 21--02--0-==1 63 | 1==0=11012 64 | 122 65 | 1000=20=0012 66 | 22-=0-011110= 67 | 2=21-1-20212--= 68 | 1-=202010 69 | 1-0=-===21-100 70 | 1-22--21 71 | 1-12011122=02=-21 72 | 1-01-2=1000-01=21- 73 | 12-1-1=1=-=01=-1= 74 | 202 75 | 1==-1-00 76 | 1201--=-=- 77 | 1=-1--==1122=2=12 78 | 201020 79 | 1=-11-0111-00-2 80 | 10==1==-1=2 81 | 120=1002-00122 82 | 1=12 83 | 2122110 84 | 1112-10=12== 85 | 1=02--0-= 86 | 2-0021-==112=10= 87 | 2212-022-==10== 88 | 1001200-1-= 89 | 2=000-=00--11 90 | 1==0122120 91 | 1=1==-=2210- 92 | 1-02===012 93 | 12-21-11-1-10 94 | 2=10==-=11=- 95 | 22---2=2012-22100 96 | 1=-0=-- 97 | 1=20--2=1212===12= 98 | 1= 99 | 2- 100 | 1-=2=2-00211-2=0 101 | 1=222---02=00211- 102 | 112==10 103 | 2-0=101-10=02 104 | 11212101- 105 | 10-= 106 | 1=01=2=011=--0 107 | 21=0-10==22-201 108 | 1-20=-1=121- 109 | 120= 110 | 1-=01=-1- 111 | 11--1-11=0=221 112 | 1-=120== 113 | 20--002= 114 | 12--22 115 | 200-= 116 | 1=1--21=111=--=011-1 117 | 12011=0=--11 118 | 1-2 119 | 121==1 120 | 10220-==- 121 | -------------------------------------------------------------------------------- /Tommimon/d25/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | const string digits = "=-012"; 10 | 11 | int digitToNum (char d) { 12 | return digits.find(d)-2; 13 | } 14 | 15 | char numToDigit (int n) { 16 | return digits[n+2]; 17 | } 18 | 19 | const int space = 25; 20 | 21 | int main() { 22 | ifstream ifs("input"); 23 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 24 | 25 | vector lines(split(input, "\n")); 26 | vector> nums = py::map>([](string s){ 27 | vector n(space, 0); 28 | for (int i = 1; i <= s.size(); ++i) 29 | n[space-i] = digitToNum(s[s.size()-i]); 30 | return n; 31 | }, lines); 32 | 33 | 34 | vector total(space, 0); 35 | 36 | for (auto n: nums) { 37 | for (int i = 0; i < space; ++i) 38 | total[i] += n[i]; 39 | for (int i = space-1; i >= 1; --i) { 40 | if (total[i] > 2) { 41 | total[i] -= 5; 42 | total[i-1]++; 43 | } 44 | else if (total[i] < -2) { 45 | total[i] += 5; 46 | total[i-1]--; 47 | } 48 | } 49 | } 50 | 51 | bool firstNotZero = false; 52 | for (auto n: total) 53 | if (firstNotZero || n != 0){ 54 | firstNotZero = true; 55 | cout << numToDigit(n); 56 | } 57 | cout << endl; 58 | } 59 | -------------------------------------------------------------------------------- /Tommimon/input-downloader.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import datetime 3 | import time 4 | 5 | COOKIE_FILE = 'cookies' 6 | URL = 'https://adventofcode.com/{}/day/{}/input' 7 | USER = 'Tommimon' 8 | 9 | time.sleep(20) 10 | 11 | year = datetime.date.today().year 12 | day = datetime.date.today().day 13 | 14 | with open(COOKIE_FILE, "r") as cookies_file: 15 | cookies = cookies_file.readlines()[0] 16 | headers = {"Cookie": cookies} 17 | 18 | folder = str(day) if day >= 10 else '0'+str(day) 19 | puzzle_input = requests.get(URL.format(year, day), headers=headers).text 20 | 21 | path = USER + '/d' + folder + '/input' 22 | with open(path, "w") as file: 23 | file.write(puzzle_input) 24 | -------------------------------------------------------------------------------- /Tommimon/py++.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace py { 5 | std::vector split(std::string const &str, std::string const &delim) { 6 | std::vector out; 7 | std::string remaining(str); 8 | size_t cut = 0; 9 | while (!remaining.empty() && cut != std::string::npos) { 10 | cut = remaining.find(delim); 11 | if (cut == std::string::npos) 12 | out.push_back(remaining.substr(0, remaining.size())); 13 | else { 14 | out.push_back(remaining.substr(0, cut)); 15 | remaining = remaining.substr(cut + delim.size(), remaining.size()); 16 | } 17 | } 18 | return out; 19 | } 20 | 21 | std::string replace(std::string const &str, std::string const &charsIn, std::string const &charsOut) { 22 | std::string out(str); 23 | size_t pos = 0; 24 | while (pos != std::string::npos) { 25 | pos = out.find(charsIn, pos); 26 | if (pos != std::string::npos) { 27 | out.erase(pos, charsIn.length()); 28 | out.insert(pos, charsOut); 29 | pos += charsOut.length(); 30 | } 31 | } 32 | return out; 33 | } 34 | 35 | int sum(std::vector const &vec) { 36 | int tot = 0; 37 | for (auto e: vec) 38 | tot += e; 39 | return tot; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Tommimon/py++.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ADVENT_OF_CODE_2022_PY_HPP 2 | #define ADVENT_OF_CODE_2022_PY_HPP 3 | 4 | #include 5 | 6 | namespace py { 7 | std::vector split(std::string const &str, std::string const &delim); 8 | 9 | std::string replace(std::string const &str, std::string const &charsIn, std::string const &charsOut); 10 | 11 | int sum(std::vector const &vec); 12 | 13 | template 14 | std::vector map(std::function const &func, std::vector const &vec) { 15 | std::vector out; 16 | for (const auto &e: vec) 17 | out.push_back(func(e)); 18 | return out; 19 | } 20 | 21 | template 22 | T max(std::vector const &vec, std::function const &key) { 23 | T max_elem; 24 | int max_key; 25 | bool first = true; 26 | for (const auto &e: vec) { 27 | int e_key = key(e); 28 | if (first || e_key > max_key) { 29 | first = false; 30 | max_key = e_key; 31 | max_elem = e; 32 | } 33 | } 34 | return max_elem; 35 | } 36 | 37 | template 38 | void remove(std::vector &vec, T const &elem) { 39 | std::remove_if(vec.begin(), vec.end(), [&](const auto &item) { 40 | return elem == item; 41 | }); 42 | } 43 | } 44 | 45 | #endif //ADVENT_OF_CODE_2022_PY_HPP 46 | -------------------------------------------------------------------------------- /Tommimon/template/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tommimon/advent-of-code-2022/a239aab17c0d523fe9ab1e04c7a5142d1c824b50/Tommimon/template/input -------------------------------------------------------------------------------- /Tommimon/template/part1and2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "py++.hpp" 5 | 6 | using namespace std; 7 | using namespace py; 8 | 9 | int main() { 10 | ifstream ifs("input"); 11 | string input((istreambuf_iterator(ifs)), (istreambuf_iterator())); 12 | 13 | vector lines(split(input, "\n")); 14 | int score1 = 0; 15 | int score2 = 0; 16 | 17 | for (int i = 0; i < lines.size(); ++i) { 18 | string l = lines[i]; 19 | 20 | } 21 | 22 | cout << score1 << endl; 23 | cout << score2 << endl; 24 | } 25 | -------------------------------------------------------------------------------- /marcomole00/d01/d1.raku: -------------------------------------------------------------------------------- 1 | say 'input'.IO.split("\n\n").map(*.words.sum).sort(-*)[0,^3].map(*.sum); -------------------------------------------------------------------------------- /marcomole00/d01/p1.py: -------------------------------------------------------------------------------- 1 | f = open("marcomole00/d01/input","r").readlines() 2 | 3 | elves = [] 4 | 5 | temp =0 6 | for el in f: 7 | elem = el.strip("\n") 8 | if elem == "": 9 | elves.append(temp) 10 | temp =0 11 | else: 12 | temp += int(elem) 13 | 14 | 15 | elves.sort() 16 | 17 | print(elves[-1]) 18 | 19 | print(elves[-1] + elves[-2] +elves[-3]) 20 | 21 | print("eval solution") 22 | f2 = sorted(eval(open("marcomole00/d01/input", "r").read().replace("\n\n", ",").replace("\n", "+"))) # this is copied from reddit 23 | 24 | print(f2[-1], sum(f2[-3:])) 25 | -------------------------------------------------------------------------------- /marcomole00/d02/sol.py: -------------------------------------------------------------------------------- 1 | 2 | ref = ["A","B","C"] 3 | 4 | ## part11 5 | # ROCK beats SCISSORS A beats C 0,2 6 | # PAPER beats ROCK B beats A 1,0 7 | # SCISSORS beats PAPER C beats B 2,1 8 | 9 | points = { 10 | "A" : { 11 | "A": 1+3, 12 | "B": 2+6, 13 | "C": 3+0 14 | }, 15 | "B" : { 16 | "A": 1+0, 17 | "B": 2+3, 18 | "C": 3+6 19 | }, 20 | "C" : { 21 | "A": 1+6, 22 | "B": 2+0, 23 | "C": 3+3 24 | }, 25 | 26 | 27 | } 28 | 29 | 30 | f = open("marcomole00/d02/input.txt","r").readlines() 31 | 32 | f1 = list(map(lambda s: s.replace("\n", "").replace("X", "A").replace("Y", "B").replace("Z", "C"),f)) 33 | 34 | 35 | my_points1 = 0 36 | 37 | for p in f1: 38 | opponent, me = p.split(" ") 39 | my_points1+= points[opponent][me] 40 | 41 | 42 | print(my_points1) 43 | 44 | 45 | 46 | ### part 2 47 | 48 | my_points2 =0 49 | 50 | # X lose, Y draw, Z wins 51 | 52 | f2 = list(map(lambda s: s.strip(),f)) 53 | 54 | for p in f2: 55 | opponent, result = p.split(" ") 56 | 57 | 58 | if (result == "X"): 59 | my_points2 += points[opponent][ref[ref.index(opponent)-1]] 60 | if (result == "Y"): 61 | my_points2 += points[opponent][ref[ref.index(opponent)]] 62 | if (result == "Z"): 63 | my_points2 += points[opponent][ref[(ref.index(opponent)+1)%3]] 64 | 65 | print(my_points2) -------------------------------------------------------------------------------- /marcomole00/d03/sol.py: -------------------------------------------------------------------------------- 1 | f = open("marcomole00/d03/input","r").read().split("\n") 2 | 3 | #print(f) 4 | 5 | total = {} 6 | 7 | points = {} 8 | 9 | sum_of_total =0 10 | 11 | for index, let in enumerate(range(ord("a"),ord("z")+1)): 12 | points[chr(let)] = index+1 13 | 14 | for index, let in enumerate(range(ord("A"),ord("Z")+1)): 15 | points[chr(let)] = index+27 16 | 17 | 18 | 19 | for l in f: 20 | partial = {} 21 | 22 | for i in range(0,int(len(l)/2)): 23 | partial[l[i]] = 1 24 | 25 | for i in range(int(len(l)/2), len(l)): 26 | if(l[i] in partial.keys()): partial[l[i]]+=1 27 | 28 | for (key,value) in partial.items(): 29 | if value >= 2: 30 | if key not in total.keys(): 31 | total[key] =0 32 | 33 | total[key] +=1 34 | 35 | 36 | 37 | 38 | 39 | for key in total.keys(): 40 | sum_of_total += points[key]*total[key] 41 | 42 | 43 | print(sum_of_total) 44 | 45 | 46 | ### part1 with sets 47 | total.clear() 48 | sum_of_total =0 49 | 50 | for l in f: 51 | a,b = l[:len(l)//2], l[len(l)//2:] 52 | item = (set(a) & set(b)).pop() 53 | if item not in total.keys(): total[item] =0 54 | total[item] += 1 55 | 56 | 57 | for key in total.keys(): 58 | sum_of_total += points[key]*total[key] 59 | 60 | 61 | print(sum_of_total) 62 | 63 | 64 | 65 | ### part2 66 | total.clear() 67 | partial.clear() 68 | index =0 69 | for ln, l in enumerate(f): 70 | 71 | 72 | for c in l: 73 | if c not in partial.keys(): partial[c] = set() 74 | partial[c].add(ln) 75 | 76 | index += 1 77 | 78 | if index == 3: 79 | for (key,value) in partial.items(): 80 | if (len(value) >=3): 81 | if key not in total.keys(): total[key]=0 82 | total[key] += 1 83 | 84 | partial.clear() 85 | index =0 86 | 87 | 88 | print (total) 89 | 90 | sum_of_total=0 91 | for key in total.keys(): 92 | sum_of_total += points[key]*total[key] 93 | 94 | 95 | print(sum_of_total) 96 | -------------------------------------------------------------------------------- /marcomole00/d03/test.py: -------------------------------------------------------------------------------- 1 | 2 | f = open("marcomole00/d03/input","r").read().split("\n") 3 | 4 | 5 | #part2 = sum(priority(set(x[0]).intersection(set(x[1])).intersection(set(x[2])).pop()) for x in zip(*[iter(data)]*3)) 6 | 7 | 8 | for x in zip(*[iter(f)]*2): 9 | print(x) -------------------------------------------------------------------------------- /marcomole00/d04/sol.py: -------------------------------------------------------------------------------- 1 | import re 2 | f = open("marcomole00/d04/input","r").read().split("\n") 3 | 4 | set_a,set_b = set(),set() 5 | 6 | fully_contained_sets =0 7 | number_of_overlaps =0 8 | for l in f: 9 | elf1,elf2 = l.split(",") 10 | # elf1lb, elf1ub = map( lambda s: int(s) ,elf1.split("-")) 11 | elf1lb, elf1ub,elf2lb, elf2ub = map(int, re.findall(r'\d+',l)) 12 | 13 | set_a = set(range(elf1lb,elf1ub+1)) 14 | 15 | 16 | # elf2lb, elf2ub = map( lambda s: int(s) ,elf2.split("-")) 17 | set_b = set( range(elf2lb,elf2ub+1)) 18 | 19 | if any(set_a & set_b): 20 | number_of_overlaps+=1 21 | 22 | if set_a <= set_b or set_b<=set_a: 23 | fully_contained_sets +=1 24 | 25 | 26 | print(fully_contained_sets) 27 | print(number_of_overlaps) -------------------------------------------------------------------------------- /marcomole00/d05/sol.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | f = open("marcomole00/d05/input","r").read().split("\n") 4 | 5 | piles = [list() for i in range(0,9)] 6 | 7 | for l in f: 8 | if len(l) == 0: continue 9 | if l[0] == '[': 10 | print(l) 11 | for (index, char) in enumerate(l): 12 | if (index % 4 == 1) and char != ' ': 13 | piles[index//4].append(char) 14 | elif l[0] == 'm': 15 | amount, p0,p1 = map(int,re.findall('(\d+)',l)) 16 | for i in range(0,amount): 17 | if(len(piles[p0-1]) == 0): break 18 | piles[p1-1].insert(0,piles[p0-1].pop(0)) 19 | 20 | 21 | sol1 = "" 22 | for p in piles: 23 | sol1 += p[0] 24 | 25 | print(sol1) 26 | print(*[*zip(*piles)][0]) 27 | 28 | ### part2 29 | 30 | piles = ["" for i in range(0,9)] 31 | 32 | for l in f: 33 | if len(l) == 0: continue 34 | if l[0] == '[': 35 | # 36 | for (index, char) in enumerate(l): 37 | if (index % 4 == 1) and char != ' ': 38 | piles[index//4] += char 39 | elif l[0] == 'm': 40 | amount, p0,p1 = map(int,re.findall('(\d+)',l)) 41 | piles[p1-1] = piles[p0-1][0:amount] + piles[p1-1] 42 | piles[p0-1] = piles[p0-1][amount:] 43 | 44 | 45 | sol2 = "" 46 | for p in piles: 47 | sol2 += p[0] 48 | 49 | print(sol2) 50 | -------------------------------------------------------------------------------- /marcomole00/d06/input: -------------------------------------------------------------------------------- 1 | rhghwwsmsgmgsmmmlzljllrddvsvhvhhnvvrcccwhhvgghchwhvwvcvrrrgtgrgdggfdgffshsllvvslsppglgvgwgswwcbcwbwrrbjbwjjtgjjdzdfdhfddrmmhqhpqhhqghhzssqzqccbwwffzvzffvtftddrbrtrrcjrrmmbmrrrlrppplmplmppfzpfpvpqvpqqdndpndnmnwmwjmwmwbbrhhmzmffwfqwfqwffppvfpvffnwwwwcbwbhbccvmmplmplmmlmplpprbpbllmhlhzhffngfnntrrsrprrvlllvjvmjvvppsrrfnrfnrfrfwrffrsfrflfltfllpfprrthhhnhqnnfwwcttvzzddqsddwpddnrnttvjttjjdjvdjvjnvnrvvchhschhlffjggtqgqnnvrnrsnrsnsvspsnncppvwvvtmmcqmcmscmsmwmvmpvvlqqmbqqlwwtgtztgttpjjbfjjzhzthhzssffwqqdmdcdlclrclrccbmmqjqmqcmmnnsvnntpphhdpdhhtbtdtffhzfhhpwhppwbppfspphdhggzqgzzffdsdfsszcscpscsqcssffhjhnhhlqqdbqqdmdsmddrfddndldgglvglvlggqlggjngjnnbffthhjbjpjnjmnmgmqqbjqjzjqzzzjffwttcnnzffmllcncmnnthnncttpgpnnjdjfftmmvqmqsqggfrfjjqbjbvvshvhphfhhljlfjjvcclzclctcmmfwfvwfvwwjvwvjjplpbpqqdvqqbhbdbnnsndssgffgsglssmfmjjfrjfrfttljldjdffjjdnjjlbbnrntrtfrttmhhndnsdsffgqqrpplvvfwvfvsfszzfzhfzzlttlctllrzrggvwvhhgnnlmnlmlpppdldqqdpdlplrpppjspspzzhphccgjjpmmwvwbwssnpssrzrwrhhmzhmhsmhmttrqrvqvcclggptpjpzpccltctppflfjffrtrjrhjhffhjfjbfjfqfqzffdtthwwjwzjzggsgzgqqjdqjddvgvsvnnqwqgwgtttfvfrfwrfwfwmwmqwmmmbzmzvzfvfrfvvgvtvptpbpwbwrbwrwffzzfgzfgfcfzcffjdjjvrvnnfmmtztffsggjcjzcjcssscnscnssmmwgwbbljlnjndnrrqwqjjjbnnmbmcmbcmbbvvmtvmmfcmcjcdcnnzfzfzqfzfvvmbvmmndnbnsnnlddqvvsnngvnnrttfbtbqqnwqqzfzfzccfmmgrrsbbhvvgjjhfhssjmmsjjzzbqblqlfqllwhlwwvvstvtfvttnncjjzcjcjrrsvrssmfsfczbrzrvscvmcmrjpzwhcqfrrzbljnmqlzbzqtmhrshlrjjpvhnsvtlhqggqwppsjpszmqwfqmlwbqzwcrggrvfbvztnflwvbrqcrqbcllswvsvhwjzpldgphwptfdnlgdlnbttjfzrdcfvpdhlssfsljvdjmwddbnrpqnnqlfdfdspbnjwqjwrgtnrftsqcfjpmqwgwhttggjwzvgbwlhmtmmjlwhssrgshzpbcnstzlqdshdhjfgqlsmqqhpwbscsjhfbhprvhmftqngjgdbcvfldqgqsjqjfdmcvsflwzflsjfssnjpbwffnsfcnrdsphbjpgghmcthgnzmpgppqjdvbztvhnwqzndntcpjdtwwhvsmgdcthpssszrqcbntgsznpghmbqddpqscntjprlwzhbzhjtwzbwwcldwdgsttzmtnstjnngzrgvhncdbgqnllfzbthldztsdwsngjzprbvrnbzrsghlgssbqfnbvhnhzwmmmtncvsdngdtwcbjlnnzbnlrnmrvnvsjnvzdqnggmsvljlvjznwdszcmblhrsjvczpnlhsmqjsmwhbjbplbtqsgqjdllhncwdgbvzwnmqvndcbfhnvtjnzmvjhwzvdldhgfwbqqzcnbflfnwntlhmqgdhmrgwqcpmsvfbmwhbtsbdlhcnbcbswvdfffgjvddrbpzpcwsrsjnfvmzhlvbdnwttnqrmzbnnntbfvptrlhjhwjcsbnhvtwtwzvgfnzjplthqjbsjjwzdtqqvblnbvgcmvrmnvmwfrhcqgvrcjlfzdlpbfvncbtfgvnsflbjzqqhczcmtbwqmrppmptfgzvfbmcslwlfrfpvvnvvnwfvvmmdzmmtjsgqdfhngtphtlfjqrtljgnthgnbbqfrnpfmpwhpzdvzmtswwdvcnpsdcqwjdwlvbsbmlwdsjbcbgcrfljshlvpngfmsrzlfhtfqgwbcctnzzhnqhdmqzdwthftwtmpbcmqvdcdtgvltbzmszzwwmhzlfvbdqnhjqgdmstsnhftcwzvvbmnhwvgqzscwcdjbdgfmvpjdzctwqwltbwjlgcblnnhpnmggbmvqpqtgqjzspgqzvcvsdbvjgjfzdzhfpbzjqljjcgldzgnlmtjcmfgdbqgglvjqrppwmhccvqzvsrjjvfhjprwdsqsnszfprznljtcsrtqhcrpljfrccflmbpvqtzgmzhjrlbnrmmsmmjbtzwpglqgdvvvjvnfzmplsmvlvcnjshvjwntclwgpznnzwhjssgdcjbzrmsgnfgcgphrhfvrfhzwdcvsplhbmqwhpmjvqlmschznbqblvhtqfgtdggmncndhhplnzjphccjmlmtdqnmnlnpnfqdstljqnsqbrjrtspvrwvdmwzlgdmsfvctzgtmgqhqqrzpbgplzcfdqnzhsqrbcvhsccshnnpvvrpvqzqsgzgmpzfvvrrcvhdtntnsqnjrbzlbzmpgwdqzbhtlrrhbwdqjlsfgdhmvgmgbqhwvljmmqfllqvvrznrlftgzjdcgtstjffqmgvffpvtctzpdqjfnmlcdzscntctmqhrtmhrlhbjzttrcvcnlhsrvtpbmdchhntnnpnzlvqqnsrjcmblcvphqgwshnjmplgvnbsmmdzgqcpqztjhhgjvtlbpdpdwlwmmfrdgcvzfbgvbgpbjnwsssvhszwplcgvpgjwdrwngbcdjwvlsfhqlrqzgrzpfgjstqfdbrpqdvrlgdwqcsrgvhctznjjlzsmzctsqtfnhhlpjgnltssglmlwshfbrgmjqbvsmqwvszdfsvhmtrfjgwjctpsmgzzjbpwsztnnvzrhwvvmhdpgdmwzsjprhlgzcdvhznlfgjqvcwqrplcfvzmthsdsnrtfvnlrmvwplmbdvdggmlvpgdgzhvzmvzwmptzsnfrcrjspccmqjpjmhjqgrjbdcdjbzjmphmcdvjqtmdshhjrqgjgsnpzfbfgpjpczwzvmclgzgztlvzmdbwgncnndjwhhhjnhtjdmcnrmnqbmjdrdcmtvcsmftqcfhsvhsfjmtzjpnwffggpfqlqmzlbhnnhbtgzfgnjvdzmvthqjrhzbwvhcjzcsmsvsctrqbltpcrpjjnsbjdbfjqfcbpcgcwtqsflmlwprjcwlmcjjgsfdpwcqvhjpsgvdgsfnscnbzsrmrbbvdrlltzplbvgqsdnplcvbhddbtmwnfmvqhqdlrtrmrmzmhlccgwgmbdppjqdjtwmvdzfsbsggrfstjwjpjnljffwffmqncfnthnhglwvsgvzmgbzhtdfpfmdwmcldthvsnqnptpmhqctblgfsszhcfbvcrggjdhthqvvvlldshvqwmvdtfslrhzvgdfwztrczdjgcfcgtmwnphqthlgpfnrqwcgpzwnlgdvsnvzftlnlfflfsmjzhrhqjctsbvtccwbfsdrnbhszzjhqndvwcsmffnstnfdfwpbgfztjmjngdczzlgpscjtshpmmmzlnqndsttbdgfjqcvbqlphwhlhgcvjbhjmtrfzlgpwdnvzrllndbhvhlngvhlszzdcrdgvrmjwcvhhtbhnjmdzgctqnpdlrnqjzbchjtcsggsczlgmvtqvzmsqvtrhtvdmzlcdddfnbvbsnrzvgzfqjtbhjqhdznrhbfbqwtnwvrfqsznbqfzfzfgmhvjjsgbbdbdtzswwlnfrq 2 | -------------------------------------------------------------------------------- /marcomole00/d06/sol.py: -------------------------------------------------------------------------------- 1 | f = open("marcomole00/d06/input").read() 2 | 3 | def solve(n,f): 4 | i=0 5 | while True: 6 | if len(set(f[i:i+n])) ==n: 7 | print(f[i:i+n]) 8 | print(i+n) 9 | break 10 | else: 11 | i += 1 12 | 13 | solve(4,f) 14 | solve(14,f) -------------------------------------------------------------------------------- /marcomole00/d07/sol.py: -------------------------------------------------------------------------------- 1 | from itertools import accumulate 2 | 3 | f = open("marcomole00/d07/input").read().split("\n") 4 | 5 | directories = {'//':0} 6 | 7 | current_directory = [] 8 | 9 | for l in f: 10 | match l.split(): 11 | case 'dir', dirname : 12 | directories[''.join(current_directory)+dirname+"/"] = 0 13 | case "$", 'ls':continue 14 | 15 | case '$', 'cd','..': 16 | current_directory.pop() 17 | case '$', 'cd' , x : 18 | current_directory.append( x + "/") 19 | case size , file: 20 | for i in accumulate(current_directory): 21 | directories[i] += int(size) 22 | 23 | 24 | 25 | p1=0 26 | for val in directories.values(): 27 | if val <= 100000: p1+=val 28 | 29 | 30 | print(f"part1 is {p1}") 31 | 32 | 33 | ## part2 34 | 35 | ## Find the smallest directory that, 36 | # if deleted, would free up enough space on the filesystem to run the update 37 | 38 | total = 70000000 39 | update_size= 30000000 40 | 41 | dir_size = total 42 | for val in directories.values(): 43 | if total -int(directories["//"]) + val >= update_size: dir_size= min(dir_size,val) 44 | 45 | 46 | print(f"part2 is {dir_size}") -------------------------------------------------------------------------------- /marcomole00/d08/sol.py: -------------------------------------------------------------------------------- 1 | input = open("marcomole00/d08/input").read().split("\n") 2 | 3 | f = [[int(y) for y in x] for x in input] 4 | 5 | visibles_trees = {(i,j):[] for i in range(1,99) for j in range(1,99)} 6 | 7 | visible = 2*len(f) + 2*len(f[0]) -4 8 | print(visible) 9 | scenic_scores = {(i,j):int() for i in range(1,99) for j in range(1,99)} 10 | 11 | print(f) 12 | 13 | # A tree is visible if all of the other trees between it and an edge of the grid are shorter than it. 14 | # Only consider trees in the same row or column; that is, only look up, down, left, or right from any given tree. 15 | 16 | for i in range (1,len(f)-1): 17 | for j in range(1,len(f[0])-1): 18 | tree_up, tree_right, tree_down, tree_left = (False,False,False,False) 19 | scenic_score = [0,0,0,0] 20 | 21 | #up 22 | for d in range(i-1,-1,-1): 23 | scenic_score[0]+=1 24 | if (f[d][j] >= f[i][j]): 25 | tree_up = True 26 | break 27 | if not tree_up : visibles_trees[(i,j)].append("up") 28 | 29 | #down 30 | for d in range(i+1,len(f)): 31 | scenic_score[1]+=1 32 | if (f[d][j] >= f[i][j]): 33 | tree_down = True 34 | break 35 | 36 | 37 | if not tree_down: visibles_trees[(i,j)].append("down") 38 | 39 | #left 40 | for d in range(j-1,-1,-1): 41 | scenic_score[2]+=1 42 | if (f[i][d] >= f[i][j]): 43 | tree_left = True 44 | break 45 | 46 | 47 | if not tree_left: visibles_trees[(i,j)].append("left") 48 | 49 | #right 50 | for d in range(j+1,len(f[0])): 51 | scenic_score[3]+=1 52 | if (f[i][d] >= f[i][j]): 53 | tree_right = True 54 | break 55 | 56 | if not tree_right : visibles_trees[(i,j)].append("right") 57 | 58 | scenic_scores[i,j] = scenic_score[0]*scenic_score[1]*scenic_score[2]*scenic_score[3] 59 | 60 | for keys,values in visibles_trees.items(): 61 | if values != []: 62 | visible += 1 63 | print(keys,values) 64 | 65 | 66 | 67 | 68 | print(visible) 69 | 70 | print(max( scenic_scores.values())) 71 | 72 | -------------------------------------------------------------------------------- /marcomole00/d09/sol.py: -------------------------------------------------------------------------------- 1 | 2 | visited_by_tail = set() 3 | 4 | 5 | 6 | def isNotAdjacent(head:complex,tail:complex) -> bool: 7 | if abs(head-tail) >= 2: 8 | return True 9 | else: return False 10 | 11 | 12 | input = open("marcomole00/d09/input").read().split("\n") 13 | head = 0+0j 14 | tail = 0+0j 15 | 16 | visited_by_tail.add(tail) 17 | 18 | 19 | 20 | for l in input: 21 | match l.split(" "): 22 | case "R" , amount: 23 | for i in range(0,int(amount)): 24 | if isNotAdjacent(head+1,tail): 25 | tail = head 26 | visited_by_tail.add(tail) 27 | 28 | 29 | head += 1 30 | case "L" , amount: 31 | for i in range(0,int(amount)): 32 | if isNotAdjacent(head-1,tail): 33 | tail = head 34 | visited_by_tail.add(tail) 35 | 36 | 37 | head -= 1 38 | case "U" , amount: 39 | for i in range(0,int(amount)): 40 | if isNotAdjacent(head+1j,tail): 41 | tail = head 42 | visited_by_tail.add(tail) 43 | 44 | head += 1j 45 | case "D" , amount: 46 | for i in range(0,int(amount)): 47 | if isNotAdjacent(head-1j,tail): 48 | tail = head 49 | visited_by_tail.add(tail) 50 | 51 | head -= 1j 52 | 53 | print(f"after {l} -> {head}, {tail}") 54 | 55 | 56 | 57 | print(len(visited_by_tail)) 58 | print(visited_by_tail) 59 | 60 | def sign(val): 61 | return complex((val.real > 0 ) - (val.real<0) , (val.imag > 0 ) - (val.imag<0)) 62 | 63 | 64 | ## part2 now with ten knots! 65 | 66 | knots = [ 0+0j for _ in range(0,10)] 67 | 68 | seen = [set([0]) for _ in range(0,10)] 69 | 70 | 71 | for l in input: 72 | 73 | dir,amount = l.split() 74 | 75 | for _ in range(0,int(amount)): 76 | match dir: 77 | case "R": 78 | knots[0] += 1 79 | case "L": 80 | knots[0] -= 1 81 | case "U": 82 | knots[0] += 1j 83 | case "D": 84 | knots[0] -= 1j 85 | 86 | for k in range(1,10): 87 | distance = knots[k-1] - knots[k] 88 | if abs(distance) >=2: 89 | knots[k] += sign(distance) 90 | seen[k].add(knots[k]) 91 | 92 | 93 | 94 | 95 | print(len(seen[9])) 96 | 97 | # -------------------------------------------------------------------------------- /marcomole00/d10/input: -------------------------------------------------------------------------------- 1 | addx 1 2 | addx 4 3 | noop 4 | noop 5 | addx 30 6 | addx -24 7 | addx -1 8 | noop 9 | addx 4 10 | addx 1 11 | addx 5 12 | addx -4 13 | addx 5 14 | addx 4 15 | addx 1 16 | noop 17 | addx 5 18 | addx -1 19 | addx 5 20 | addx 3 21 | noop 22 | addx -38 23 | addx 9 24 | addx -4 25 | noop 26 | addx 3 27 | noop 28 | addx 2 29 | addx 3 30 | noop 31 | addx 2 32 | addx 3 33 | noop 34 | addx 2 35 | addx 3 36 | noop 37 | addx 2 38 | addx -17 39 | addx 22 40 | addx -2 41 | addx 5 42 | addx 2 43 | addx 3 44 | addx -2 45 | addx -36 46 | noop 47 | addx 5 48 | noop 49 | addx 3 50 | noop 51 | addx 2 52 | addx -5 53 | noop 54 | addx 10 55 | addx 3 56 | addx -2 57 | addx 3 58 | addx 2 59 | addx 4 60 | noop 61 | noop 62 | noop 63 | noop 64 | addx 3 65 | noop 66 | noop 67 | addx 7 68 | addx 1 69 | noop 70 | noop 71 | addx -38 72 | addx 39 73 | addx -32 74 | noop 75 | noop 76 | noop 77 | addx 5 78 | addx 2 79 | addx -1 80 | addx 4 81 | noop 82 | addx 5 83 | addx -2 84 | addx 5 85 | addx 2 86 | addx -26 87 | addx 31 88 | addx -2 89 | addx 4 90 | addx 3 91 | addx -18 92 | addx 19 93 | addx -38 94 | addx 7 95 | noop 96 | noop 97 | addx 34 98 | addx -39 99 | addx 8 100 | addx 5 101 | addx 2 102 | addx 10 103 | addx -5 104 | addx -2 105 | addx 5 106 | addx 2 107 | addx 11 108 | addx -6 109 | noop 110 | addx 3 111 | noop 112 | addx 2 113 | addx 3 114 | addx -2 115 | addx -38 116 | noop 117 | noop 118 | noop 119 | addx 5 120 | addx 11 121 | noop 122 | addx -3 123 | noop 124 | noop 125 | noop 126 | addx 2 127 | noop 128 | addx -11 129 | addx 16 130 | noop 131 | addx 3 132 | addx 2 133 | addx 8 134 | noop 135 | noop 136 | noop 137 | noop 138 | noop 139 | addx 4 140 | addx 3 141 | noop 142 | addx -20 143 | noop -------------------------------------------------------------------------------- /marcomole00/d10/sol.py: -------------------------------------------------------------------------------- 1 | input = iter(open("marcomole00/d10/input").read().split("\n")+["end"]) 2 | image = [['.' for _ in range(0,40)] for _ in range(0,6)] 3 | x =1 4 | exec = [] 5 | cycle =1 6 | p1 =0 7 | while True: 8 | print(x) 9 | if cycle in (20,60,100,140,180,220): p1 += cycle*x 10 | if (cycle % 40) -1 in (x-1,x,x+1): image[cycle // 40][(cycle % 40)-1] = "#" 11 | if exec ==[]: 12 | match next(input).split(): 13 | case "addx",amount: exec.append(int(amount)) 14 | case ["noop"]: pass 15 | case ["end"]: break 16 | else: 17 | x += exec.pop() 18 | 19 | cycle+=1 20 | print(p1) 21 | for l in image: 22 | print("".join(l)) 23 | -------------------------------------------------------------------------------- /marcomole00/d11/input.txt: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 89, 74 3 | Operation: new = old * 5 4 | Test: divisible by 17 5 | If true: throw to monkey 4 6 | If false: throw to monkey 7 7 | 8 | Monkey 1: 9 | Starting items: 75, 69, 87, 57, 84, 90, 66, 50 10 | Operation: new = old + 3 11 | Test: divisible by 7 12 | If true: throw to monkey 3 13 | If false: throw to monkey 2 14 | 15 | Monkey 2: 16 | Starting items: 55 17 | Operation: new = old + 7 18 | Test: divisible by 13 19 | If true: throw to monkey 0 20 | If false: throw to monkey 7 21 | 22 | Monkey 3: 23 | Starting items: 69, 82, 69, 56, 68 24 | Operation: new = old + 5 25 | Test: divisible by 2 26 | If true: throw to monkey 0 27 | If false: throw to monkey 2 28 | 29 | Monkey 4: 30 | Starting items: 72, 97, 50 31 | Operation: new = old + 2 32 | Test: divisible by 19 33 | If true: throw to monkey 6 34 | If false: throw to monkey 5 35 | 36 | Monkey 5: 37 | Starting items: 90, 84, 56, 92, 91, 91 38 | Operation: new = old * 19 39 | Test: divisible by 3 40 | If true: throw to monkey 6 41 | If false: throw to monkey 1 42 | 43 | Monkey 6: 44 | Starting items: 63, 93, 55, 53 45 | Operation: new = old * old 46 | Test: divisible by 5 47 | If true: throw to monkey 3 48 | If false: throw to monkey 1 49 | 50 | Monkey 7: 51 | Starting items: 50, 61, 52, 58, 86, 68, 97 52 | Operation: new = old + 4 53 | Test: divisible by 11 54 | If true: throw to monkey 5 55 | If false: throw to monkey 4 56 | -------------------------------------------------------------------------------- /marcomole00/d11/sol.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | monkeys = [] 4 | 5 | input = list(map(lambda s: s.split("\n"), open("marcomole00/d11/input.txt").read().split("\n\n"))) 6 | 7 | modulo_operator_constant = 1 8 | 9 | for monkey in input: 10 | m = { 11 | "number": int(re.findall( "\d", monkey[0])[0]), 12 | "items" : list(map(int, re.findall("\d+", monkey[1]))), 13 | "operation" : monkey[2].replace("Operation: new = ", "").strip(), 14 | "divisible" : int(monkey[3].split()[-1]), 15 | "true" : int(monkey[4].strip()[-1]), 16 | "false" : int(monkey[5].strip()[-1]), 17 | "items_seen" :0 18 | } 19 | modulo_operator_constant *= m["divisible"] 20 | monkeys.append(m) 21 | 22 | for round in range(1, 21): 23 | for m in monkeys: 24 | while m["items"] != []: 25 | old_item = m["items"].pop(0) 26 | item = ( eval(m["operation"].replace("old",str(old_item))) //3 ) % modulo_operator_constant 27 | #print(old_item,item, m["operation"]) 28 | if item % m["divisible"] == 0: monkeys[m["true"]]["items"].append(item) 29 | else: monkeys[m["false"]]["items"].append(item) 30 | m["items_seen"] += 1 31 | 32 | # print("round: ", round) 33 | p1 = [] 34 | for m in monkeys: 35 | #print(f'{m["number"]}: {m["items_seen"]}') 36 | p1.append(m["items_seen"]) 37 | 38 | p1.sort() 39 | print(p1[-1]*p1[-2]) 40 | monkeys.clear() 41 | 42 | for monkey in input: 43 | m = { 44 | "number": int(re.findall( "\d", monkey[0])[0]), 45 | "items" : list(map(int, re.findall("\d+", monkey[1]))), 46 | "operation" : monkey[2].replace("Operation: new = ", "").strip(), 47 | "divisible" : int(monkey[3].split()[-1]), 48 | "true" : int(monkey[4].strip()[-1]), 49 | "false" : int(monkey[5].strip()[-1]), 50 | "items_seen" :0 51 | } 52 | modulo_operator_constant *= m["divisible"] 53 | monkeys.append(m) 54 | 55 | for round in range(1, 10001): 56 | for m in monkeys: 57 | while m["items"] != []: 58 | old_item = m["items"].pop(0) 59 | item = ( eval(m["operation"].replace("old",str(old_item))) ) % modulo_operator_constant 60 | #print(old_item,item, m["operation"]) 61 | if item % m["divisible"] == 0: monkeys[m["true"]]["items"].append(item) 62 | else: monkeys[m["false"]]["items"].append(item) 63 | m["items_seen"] += 1 64 | 65 | # print("round: ", round) 66 | p2 = [] 67 | for m in monkeys: 68 | #print(f'{m["number"]}: {m["items_seen"]}') 69 | p2.append(m["items_seen"]) 70 | 71 | 72 | p2.sort() 73 | print(p2[-1]*p2[-2]) 74 | 75 | 76 | -------------------------------------------------------------------------------- /marcomole00/d12/input: -------------------------------------------------------------------------------- 1 | abccccaaaaaaacccaaaaaaaccccccccccccccccccccccccccccccccccaaaa 2 | abcccccaaaaaacccaaaaaaaaaaccccccccccccccccccccccccccccccaaaaa 3 | abccaaaaaaaaccaaaaaaaaaaaaaccccccccccccccccccccccccccccaaaaaa 4 | abccaaaaaaaaaaaaaaaaaaaaaaacccccccccaaaccccacccccccccccaaacaa 5 | abaccaaaaaaaaaaaaaaaaaacacacccccccccaaacccaaaccccccccccccccaa 6 | abaccccaaaaaaaaaaaaaaaacccccccccccccaaaaaaaaaccccccccccccccaa 7 | abaccccaacccccccccaaaaaacccccccccccccaaaaaaaacccccccccccccccc 8 | abcccccaaaacccccccaaaaaaccccccccijjjjjjaaaaaccccccaaccaaccccc 9 | abccccccaaaaacccccaaaacccccccciiijjjjjjjjjkkkkkkccaaaaaaccccc 10 | abcccccaaaaacccccccccccccccccciiiirrrjjjjjkkkkkkkkaaaaaaccccc 11 | abcccccaaaaaccccccccccccccccciiiirrrrrrjjjkkkkkkkkkaaaaaccccc 12 | abaaccacaaaaacccccccccccccccciiiqrrrrrrrrrrssssskkkkaaaaacccc 13 | abaaaaacaaccccccccccccccccccciiiqqrtuurrrrrsssssskklaaaaacccc 14 | abaaaaacccccccccccaaccccccccciiqqqttuuuurrssusssslllaaccccccc 15 | abaaaaaccccccccaaaaccccccccciiiqqqttuuuuuuuuuuusslllaaccccccc 16 | abaaaaaacccccccaaaaaaccccccciiiqqqttxxxuuuuuuuusslllccccccccc 17 | abaaaaaaccccaaccaaaaacccccchhiiqqtttxxxxuyyyyvvsslllccccccccc 18 | abaaacacccccaacaaaaaccccccchhhqqqqttxxxxxyyyyvvsslllccccccccc 19 | abaaacccccccaaaaaaaacccccchhhqqqqtttxxxxxyyyvvssqlllccccccccc 20 | abacccccaaaaaaaaaaccaaacchhhpqqqtttxxxxxyyyyvvqqqlllccccccccc 21 | SbaaacaaaaaaaaaaaacaaaaahhhhppttttxxEzzzzyyvvvqqqqlllcccccccc 22 | abaaaaaaacaaaaaacccaaaaahhhppptttxxxxxyyyyyyyvvqqqlllcccccccc 23 | abaaaaaaccaaaaaaaccaaaaahhhppptttxxxxywyyyyyyvvvqqqmmcccccccc 24 | abaaaaaaacaaaaaaacccaaaahhhpppsssxxwwwyyyyyyvvvvqqqmmmccccccc 25 | abaaaaaaaaaaaaaaacccaacahhhpppssssssswyyywwvvvvvqqqmmmccccccc 26 | abaaaaaaaacacaaaacccccccgggppppsssssswwywwwwvvvqqqqmmmccccccc 27 | abcaaacaaaccccaaaccccccccgggppppppssswwwwwrrrrrqqqmmmmccccccc 28 | abcaaacccccccccccccccccccgggggpppoosswwwwwrrrrrqqmmmmddcccccc 29 | abccaacccccccccccccccccccccgggggoooosswwwrrrnnnmmmmmddddccccc 30 | abccccccccccccccccccccccccccgggggooossrrrrrnnnnnmmmddddaccccc 31 | abaccccaacccccccccccccccccccccgggfoossrrrrnnnnndddddddaaacccc 32 | abaccaaaaaaccccccccccccccccccccgffooorrrrnnnneeddddddaaaacccc 33 | abaccaaaaaacccccccccccccccccccccfffooooonnnneeeddddaaaacccccc 34 | abacccaaaaaccccccccaaccaaaccccccffffoooonnneeeeccaaaaaacccccc 35 | abcccaaaaacccccccccaaccaaaaccccccffffoooneeeeeaccccccaacccccc 36 | abaccaaaaaccccccccaaaacaaaaccccccafffffeeeeeaaacccccccccccccc 37 | abacccccccccccccccaaaacaaacccccccccffffeeeecccccccccccccccaac 38 | abaaaacccccccaaaaaaaaaaaaaacccccccccfffeeeccccccccccccccccaaa 39 | abaaaacccccccaaaaaaaaaaaaaaccccccccccccaacccccccccccccccccaaa 40 | abaacccccccccaaaaaaaaaaaaaaccccccccccccaacccccccccccccccaaaaa 41 | abaaaccccccccccaaaaaaaaccccccccccccccccccccccccccccccccaaaaaa -------------------------------------------------------------------------------- /marcomole00/d12/sol.py: -------------------------------------------------------------------------------- 1 | file = list(map(list, open("marcomole00/d12/input").read().split("\n"))) 2 | 3 | 4 | input = {(i,j):file[j][i] for i in range(0,len(file[0])) for j in range(0,len(file))} 5 | 6 | BFS_info = {(i,j): False for i in range(0,len(file[0])) for j in range(0,len(file))} 7 | 8 | 9 | start_point = (0,20) 10 | end_point = (36,20) 11 | 12 | 13 | input[start_point] = 'a' 14 | input[end_point] = 'z' 15 | 16 | Q = [] 17 | 18 | Q.append(start_point) 19 | BFS_info[start_point] = (True,start_point) 20 | 21 | 22 | while len(Q) > 0: 23 | v = Q.pop(0) 24 | 25 | if v == end_point: break 26 | 27 | for dir in [(+1,0),(-1,0),(0,+1),(0,-1)]: 28 | 29 | w = (v[0] + dir[0], v[1] + dir[1] ) 30 | if w not in input.keys(): continue 31 | 32 | if ord(input[v]) +1 < ord(input[w]): continue 33 | 34 | if BFS_info[w] == False: 35 | BFS_info[w] = (True, v) 36 | Q.append(w) 37 | 38 | 39 | #print(BFS_info) 40 | 41 | s = end_point 42 | step = 0 43 | while s != start_point: 44 | #print(s, step) 45 | s= BFS_info[s][1] 46 | step += 1 47 | 48 | print(step) 49 | 50 | 51 | 52 | ## part 2 53 | 54 | BFS_info.clear() 55 | BFS_info = {(i,j): False for i in range(0,len(file[0])) for j in range(0,len(file))} 56 | start_point = end_point 57 | end_point = (-1,-1) 58 | Q = [] 59 | 60 | Q.append(start_point) 61 | BFS_info[start_point] = (True,start_point) 62 | 63 | while len(Q) > 0: 64 | v = Q.pop(0) 65 | 66 | if input[v] == 'a': 67 | end_point = v 68 | break 69 | 70 | for dir in [(+1,0),(-1,0),(0,+1),(0,-1)]: 71 | w = (v[0] + dir[0], v[1] + dir[1] ) 72 | if w not in input.keys(): continue 73 | 74 | if ord(input[w]) +1 < ord(input[v]): continue 75 | 76 | if BFS_info[w] == False: 77 | BFS_info[w] = (True, v) 78 | Q.append(w) 79 | 80 | s = end_point 81 | step = 0 82 | while s != start_point: 83 | #print(s, step) 84 | s= BFS_info[s][1] 85 | step += 1 86 | 87 | print(step) 88 | 89 | -------------------------------------------------------------------------------- /marcomole00/d13/sol.py: -------------------------------------------------------------------------------- 1 | f = open("marcomole00/d13/input.txt").read().split("\n\n") 2 | 3 | 4 | 5 | f = list(map(lambda s: eval( s.replace("\n", ",") ), f)) 6 | 7 | ## the recursive idea was stolen, i'm too dumb to do recursion at 7 am 8 | def compare(l,r): 9 | match l,r: 10 | case int(),int(): return l-r 11 | case list(),int(): return compare(l,[r]) 12 | case int(),list(): return compare([l],r) 13 | case list(),list(): 14 | for x,y in zip(l,r): 15 | res = compare(x,y) 16 | if res != 0: return res 17 | 18 | return len(l) - len(r) 19 | 20 | 21 | 22 | indices =0 23 | 24 | for index,p in enumerate(f): 25 | index += 1 26 | l,r = p 27 | 28 | if compare(l,r) < 0 : indices += index 29 | 30 | print(indices) 31 | 32 | ## part2 33 | 34 | f = filter( lambda s: s != "" , open("marcomole00/d13/input.txt").read().split("\n")) 35 | f = list(map(lambda s: eval( s ), f)) 36 | 37 | less_than_two = 0 38 | higher_than_six = 0 39 | for p in f: 40 | if compare(p,[[2]])<0: less_than_two +=1 41 | if compare([[6]],p)<0: 42 | higher_than_six +=1 43 | print(p) 44 | 45 | print((less_than_two+1)* (len(f)+2-higher_than_six)) -------------------------------------------------------------------------------- /marcoparadina/d01/d01.py: -------------------------------------------------------------------------------- 1 | with open('marcoparadina/d01/input.txt') as f: 2 | lines = f.readlines() 3 | 4 | curr = 0 5 | ranking = [0,0,0] 6 | res=0 7 | 8 | for l in lines: 9 | if l == '\n': 10 | for i in range(len(ranking)): 11 | if curr > ranking[i]: 12 | ranking[i] = curr 13 | ranking.sort() 14 | break 15 | curr = 0 16 | else: 17 | curr+=int(l) 18 | 19 | for i in range(len(ranking)): 20 | if curr > ranking[i]: 21 | ranking[i] = curr 22 | ranking.sort() 23 | break 24 | 25 | for i in ranking: 26 | res+=i 27 | print(ranking) 28 | print(res) 29 | 30 | 31 | -------------------------------------------------------------------------------- /marcoparadina/d02/d02.py: -------------------------------------------------------------------------------- 1 | def part1(filename): 2 | score = 0 3 | ascii = ord('A') 4 | dist = ord('X') - ord('A') 5 | with open(filename) as f: 6 | for l in f : 7 | if ord(l[0])==(ord(l[2]) - dist): 8 | #draw 9 | score += ((ord(l[0]) - ascii + 1) + 3) 10 | if l[0]=='A' and l[2] == 'Y': 11 | score += 2+6 12 | if l[0]=='A' and l[2] == 'Z': 13 | score += 3+0 14 | if l[0]=='B' and l[2] == 'X': 15 | score += 1+0 16 | if l[0]=='B' and l[2] == 'Z': 17 | score += 3+6 18 | if l[0]=='C' and l[2] == 'X': 19 | score += 1+6 20 | if l[0]=='C' and l[2] == 'Y': 21 | score += 2+0 22 | print(score) 23 | 24 | output_line = ['A', ' ', 'X'] 25 | out = open('marcoparadina/d02/out.txt', 'w') 26 | with open('marcoparadina/d02/input.txt') as f: 27 | for l in f: 28 | output_line[0] = l[0] 29 | if l[2] == 'X': 30 | #lose 31 | if l[0] == 'A': 32 | output_line[2] = 'Z' 33 | if l[0] == 'B': 34 | output_line[2] = 'X' 35 | if l[0] == 'C': 36 | output_line[2] = 'Y' 37 | if l[2] == 'Y': 38 | #draw 39 | if l[0] == 'A': 40 | output_line[2] = 'X' 41 | if l[0] == 'B': 42 | output_line[2] = 'Y' 43 | if l[0] == 'C': 44 | output_line[2] = 'Z' 45 | if l[2] == 'Z': 46 | #win 47 | if l[0] == 'A': 48 | output_line[2] = 'Y' 49 | if l[0] == 'B': 50 | output_line[2] = 'Z' 51 | if l[0] == 'C': 52 | output_line[2] = 'X' 53 | for i in output_line: 54 | out.write(i) 55 | out.write('\n') 56 | out.close() 57 | part1('marcoparadina/d02/out.txt') 58 | -------------------------------------------------------------------------------- /mynam3isg00d/d01/src/Main.scala: -------------------------------------------------------------------------------- 1 | import scala.io.Source 2 | 3 | @main 4 | def d01(): Unit = { 5 | val nl = System.lineSeparator 6 | val sol = Source.fromFile("mynam3isg00d/d01/input.txt") 7 | .mkString 8 | .split(nl+nl) 9 | .map(s => { 10 | s.split(nl).map(f => f.toInt).sum 11 | }).sorted.reverse.take(3) 12 | 13 | println("Part 1: " + sol.head) 14 | println("Part 2: " + sol.sum) 15 | } -------------------------------------------------------------------------------- /mynam3isg00d/d02/README.txt: -------------------------------------------------------------------------------- 1 | Solution of Day 2 in Node-Red 2 | 3 | How to run: 4 | Follow the guide at https://nodered.org/#get-started for Node-Red setup, 5 | then import the flow as a json file from d02.json 6 | 7 | Notes: 8 | The input is taken directly as a request from https://adventofcode.com/2022/day/2/input, nevertheless, for 9 | tradition's sake, I've left my input as a txt file -------------------------------------------------------------------------------- /mynam3isg00d/d02/d02.json: -------------------------------------------------------------------------------- 1 | [{"id":"f6f2187d.f17ca8","type":"tab","label":"d02","disabled":false,"info":""},{"id":"8f5308cecf6adda6","type":"http request","z":"f6f2187d.f17ca8","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://adventofcode.com/2022/day/2/input","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"other","keyValue":"cookie","valueType":"other","valueValue":"session=53616c7465645f5faa0ff3822ffacaaaf390f225bbc7294986ec210e4e2fd897425f1248700f75e26d63f3ed1f3cec9d9794d5ec57c0a6a2555c13f4623c0817; _ga=GA1.2.1816729364.1669899048; _gid=GA1.2.965087439.1669899048"}],"credentials":{},"x":290,"y":300,"wires":[["5cce1d4859b0496a","3fa576cc27b6e6c1"]]},{"id":"4cfface3e0a03e55","type":"inject","z":"f6f2187d.f17ca8","name":"Activate","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110,"y":300,"wires":[["8f5308cecf6adda6"]]},{"id":"5cce1d4859b0496a","type":"function","z":"f6f2187d.f17ca8","name":"Part 1","func":"let input = msg.payload\n\nlet tot = input.replaceAll(\"A X\", \"4\")\n .replaceAll(\"A Y\", \"8\")\n .replaceAll(\"A Z\", \"3\")\n .replaceAll(\"B X\", \"1\")\n .replaceAll(\"B Y\", \"5\") \n .replaceAll(\"B Z\", \"9\")\n .replaceAll(\"C X\", \"7\")\n .replaceAll(\"C Y\", \"2\")\n .replaceAll(\"C Z\", \"6\")\n .split(\"\\n\").map(Number).reduce((a,b) => a+b)\n\nmsg.payload = \"Part 1: \" + tot\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":200,"wires":[["0b5544bca90f5d5e"]]},{"id":"3fa576cc27b6e6c1","type":"function","z":"f6f2187d.f17ca8","name":"Part 2","func":"let input = msg.payload\n\nlet tot = input.replaceAll(\"A X\", \"3\")\n .replaceAll(\"A Y\", \"4\")\n .replaceAll(\"A Z\", \"8\")\n .replaceAll(\"B X\", \"1\")\n .replaceAll(\"B Y\", \"5\")\n .replaceAll(\"B Z\", \"9\")\n .replaceAll(\"C X\", \"2\")\n .replaceAll(\"C Y\", \"6\")\n .replaceAll(\"C Z\", \"7\")\n .split(\"\\n\").map(Number).reduce((a,b) => a+b)\n \nmsg.payload = \"Part 2: \" + tot\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":400,"wires":[["0b5544bca90f5d5e"]]},{"id":"0b5544bca90f5d5e","type":"debug","z":"f6f2187d.f17ca8","name":"Out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":300,"wires":[]},{"id":"9a90ee7e7f5433fa","type":"comment","z":"f6f2187d.f17ca8","name":"Welcome","info":"In order to use your own input, simply change \nthe cookie in the \"http request\" node headers to your own!","x":100,"y":140,"wires":[]}] -------------------------------------------------------------------------------- /mynam3isg00d/d04/src/main.rs: -------------------------------------------------------------------------------- 1 | //This sucks, i found a better solution on reddit using scan_fmt!() 2 | 3 | use std::fs; 4 | 5 | fn main() { 6 | let input = fs::read_to_string("input.txt").expect("ERROR in opening file"); 7 | 8 | let sol1 = input.split("\r\n").filter(|x| -> bool { 9 | let elf1 = x.split(",").collect::>()[0].split("-").collect::>(); 10 | let elf2 = x.split(",").collect::>()[1].split("-").collect::>(); 11 | 12 | let r11 = elf1[0].parse::().unwrap(); 13 | let r12 = elf1[1].parse::().unwrap(); 14 | let r21 = elf2[0].parse::().unwrap(); 15 | let r22 = elf2[1].parse::().unwrap(); 16 | 17 | let a = r11 >= r21 && r12 <= r22; 18 | let b = r21 >= r11 && r22 <= r12; 19 | 20 | return a || b; 21 | }).count(); 22 | 23 | let sol2 = input.split("\r\n").filter(|x| -> bool { 24 | let elf1 = x.split(",").collect::>()[0].split("-").collect::>(); 25 | let elf2 = x.split(",").collect::>()[1].split("-").collect::>(); 26 | 27 | let r11 = elf1[0].parse::().unwrap(); 28 | let r12 = elf1[1].parse::().unwrap(); 29 | let r21 = elf2[0].parse::().unwrap(); 30 | let r22 = elf2[1].parse::().unwrap(); 31 | 32 | let a = (r21 >= r11) && (r12 - r11 >= r21 - r11); 33 | let b = (r11 >= r21) && (r22 - r21 >= r11 - r21); 34 | 35 | return a || b; 36 | }).count(); 37 | 38 | println!("Part 1: {sol1}"); 39 | println!("Part 2: {sol2}"); 40 | } 41 | -------------------------------------------------------------------------------- /mynam3isg00d/d07/d07.py: -------------------------------------------------------------------------------- 1 | dirstack = [] 2 | sizes = [] 3 | part1 = 0 4 | part2 = 0 5 | 6 | def finsize(): 7 | final = dirstack.pop() 8 | sizes.append(final) 9 | dirstack.append(dirstack.pop() + final) 10 | if (final < 100_000): 11 | return final 12 | else: 13 | return 0 14 | 15 | for cmd in open("input.txt"): 16 | if ("$ cd " in cmd): 17 | if (".." in cmd): 18 | part1 += finsize() 19 | else: 20 | dirstack.append(0) 21 | else: 22 | try: 23 | n = int(cmd.split(" ")[0]) 24 | dirstack.append(dirstack.pop() + n) 25 | except: 26 | do_nothing_lol = 0 27 | 28 | while (len(dirstack) > 1): 29 | part1 += finsize() 30 | sizes.append(dirstack[0]) 31 | 32 | tofree = sorted(sizes)[-1] - 40_000_000 33 | for size in sorted(sizes)[::-1]: 34 | if size > tofree: 35 | part2 = size 36 | else: 37 | break 38 | 39 | print(f"Part 1: {part1}") 40 | print(f"Part 2: {part2}") -------------------------------------------------------------------------------- /mynam3isg00d/d09/d09.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def sign(a): 4 | if (a == 0): 5 | return 0 6 | return abs(a)/a 7 | 8 | def d09(n): 9 | pos = [(0, 0)] 10 | knots = [(0, 0) for i in range(0, n)] 11 | for instr in open("input.txt"): 12 | for i in range(0, int(instr[1:])): 13 | for k in range(1, len(knots)): 14 | head = knots[k-1] 15 | tail = knots[k] 16 | if (k == 1): 17 | knots[k-1] = head = (head[0] + (instr[0] == "R" or instr[0] == "L") * 2 * (0.5 - (instr[0] == "L")), 18 | head[1] + (instr[0] == "U" or instr[0] == "D") * 2 * (0.5 - (instr[0] == "D"))) 19 | if (math.dist(head, tail) > math.sqrt(2)): 20 | knots[k] = (tail[0] + sign(head[0] - tail[0]), tail[1] + sign(head[1] - tail[1])) 21 | if (k == len(knots)-1): 22 | pos.append(knots[k]) 23 | return len(set(pos)) 24 | 25 | print(f"Part 1: {d09(2)}") 26 | print(f"Part 2: {d09(10)}") -------------------------------------------------------------------------------- /riccardo-negri/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | -------------------------------------------------------------------------------- /riccardo-negri/d01/x.go: -------------------------------------------------------------------------------- 1 | // 2 | // Day 1 solution of Advent Of Code 2022 by Riccardo Negri 3 | // First part solution: 73211 4 | // Second part solution: 213958 5 | // 6 | 7 | package main 8 | 9 | import ( 10 | "bufio" 11 | "fmt" 12 | "log" 13 | "os" 14 | "sort" 15 | "strconv" 16 | "strings" 17 | ) 18 | 19 | func main() { 20 | // open file 21 | f, err := os.Open("input.txt") 22 | if err != nil { 23 | log.Fatal(err) 24 | } 25 | 26 | // close the file at the end of the program 27 | defer func(f *os.File) { 28 | err := f.Close() 29 | if err != nil { 30 | log.Fatal(err) 31 | } 32 | }(f) 33 | 34 | // read the file line by line using scanner 35 | scanner := bufio.NewScanner(f) 36 | temp := 0 37 | var sums []int 38 | for scanner.Scan() { 39 | line := scanner.Text() 40 | line = strings.Replace(line, "\n", "", -1) 41 | if line == "" { 42 | sums = append(sums, temp) 43 | temp = 0 44 | } else { 45 | num, err := strconv.ParseInt(line, 10, 64) 46 | if err != nil { 47 | log.Fatal(err) 48 | } 49 | temp += int(num) 50 | } 51 | } 52 | 53 | err = scanner.Err() 54 | if err != nil { 55 | log.Fatal(err) 56 | } 57 | 58 | sort.Ints(sums) 59 | 60 | // part 1 61 | fmt.Println(sums[len(sums)-1]) 62 | 63 | // part 2 64 | sum := 0 65 | i := 0 66 | for i < 3 { 67 | sum += sums[len(sums)-1-i] 68 | i += 1 69 | } 70 | fmt.Println(sum) 71 | } 72 | -------------------------------------------------------------------------------- /riccardo-negri/d02/x.go: -------------------------------------------------------------------------------- 1 | // 2 | // Day 2 solution of Advent Of Code 2022 by Riccardo Negri 3 | // First part solution: 11906 4 | // Second part solution: 11186 5 | // 6 | 7 | package main 8 | 9 | import ( 10 | "bufio" 11 | "fmt" 12 | "log" 13 | "os" 14 | "strings" 15 | ) 16 | 17 | func main() { 18 | // open file 19 | f, err := os.Open("input.txt") 20 | if err != nil { 21 | log.Fatal(err) 22 | } 23 | 24 | // close the file at the end of the program 25 | defer func(f *os.File) { 26 | err := f.Close() 27 | if err != nil { 28 | log.Fatal(err) 29 | } 30 | }(f) 31 | 32 | // read the file line by line using scanner 33 | score1 := 0 34 | score2 := 0 35 | scanner := bufio.NewScanner(f) 36 | for scanner.Scan() { 37 | line := scanner.Text() 38 | line = strings.Replace(line, "\n", "", -1) 39 | shapes := strings.Split(line, " ") 40 | 41 | // part 1: A and X are rock, B and Y are paper, C and Z are scissors 42 | shapeScore := map[string]int{ 43 | "X": 1, 44 | "Y": 2, 45 | "Z": 3, 46 | } 47 | // winning combinations 48 | if (shapes[0] == "A") && (shapes[1] == "Y") || (shapes[0] == "B") && (shapes[1] == "Z") || (shapes[0] == "C") && (shapes[1] == "X") { 49 | score1 += 6 50 | } 51 | // tie combinations 52 | if (shapes[0] == "A") && (shapes[1] == "X") || (shapes[0] == "B") && (shapes[1] == "Y") || (shapes[0] == "C") && (shapes[1] == "Z") { 53 | score1 += 3 54 | } 55 | // shape score1 56 | score1 += shapeScore[shapes[1]] 57 | 58 | // part 2: X you loose, Y you draw, Z you win 59 | values := map[string]int{ 60 | "A": 0, 61 | "B": 1, 62 | "C": 2, 63 | } 64 | if shapes[1] == "Y" { // draw 65 | score2 += 3 66 | score2 += values[shapes[0]] + 1 67 | } else if shapes[1] == "Z" { // win 68 | score2 += 6 69 | score2 += (values[shapes[0]]+1)%3 + 1 70 | } else { // lose 71 | tmp := values[shapes[0]] - 1 72 | if tmp < 0 { 73 | tmp = 2 74 | } 75 | score2 += tmp%3 + 1 76 | } 77 | } 78 | 79 | err = scanner.Err() 80 | if err != nil { 81 | log.Fatal(err) 82 | } 83 | 84 | fmt.Println(score1) 85 | fmt.Println(score2) 86 | } 87 | -------------------------------------------------------------------------------- /riccardo-negri/d03/x.go: -------------------------------------------------------------------------------- 1 | // 2 | // Day 3 solution of Advent Of Code 2022 by Riccardo Negri 3 | // First part solution: 8401 4 | // Second part solution: 2641 5 | // 6 | 7 | package main 8 | 9 | import ( 10 | "bufio" 11 | "fmt" 12 | "log" 13 | "os" 14 | "strings" 15 | ) 16 | 17 | func getPriority(value int) int { 18 | // 65 is A, 90 is Z, 97 is a 19 | if value >= 65 && value <= 90 { 20 | return value - 65 + 27 21 | } else { 22 | return value - 97 + 1 23 | } 24 | } 25 | 26 | func main() { 27 | score1 := 0 28 | score2 := 0 29 | lineNumber := 0 30 | var group [3]string 31 | 32 | // open file 33 | f, err := os.Open("input.txt") 34 | if err != nil { 35 | log.Fatal(err) 36 | } 37 | 38 | // close the file at the end of the program 39 | defer func(f *os.File) { 40 | err := f.Close() 41 | if err != nil { 42 | log.Fatal(err) 43 | } 44 | }(f) 45 | 46 | // read the file line by line using scanner 47 | scanner := bufio.NewScanner(f) 48 | for scanner.Scan() { 49 | line := strings.Replace(scanner.Text(), "\n", "", -1) 50 | 51 | // part 1 52 | for _, value := range line[:len(line)/2] { 53 | fmt.Println(string(value)) 54 | if strings.Contains(line[len(line)/2:], string(value)) { 55 | score1 += getPriority(int(value)) 56 | break 57 | } 58 | } 59 | 60 | // part 2 61 | group[lineNumber%3] = line 62 | if lineNumber%3 == 2 { 63 | for _, value := range group[0] { 64 | if strings.Contains(group[1], string(value)) && strings.Contains(group[2], string(value)) { 65 | score2 += getPriority(int(value)) 66 | break 67 | } 68 | } 69 | } 70 | 71 | lineNumber += 1 72 | } 73 | 74 | err = scanner.Err() 75 | if err != nil { 76 | log.Fatal(err) 77 | } 78 | 79 | fmt.Println(score1) 80 | fmt.Println(score2) 81 | } 82 | -------------------------------------------------------------------------------- /riccardo-negri/d04/x.go: -------------------------------------------------------------------------------- 1 | // 2 | // Day 4 solution of Advent Of Code 2022 by Riccardo Negri 3 | // First part solution: 453 4 | // Second part solution: 919 5 | // 6 | 7 | package main 8 | 9 | import ( 10 | "bufio" 11 | "fmt" 12 | "log" 13 | "os" 14 | "strconv" 15 | "strings" 16 | ) 17 | 18 | func main() { 19 | score1 := 0 20 | score2 := 0 21 | var numbers [4]int 22 | 23 | // open file 24 | f, err := os.Open("input.txt") 25 | if err != nil { 26 | log.Fatal(err) 27 | } 28 | 29 | // close the file at the end of the program 30 | defer func(f *os.File) { 31 | err := f.Close() 32 | if err != nil { 33 | log.Fatal(err) 34 | } 35 | }(f) 36 | 37 | // read the file line by line using scanner 38 | scanner := bufio.NewScanner(f) 39 | for scanner.Scan() { 40 | line := strings.Replace(scanner.Text(), "\n", "", -1) 41 | sections := strings.Split(strings.ReplaceAll(line, ",", "-"), "-") 42 | for ind, value := range sections { 43 | numbers[ind], _ = strconv.Atoi(value) 44 | } 45 | 46 | // part 1 47 | ab := numbers[1] - numbers[0] 48 | cd := numbers[3] - numbers[2] 49 | if ab > cd { 50 | if numbers[2] >= numbers[0] && numbers[3] <= numbers[1] { 51 | score1 += 1 52 | } 53 | } else { 54 | if numbers[0] >= numbers[2] && numbers[1] <= numbers[3] { 55 | score1 += 1 56 | } 57 | } 58 | 59 | // part 2 60 | i := 0 61 | for i < 4 { 62 | j := i / 2 * 2 63 | if 0 >= (numbers[i]-numbers[(j+2)%4])*(numbers[i]-numbers[(j+3)%4]) { 64 | score2 += 1 65 | break 66 | } 67 | i += 1 68 | } 69 | } 70 | 71 | err = scanner.Err() 72 | if err != nil { 73 | log.Fatal(err) 74 | } 75 | 76 | fmt.Println(score1) 77 | fmt.Println(score2) 78 | } 79 | -------------------------------------------------------------------------------- /riccardo-negri/d05/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "d05" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /riccardo-negri/d06/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "d06" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /riccardo-negri/d06/input.txt: -------------------------------------------------------------------------------- 1 | mqllsjlslffbqbsbpbcbdbfbfvbfblfltffpddsmmhjmjvmvsmvvjfvfjjfccblbddhrdhrrlcrllrrswwlpwlwwgrglrgllrrlsshffrwfwcffmrrdfdrrncrnnlvllbsbcbwcchsshsrhhbnhbbqbmmmfdfvvqpqlpqppfgpfgpfpfttwrwwwfmfpfpmmpgglwglgfgsfgsggdllfhhmchmhttlhlchhrzrszsqqqzdzrrbbpgpccmfcmcbcdcrdrzzvjjgjfgfqfcqffvbbjbjbjsbjjbhjjzjlzjznzbbjvvpssdfdvffssrffpzfppsddgwgzgccrmrmdrrzpzbbjlbjlbjbgjbjmmtmllqffpjfppzzztfzfzfmfnndmdtmmhgmmsdshdhppbnpnbpnpdndgdbdwdtwwzqqjwjpphnnjddlglzlczzdpdjdjtjtgtqtptrrbqqqspsfswsdwdjdsdcdllpzpqplqqjnjrnnvrrnrccnwnnsttqzqcqppvmvdmdjjpnppljppptpzzwbwrrqgqhghshlslblflhflfbbwhwlhwhbwbdwbdwwzlwzwhzwznwwmmrddlttdrtrsttqbqtqtdqttqwwglwlbbdmbmcbbwbnbtbmbggdqqnhqqptpdtttvcvmvmdmtdttcwclccjcrctcllrprlrqqzqccttgnntstrtqqtfthtghhvcczhhctclljcljlcjcdjcdchhmjmjjrsjszjszsbsqqpfflqffqvfqvqjjnjtthvvvcjcjrcjclcwcrcrjcrchrcrggcjjpjjbwjbwjbwjwsjjrssfggzbgzgzrzmrmwmrrczcppnmnmzzgtglljlhjlhhgzgtgddrrhvvvlhlwhlhzzgzfgzzbvbvrvllplnlqnnfvnvgvdgvddsdqsdscscjjjvrvpptgpgdgtdtdzdtztfzfszfftjftfmftmtftddjzdzssfllfsllpwpwswrwdrwrcclczlclpccdrrftrftrtltftnftfnfrnfrfgfjfttrsrqsshlhdhnnztntwwnwppwbbmrbbtntznzggthghfggttpnpnpbplpvvpmmpwmbgcpwgsfndbrclcwbdcfhlcqblplglnqpnrpjqbddfqlqvbzrtwbwzvwqntcgmzrzztlffzmfmcmfzrmcvfctmlrlbtbpsgddbqrlblsslsbcmcglzdzjzlpgzprbrmfmlzrssqddzfjzfgbpvdgrrnldmtqgtjppqqwtzbltpfgpqtdqpwhbbwblnvvpmnljdghwrbnphswhgcvhpcplbbmwprznzzwnfntfplscpflhwdmlvfwtgrjhchnmnqbfgvsglllnnzwchqtcrvqzzhttcmblcthqrjdbvpwptcqtsnwrnfbbsqlshhtqdvcfcgdlbgzqjvzvglbcdwzpzttjnsvwrdldcqqstnnfnjthncgfvggphgfgstnmvnbmtvhpmsgmrccmmslqmjfzdjnbcjbjnpmsnvmzrphhjrdrrssnclvwbnzvpccqglnpljdtwrlnvpqzlshpcmfnmrjchqvlmthqbdrlnnpwcmfnwfzpbpnrsdmrqgqsjgwttwhgqlwghjntrvdndfhdwfzbwnmbjlzbhhdqfrdtwcjjvfnjbqdmdwncfhmslflvhqdmrcdrcdrldnqdmhzsvlglgflmlhwjqvfjdmqbmgffvdmmsbrrnrlcsbncvsjffttmhnbpwmqrnvdmzhztbbsrtwgfshjnlvhqvzwpvrmqfbsszswvrglnmwlmcdpjvmqsgnjshspzwrwctwwghmgjvbthcqcrlflsnrnpwvbnghrhvzpzchjlcljplflzqdvglgtvczhnbnlqltblddslqmdpvfbstvszqdsjvgfqlmdgbsnlzlrnbbqqfqjfqhljzlpbbgbnchwljjcpzbhdmwfzmqstcwtvgtvwcpgvmhpsngrshjvzzngbhjqmcfgjjzgdzcsbsvfwmznmwnnvlbntvcmgphqmdfjvhrlldcpwgnmbpjlqflvsrwqphvlpzlsdthfzdzvlphzdbqldvggsgrcmmfmfnjsfszqqbhnmntfgrbfwtlpqgwnrcqdsmqpqbtfdsnhbdcbwcdrhrfgsctrnlchrrnlptbcnqhndcpcdrgtznqrbgjlwzsjhblptncwtqcqcbzccrnjcmfvfnzwlrgdtgcvvcprwvnrrbdjzfnlvlqfpgbpwsvcnmnmmhnshtjgrcnscljwncdjqtwhlhvcggnwbzlzvfqmcdhmzddrdhvnnjbzbtnrgqcbmzhzzfldhlwwsgztfhncgctvjvszdzhrqmzvffmhvsqssjjvrrmtwqswhwjqgbfghbgfmgqssfhbcrglnbstfnqzvwqcznzgtnvjdvhtrlmgthcrqcwbjnzddsqhzwmdwndqcplhvpbpsdthngqwmlfqfndfqbpbwwrvsrnsjbsrwjdjbcqcvdfcsscgblggwggtmbntnbmmswfhvzhltwvprdgvzwltchhzsqlpwdndwftmsgbfwbpmhsdjhwbvvpzlpspsrsnpbwtdspfvdqdjfjbzmmtbnpzrqngccrbfndnjbcjfvwjvfjdvmsqdvgctzvpzmjmjvggpqfmmrsvqbrrlwrmzhmhpcmpltwdbtmwgzrrvsdhvhlwzggjwqzpbzvzrdbptzhzcrrjwjmdwdpsfwfspjgtmfcvddgspldbldtbtwrzdsjrbhvvcjgnrsbzvbrnqjwhrzgfsbdjlfqlszvlnrbfcrgfwrsmqmmnrwbtvfdpjzpfbhplfdsrwwgqqtgnzvddbgjjllmmcjjlglwmsbwrdrnnznwzplnbhlrlnmnllwgwgdpqdqqlmvsbgcshsmntrrlrvdhjgctzsfhmvfqtthvvchftflhlqqhhhbhqvgwtcmgcfwldhgptfddpsqrfzqmtpszswfrztzfsspltcvjwwsljsnjpnnqggscwwmcwfrljlrtqwqvplthsctvbndjfpnvcbdngzqtgjvwlsdhthdwmjvtnzrplwzwsfmgszpqjcjttslsmtbbvhjgpqmqfjbcccsnrlwmjhbsqgzqldmlhnbjnjfwmgzpvdcwndbwcncmtzccngcghhpwmjnncfgqtdtzwmhbdrpwsfbnjzfnwzwqncnlfjqjrjhgnqgvbcdhgdnbwpqjcfgprmfhzlrqtwlqpshfrgdszrwdtqfcntrzbgzlvrhtlsbjjwtnlqllnsvbzwjlmqvdgvtslmbwwcfstmqntwwwsjmrflrqnttfzjchpgwczzdtqbhdrtrpvhhbscvjtdtrhbstpqrnrzszwvcqzhbrzhlblvzrgwtqzbslbmgdqhpfqrdqrzcsbglcsshcwvlcpgjtjmcgpmsnldjzlwnrqlzzznpvmgssvzshjvtsmmzvstpqrhfvttnsrddfcqcbwhgpfdtlhcvcgjgdrvvntvdjqpvwvfmphhpzjgmshddqfsbpjbzrfdjnwrhmgcfbccmzqgvrbmcjdpvwfrtdpbwvjtjcrmnpzrrqbbvbsgcplwmlbsdwptbprlczjcqhdzprpttvnthbmtscdtjvrnwqhnvqbzvwnphnzwlgvvjhddjvjrvwlmhqcsffcnhgjzdjppqqwbglbhgzsmvzwjdvbqpztphshtrbrrhzmdlfdtssbhrcltwlqpzvpgbsgngpfjsjbrnnlzctqcqzwswhfnjjngwsztdgmmcffqfhbsgwstnflqjqttzbtgjvcfrrdwzcvhwjnhmtphszrsptjsqqwcwfnmtlzvzsqsmghtztrpvdslrmjqqvwfmzlwwjbwtpmhtqcfctdztsnfrhfqwqcjdzmjhvwwgrslmdqqwgwfvwlzzsznmdrzgcvbmrtcvjsqlftnpdhwmrzjwsnjjdrczbjcwhwlrtljwjsfmcfcrsjflsldbjrzpdgltmhtszzznjjlfqmgpbjfjncvtvlcfsmltbsvsrgdhwwhcpbdbntqhgjztvlwtwdsgqfwtlcdzffcszjmjvj 2 | -------------------------------------------------------------------------------- /riccardo-negri/d06/src/main.rs: -------------------------------------------------------------------------------- 1 | // 2 | // Day 6 solution of Advent Of Code 2022 by Riccardo Negri 3 | // First part solution: 1109 4 | // Second part solution: 3965 5 | // 6 | 7 | use std::collections::{HashSet}; 8 | use std::fs::File; 9 | use std::io::{BufRead, BufReader}; 10 | 11 | fn main() { 12 | let filename = "input.txt"; 13 | let mut part1= true; 14 | 15 | // Open the file in read-only mode (ignoring errors). 16 | let file = File::open(filename).unwrap(); 17 | let reader = BufReader::new(file); 18 | 19 | 20 | // Read the file line by line using the lines() iterator from std::io::BufRead. 21 | let lines = reader.lines().enumerate(); 22 | for (_, line) in lines { 23 | let line = line.unwrap(); // Ignore errors. 24 | 25 | for i in 3..line.len() { 26 | let mut counts1 : HashSet = HashSet::new(); 27 | let mut counts2 : HashSet = HashSet::new(); 28 | 29 | // part 1 30 | for j in 0..4 { 31 | let c = line.as_bytes()[i-j] as char; 32 | counts1.insert(c,); 33 | } 34 | if counts1.len() == 4 && part1 { 35 | part1 = false; 36 | println!("{}", i + 1); 37 | } 38 | 39 | // part 12 40 | for j in 0..14 { 41 | if j <= i { 42 | let c = line.as_bytes()[i-j] as char; 43 | counts2.insert(c,); 44 | } 45 | } 46 | if counts2.len() == 14 { 47 | println!("{}", i + 1); 48 | break 49 | } 50 | 51 | 52 | } 53 | } 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /riccardoinghilleri/d01/main.py: -------------------------------------------------------------------------------- 1 | temp = 0 2 | best_elves = [0, 0, 0] 3 | with open('input.txt') as file: 4 | for line in file: 5 | if line == '\n': 6 | if temp > min(best_elves): 7 | best_elves[best_elves.index(min(best_elves))] = temp 8 | temp = 0 9 | else: 10 | temp += int(line[:-1]) 11 | if temp > min(best_elves): 12 | best_elves[best_elves.index(min(best_elves))] = temp 13 | print(max(best_elves)) 14 | print(sum(best_elves)) 15 | -------------------------------------------------------------------------------- /riccardoinghilleri/d02/main.py: -------------------------------------------------------------------------------- 1 | def compare_1(a, b): 2 | global results, values 3 | if a == b: 4 | results[0] += 3 5 | elif (values[b] - values[a]) == 1 or (values[b] - values[a]) == -2: 6 | results[0] += 6 7 | results[0] += values[b] 8 | 9 | 10 | def compare_2(a, b): 11 | global results, values 12 | if b == 'A': 13 | if a == 'A': 14 | results[1] += values["C"] 15 | else: 16 | results[1] += values[chr(ord(a) - 1)] 17 | elif b == 'B': 18 | results[1] += 3 + values[a] 19 | else: 20 | if a == 'C': 21 | results[1] += 6 + values["A"] 22 | else: 23 | results[1] += 6 + values[chr(ord(a) + 1)] 24 | 25 | 26 | values = {"A": 1, "B": 2, "C": 3} 27 | results = [0, 0] 28 | with open('input.txt') as file: 29 | for line in file: 30 | line = line.replace("X", "A").replace("Y", "B").replace("Z", "C").split() 31 | compare_1(line[0], line[1]) 32 | compare_2(line[0], line[1]) 33 | print(results[0]) 34 | print(results[1]) 35 | -------------------------------------------------------------------------------- /riccardoinghilleri/d03/main.py: -------------------------------------------------------------------------------- 1 | results = [0, 0] 2 | group = [] 3 | with open('input.txt') as file: 4 | for line in file: 5 | line = line.replace("\n", "") 6 | group.append(line) 7 | for c in line[:int(len(line) / 2)]: 8 | if c in line[int(len(line) / 2):]: 9 | if 'A' <= c <= 'Z': 10 | results[0] += ord(c) - 38 11 | else: 12 | results[0] += ord(c) - 96 13 | break 14 | if len(group) == 3: 15 | for c in group[0]: 16 | if (c in group[1]) and (c in group[2]): 17 | if 'A' <= c <= 'Z': 18 | results[1] += ord(c) - 38 19 | else: 20 | results[1] += ord(c) - 96 21 | break 22 | group.clear() 23 | print(results[0]) 24 | print(results[1]) 25 | -------------------------------------------------------------------------------- /riccardoinghilleri/d04/main.py: -------------------------------------------------------------------------------- 1 | results = [0, 0] 2 | with open('input.txt') as file: 3 | for line in file: 4 | line = line.replace("-", ",").split(",") 5 | if (int(line[2]) >= int(line[0]) and int(line[3]) <= int(line[1])) or (int(line[0]) >= int(line[2]) and int(line[1]) <= int(line[3])): 6 | results[0] += 1 7 | if (int(line[0]) <= int(line[2]) <= int(line[1])) or (int(line[2]) <= int(line[0]) <= int(line[3])): 8 | results[1] += 1 9 | print(results) 10 | -------------------------------------------------------------------------------- /riccardoinghilleri/d05/main.py: -------------------------------------------------------------------------------- 1 | import copy 2 | part_1 = [] 3 | setup = True 4 | with open('input.txt') as file: 5 | for line in file: 6 | if len(line) == 1 or line[1] == "1": 7 | setup = False 8 | for elem in part_1: 9 | while ' ' in elem: 10 | elem.remove(' ') 11 | part_2 = copy.deepcopy(part_1) 12 | elif setup: 13 | j = 0 14 | for i in range(1, len(line), 4): 15 | if len(part_1) <= j: 16 | part_1.append(list()) 17 | part_1[j].insert(0, line[i]) 18 | j += 1 19 | else: 20 | line = line.replace("\n", "").replace("move", "").replace("from", "").replace("to", "") 21 | line = line.split() 22 | for i in range(int(line[0])): 23 | part_1[int(line[2])-1].append(part_1[int(line[1])-1].pop()) 24 | temp = part_2[int(line[1]) - 1][-int(line[0]):] 25 | part_2[int(line[1]) - 1] = part_2[int(line[1]) - 1][:-int(line[0])] 26 | for i in range(int(line[0])): 27 | part_2[int(line[2]) - 1].append(temp.pop(0)) 28 | for column in part_1: 29 | print(column[-1], end="") 30 | print() 31 | for column in part_2: 32 | print(column[-1], end="") 33 | -------------------------------------------------------------------------------- /riccardoinghilleri/d06/input.txt: -------------------------------------------------------------------------------- 1 | pnnfhnhshrhmhwwmwzmznmnwmwfmfhfjfcjjtgtbggpdgdjjbjrjsjpjrrmddmgmpmddrhddnfnfzfpfvpfpprhhlffmtffqhhdtdcdsswsdwswmmfvvpdprrnnhhhtffnfbbznbznnvdnnbffjrfrbfrbrgbrrntnggrqqwtqwwgjgsswgwqwtwwsvwvbwvwrwlrlppzfzwfzzpmzzhqqzqlzlglzzmrmwrmwwvmwvvnppjfjttlffhjjjsccbggnffqgfgjjnccmdmzmllvnlnznttlvlttvnvgnvvqvmvqqzrqqcgglzzwtztwwmjmzjjnddsffqrqlrrvsvdvldvvlgvlvccdzczcqcpphggtnthhhtbhtttcjtjcjgcjcbbrhbbfrffgjgdgzddcttczzsccbpcpddcpcggmjgjddtcccthccfrccmdmhmddnwddfldffntnptnpttcptcptpfphhmfmwfwmmlblgbgvbvlltqltldttfcfcclgcllmplmlbbjnjzjnzzttnvvgddshddsqddggsqgqddsggdhghjgjhhgchhdmdjmjddgdhghrrphrrpnnqhhjwwqrrcmmslmmszzpgzpzzrmzmznzllnjnnlnbbdvdsdffbpffcmmnznqqcbbzvvjnjvvwqwgqqpnnzwnzwnzwwwlpwpzzfqzfqqwnqnbnfnqnbqqbggqnqdndrrzzlffbbgqgfgrfrqqsddnqnqjjgssqwqwcqcpcrrqppwpjjfnfpffhphwwmcwwznwznnplptlplnlsnlsldslslsffwtwftwtbtdbbjsjcczwwfllwtlwtlwlvwlwrwppsggvcvrrqcrqqvmqvmmbrrsbsfspspjpnnmpmqmcczgzffqmfmtmpppwzppzrpzrzsrrpqrpqpmpvmpvpttbqtqmtmjttqdqgggcppclcjcpcsppctpplpdpcppcmmdzmzddvhvhnhrrldllcwwbnwnssshlhrhthggtmggbjbjwwbvbttjllvrrfggngvngnmmvzzrrmddmcddztztctfccqpcpcqqvqppqcqdcdhhvhssgfgzgwwzmmnssvwwbqbhbnhnphpqqjcqcddfwfttqjtqtlttglljgjbgjgnnsqqvrvffqvqfqbffljjpffssqdsqstqqqldqqmhmsmsqqwtqwqdqgdgjjfbjffgbbrhhqghgppqgpgmpmmfzfhhfrhffgmfggpzgpzzhtzhhlbhhqbbzvvnvqnntptmmbhbdhdwwmjjcnnmsscqcbbtjtvjvwjvvmsmjmtmpmgghttcztzggpddbfbfgbbdsdrsrrqfqjjqfjjhzhtzzmdzzcgzgdzzmvmmfmjmgjmggmppbdppmzpppvfppzhhfsfwfhhpjpmmrjjpssdccjpjpwjppvdpphcpcjcfjfwjfwfjwfjjqcqcwqqqsmmmbbgdgwwpcwwdfdlflrltlgtthfhfjhhlthtddlgdggsjsrrdpdcppgttphpgpwppmpzmmrjmmvjvgvgfglfgllbqlqhllszzlwwhzzdfdcdtctptwtztfzzmjzjtjtrjrcrnrjjmwmnnbddgvgtgsstjsszmpqdmzgqflrbrspjmtzjcrmlzltmhgblghnwqvwwqwzbpnfrpdpblpjgshfccfbjfsnwvvhnjftsdnsgtzzjtzpmtfdvzrhtqpblhwgmqtgpbfvbdmsnrrrvvbstpsznvbbwgjfqjrhdvwvgptpglpfddhddmtglmjlpwlvfpbtbmgbplbzrlpdlvqzcwhbscpszgfstjpfdvfpmljlngrbgrdnnblzqrfpzsdvblpwbtnhdjclldvwvbwcwzfzbdspgwpfqjfbdbrqcshtlvcrdstnzggbwqnzbrfzbpnrtmvpbvdhcvdsdshgtvhfgdzljflppqbwclnvbhbczvrscjhlbgbfvwdjhnjsgmvwhpfgwbbmnndpnglfrmtfdzvqgfjdqfhgrhvpbqndmqnqccgwswwdsqjnbjtjbjdbqgjnmfbdvlnfwbnrdqgvgzzhmmbbdzfdvvpwhpbwbnzdcdpchrwlhfsjnhhjggvplmqggwjdsvjtpnpnqgldjjdcscrdltssjdrpcrfbgbcjfplhzgwbprfcslhpcngtszrghmwhzdqscbfrhzdwcffzvmjrmcjcstfvhplvrsglgsjnjtrpddsdfqjsndjnfmvdhfgdbzzflqhsrrwmrnlpqzmcddqbqvvzgtlztpgjnddtcnbmqsjlhmcszrmcjvwzpptlfqsmpvgnzvrjdwzpdwqgbmdgdtvjlmfczthjbcgfhbqpnmlbmrwwhfptzlbmfdhssznjcvjbmnjtnvzjhzczlrrdnttmmcbnzhqpplzqwgttwrnwfvmnptgqlfrnzvqpjfgrzwmlcwvtptvcvrlsrdwdgqfvffspmdbnnrqjttpqvhvdpbcrvzptwnhhfsqzchmncvttcdgdnlppcfzpmjpvbvqhlvplwvrmmbbggbwttwmvsqjlllsftprsmtmnzjcqfzblrllzgshfljchrjwjlpvhpbrtrsschzltrblgjnbgdnmwdggjhqggntblnhsvfgsbcblhmctbqzqwmhqnjhpzjfqpjdgwpzhczcftfcpdhvzhzccmwmrfrbqshzmtpqgpbbvfqqbjbmvnlnlwjtzrpmhdlffccrqcfgsjfszbrzrfztntchtmgmbhjgmlsqzcbtqqjzzlghtzzqmlnnvsgsvbbjfgqsqbqmqrdzwpwdgbggpdvhvnlzshhntprjdwhnwfvdjzpqgflwrvwgtmfdmfdztcbtfnjdrvgdwwczdgphnvdgrbdchprqldfjrvcsflcmlcmzqvqgsgnzcgmrhccgcmptcdzhbcdgdtppwztfstzqqzqrdzlnzthggjmpcflmbcmdrrjnnpbpqfmjbzqbtsjjgdlmgncbmgspqqvbrvzrdjscpzjsdtcdvsdwqlmwrngttswnrsbqctvhgfnnwblpcqzdmzpfchplslspmghvgcqntmlrfhgpcbpspvfhnvqvglsqzsnsdzddqpbsjhlclslngbwvvgjhwfcncqsmqwbptzvpzlzslsjjjldjpwpfrdlfbjphqcjtsgqdsdfdjhqgdhcppndwmhmmldvvmblcqcqfqhltbcbvrnghjfmtgqwtwljtczvqlnmgscjhqdhnzwhzvzzqnlsrhqvljqpgpwghfqlhjjrrhvnmnnrbnlhdcjctwtlhmhhmhjvcgzdrzmdjrvqzgnsttjdwglgwlcmbcdnjprgfsbbdzzngbqdrvwwwhbtlnnmzqdjttsrrpvlfdqnfhhtdtvmpcjgdwtbnqmwmtszdqfmbhjsjpqqddzfggwjhbtlnqfgcwbjzdtcpcpzgnrmnvwlpgmwfjlpgppdfrfvvjwsfcdqdnpcpjbqsvhttssgptqjghctrbgntlfjzdrfjccsprsjlrrwrzsmnjsqslmpdtrvhlqbnmgpjthpqdqmnvrtzlhhzzfzbrcclpmpcszhbttgrtcpgcpjwpdbfpfvgspsgtvglwthqcmcvmrfmclwlvjlsptfgmtlrnsvjrnfwzhdcsmgztpzfcvzwdztpppvqpvqfpdrsfnlhrbqwrsqjtwjmhnpwmqmpdgdhbtbpfwnmswffdqffdggrdrpmngvpzplmmwlddnhcvjjzqqfsbbtfmzdwnpvbjrshmllczhgvwwcbcbtfrfnplqjwmjlvpwwgfrtffwddwppsgtnlmpvfnhfzcsgjbqbjmbvpnqppsrvwnlzvcmjqgtbzrdsnrgwbfmrvnflgccrssfvcwgllqqbbcthzmbtnsmbzbcczhtzcvmthttpltrtdmgspctvtpvqbhmnnpnjwmhpqclmjsdrbjwvjbtzcjlqbjsvbgdwqzflnwzcfjwtrhjgfshfmwbjfwpnhjsmtpgbpwlfjjnmdlrhchmnfmgmgcrftmwbzshdwbhndgwtjbrrvbwprqppfmgfmfllpcjgrwdmtzddthsjlgjljv -------------------------------------------------------------------------------- /riccardoinghilleri/d06/main.py: -------------------------------------------------------------------------------- 1 | def mark(length): 2 | global input 3 | for i in range(length - 1, len(input)): 4 | found = True 5 | str = input[i - (length - 1): i + 1] 6 | for ch in str: 7 | if str.count(ch) > 1: 8 | found = False 9 | break 10 | if found: 11 | return i + 1 12 | 13 | 14 | with open('input.txt') as file: 15 | input = file.read() 16 | print(mark(4)) 17 | print(mark(14)) 18 | -------------------------------------------------------------------------------- /riccardoinghilleri/d07/main.py: -------------------------------------------------------------------------------- 1 | import copy 2 | 3 | 4 | class TreeNode(object): 5 | def __init__(self, name='root', children=None, parent=None, size=0): 6 | self.name = name 7 | self.parent = parent 8 | self.children = [] 9 | self.size = size 10 | if children is not None: 11 | for child in children: 12 | self.add_child(child) 13 | 14 | def is_root(self): 15 | if self.parent is None: 16 | return True 17 | else: 18 | return False 19 | 20 | def add_child(self, node): 21 | node.parent = self 22 | assert isinstance(node, TreeNode) 23 | self.children.append(node) 24 | if (node.name.split())[0].isdigit(): 25 | self.size += int((node.name.split())[0]) 26 | self.update(int((node.name.split())[0])) 27 | 28 | def update(self, quantity): 29 | if not self.is_root(): 30 | self.parent.size += quantity 31 | self.parent.update(quantity) 32 | 33 | def check_path(self, path2): 34 | if self.parent is None and len(path2) == 0: 35 | return True 36 | elif self.parent is not None and len(path2) != 0: 37 | if self.parent.name == path2[-1]: 38 | path2.pop() 39 | return self.parent.check_path(path2) 40 | return False 41 | 42 | 43 | class Tree: 44 | def __init__(self): 45 | self.root = None 46 | self.nodes = [] 47 | 48 | def insert(self, node, parent): 49 | if parent is not None: 50 | parent.add_child(node) 51 | else: 52 | if self.root is None: 53 | self.root = node 54 | self.nodes.append(node) 55 | if (node.name.split())[0].isdigit(): 56 | self.root.size += int((node.name.split())[0]) 57 | 58 | def search(self, path1): 59 | index = -1 60 | for n in self.nodes: 61 | index += 1 62 | temp = copy.deepcopy(path1) 63 | temp.pop() 64 | if n.name == path1[-1] and n.check_path(temp): 65 | break 66 | if index == len(self.nodes) - 1: 67 | return -1 68 | else: 69 | return index 70 | 71 | 72 | tree = Tree() 73 | tree.insert(TreeNode('root'), None) 74 | max = 100000 75 | commands = ["$ cd /", "$ cd ..", "$ ls"] 76 | path = [] 77 | i = 0 78 | with open('input.txt') as file: 79 | input = file.readlines() 80 | while i < len(input): 81 | input[i] = input[i].replace("\n", '') 82 | if input[i] == commands[0]: 83 | path.clear() 84 | elif input[i] == commands[1]: 85 | path.pop() 86 | elif input[i][0] == '$' and input[i] not in commands: 87 | path.append("dir " + input[i][5:]) 88 | elif input[i] == commands[2]: 89 | i += 1 90 | while i < len(input) and input[i][0] != '$': 91 | input[i] = input[i].replace("\n", '') 92 | if len(path) != 0: 93 | tree.insert(TreeNode(input[i]), tree.nodes[tree.search(path)]) 94 | else: 95 | tree.insert(TreeNode(input[i]), None) 96 | i += 1 97 | i -= 1 98 | i += 1 99 | part_1 = sum(nd.size for nd in tree.nodes if nd.size <= max) 100 | total_space = 70000000 101 | needed_space = 30000000 102 | used_space = tree.root.size 103 | available_space = total_space - used_space 104 | needed_space -= available_space 105 | part_2 = tree.root.size 106 | for nd in tree.nodes: 107 | if needed_space <= nd.size < part_2: 108 | part_2 = nd.size 109 | print(part_1) 110 | print(part_2) 111 | -------------------------------------------------------------------------------- /riccardoinghilleri/d08/main1.py: -------------------------------------------------------------------------------- 1 | with open('input.txt') as file: 2 | input = file.readlines() 3 | for i in range(len(input)): 4 | input[i] = input[i].replace("\n", "") 5 | input[i] = list(map(int, [*input[i]])) 6 | visible_trees = 2 * len(input) + (len(input[0]) - 2) * 2 7 | for i in range(1, len(input)-1): 8 | for j in range(1, len(input[i])-1): 9 | visible = [True, True, True, True] 10 | for k in range(i): 11 | if input[k][j] >= input[i][j]: 12 | visible[0] = False 13 | break 14 | for k in range(j): 15 | if visible[0] is True: 16 | break 17 | if input[i][k] >= input[i][j]: 18 | visible[1] = False 19 | break 20 | for k in range(i + 1, len(input)): 21 | if visible[1] is True: 22 | break 23 | if input[k][j] >= input[i][j]: 24 | visible[2] = False 25 | break 26 | for k in range(j + 1, len(input[i])): 27 | if visible[2] is True: 28 | break 29 | if input[i][k] >= input[i][j]: 30 | visible[3] = False 31 | break 32 | if True in visible: 33 | visible_trees += 1 34 | print(visible_trees) 35 | -------------------------------------------------------------------------------- /riccardoinghilleri/d08/main2.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | with open('input.txt') as file: 4 | input = file.readlines() 5 | for i in range(len(input)): 6 | input[i] = input[i].replace("\n", "") 7 | input[i] = list(map(int, [*input[i]])) 8 | max = 0 9 | for i in range(len(input)): 10 | for j in range(len(input[i])): 11 | temp = [0, 0, 0, 0] 12 | for k in range(i-1, -1, -1): 13 | if input[k][j] < input[i][j]: 14 | temp[0] += 1 15 | else: 16 | temp[0] += 1 17 | break 18 | for k in range(j-1, -1, -1): 19 | if input[i][k] < input[i][j]: 20 | temp[1] += 1 21 | else: 22 | temp[1] += 1 23 | break 24 | for k in range(i + 1, len(input)): 25 | if input[k][j] < input[i][j]: 26 | temp[2] += 1 27 | else: 28 | temp[2] += 1 29 | break 30 | for k in range(j + 1, len(input[i])): 31 | if input[i][k] < input[i][j]: 32 | temp[3] += 1 33 | else: 34 | temp[3] += 1 35 | break 36 | prod = math.prod(temp) 37 | if prod > max: 38 | max = prod 39 | prod = 0 40 | print(max) 41 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | import platform 3 | import os 4 | import stat 5 | import re 6 | 7 | # TODO: check for location 8 | 9 | # First part: setup pre-commit hook 10 | text = '''#!/bin/sh 11 | # update leaderboard before commit 12 | python3 leaderboard.py 13 | git add README.md 14 | ''' 15 | 16 | print("Welcome to the leaderboard automatic updater setup!") 17 | if platform.system() == 'Windows': 18 | text = text.replace('python3', 'python') 19 | 20 | path = pathlib.Path('.git/hooks/pre-commit') 21 | fp = open(path, 'w') 22 | fp.write(text) 23 | fp.close() 24 | 25 | if platform.system() != 'Windows': 26 | st = os.stat(path) 27 | os.chmod(path, st.st_mode | stat.S_IEXEC) 28 | 29 | 30 | # Second part: write cookies to file if needed 31 | COOKIE_FILE = 'cookies' 32 | 33 | INSTRUCTIONS = "\nTo find your cookie log in to Advent of Code website (https://adventofcode.com/), open inspection, " \ 34 | "go to network tab and reload the page. You can find your cookie in the request headers of any " \ 35 | "request you made to the website. It should look like this:\nsession=" \ 36 | "53616c0065342f5fa8b77787d920039e9c9576c51gef4ed378c0e7eed5c38dfhh27bb5381d6edfh600720d59a11feb42\n" 37 | print(INSTRUCTIONS) 38 | cookies = "empty" 39 | wrong = False 40 | while not (re.match("session=[a-z0-9]*", cookies) or cookies == ""): 41 | if wrong: 42 | print("This isn't a valid cookie please retry") 43 | wrong = True 44 | try: 45 | with open(COOKIE_FILE, "r") as input_file: 46 | current = input_file.readlines()[0] 47 | cookies = input("New cookies (press ENTER to keep current): ") 48 | except (IndexError, FileNotFoundError): 49 | cookies = input("Cookies not found, enter your session cookies: ") 50 | cookies = cookies.strip("Cookie: ") 51 | 52 | if cookies != "": 53 | with open(COOKIE_FILE, 'w') as output_file: 54 | output_file.write(cookies) 55 | 56 | 57 | print("Hooks setup finished!") 58 | -------------------------------------------------------------------------------- /zoythum/d01/solve.py: -------------------------------------------------------------------------------- 1 | from sys import exit 2 | 3 | with open('input.txt') as f: 4 | data = [x.strip() for x in f.readlines()] 5 | 6 | def part_one(data): 7 | curr_max = 0 8 | curr = 0 9 | 10 | for cal in data: 11 | 12 | if cal == '': 13 | curr_max = curr_max if curr < curr_max else curr 14 | curr = 0 15 | continue 16 | 17 | curr += int(cal) 18 | 19 | print(curr_max) 20 | 21 | def part_two(data): 22 | calories = [] 23 | curr = 0 24 | 25 | for cal in data: 26 | 27 | if cal == '': 28 | calories.append(curr) 29 | curr = 0 30 | continue 31 | 32 | curr += int(cal) 33 | 34 | print(sum(sorted(calories, reverse=True)[:3])) 35 | 36 | 37 | part_two(data) --------------------------------------------------------------------------------