Advent of Code 22
Day 1
Part 1 - Perl
while(<STDIN>){unless($_=~/^$/){$c+=$_}else{if($c>$p){$p=$c};$c=0;}}print$pPart 2 - Perl
while(<STDIN>){unless($_=~/^$/){$c+=$_}else{if($c>$v[0]){unshift(@v,$c)}elsif($c>$v[1]){$v[2]=$v[1];$v[1]=$c}elsif($c>$v[2]){$v[2]=$c};$c=0;}}print$v[0]+$v[1]+$v[2]Part 1 - COBOL
IDENTIFICATION DIVISION. PROGRAM-ID. APP. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SYSIN ASSIGN TO KEYBOARD ORGANIZATION LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD SYSIN. 01 ln PIC X(255). 88 EOF VALUE HIGH-VALUES. WORKING-STORAGE SECTION. 01 current PIC 9(9) VALUE 0. 01 previous PIC 9(9) VALUE 0. 01 lnNumeric PIC 9(9) VALUE 0. PROCEDURE DIVISION. OPEN INPUT SYSIN READ SYSIN AT END SET EOF TO TRUE END-READ PERFORM UNTIL EOF IF ln NOT EQUAL TO SPACE MOVE ln TO lnNumeric ADD lnNumeric TO current ELSE IF current > previous MOVE current to previous END-IF SET current to 0 END-IF READ SYSIN AT END SET EOF TO TRUE END-READ END-PERFORM DISPLAY previous CLOSE SYSIN STOP RUN.
Day 2
Part 1 - Shell and Perl
sed -r 's/X|A/1/g;s/Y/2/g;s/Z/3/g;s/B/2/g;s/C/3/g' i2.txt | perl -e '''while(<STDIN>){$m=substr($_,2,3);$o=substr($_,0,1);$r=$m+$o;if($m==$o){$s+=3}elsif($r==5){if($m==3){$s+=6}}elsif($r==3){if($ m==2){$s+=6}}elsif($r==4){if($m==1){$s+=6}}$s+=$m}print $s'''Part 2 - Shell and Perl
sed -r 's/X|A/1/g;s/Y|B/2/g;s/Z|C/3/g;' i2.txt | perl -e '''while(<STDIN>){$m=substr($_,2,3);$o=substr($_,0,1);if($m==1){if($o==1){$m=3}elsif($o==3){$m=2}}elsif($m==2){$m=$o}else{if($o ==1){$m=2}elsif($o==3){$m=1}}$r=$m+$o;if($m==$o){$s+=3}elsif($r==5){if($m==3){$s+=6}}elsif($r==3){if($m==2){$s+=6}}elsif($r==4){if($m==1){$s+=6}}$s+=$m}print$s'''Day 3
Part 1 - Shell (fish)
for i in (cat i3.txt); set len (math (string length $i)/2); set a (string split '' (string sub -e $len $i)); set b (string split '' (string sub -s (math $len+1) $i)); set co 0; for j in $a; if test $co -eq 1; break; else; for k in $b; if test $j = $k; echo $j|od -An -vtu1|tr -s " "|cut -d " " -f 2|read r; if test $r -gt 96; math $r-96; else; math $r-38; end; set co 1; break; end; end; end; end; end|paste -s -d"+"|bc;
Part 2 - Python
result = 0 for group in [open('i3.txt').readlines()[i:i + 3] for i in range(0, 300, 3)]: badge = ord(min(set(group[0].strip('\n'))&set(group[1])&set(group[2]))) result += badge-96 if badge>96 else badge-38 print(result)
Day 4
Part 1 & 2 - Haskell
import System.IO import Data.List import Data.List.Split fromRange xs = [xs!!0..xs!!1] toInt str = read str :: Integer partOf xs = (intersect (xs!!0) (xs!!1) == (xs!!0)) || (intersect (xs!!1) (xs!!0) == (xs!!1)) countMatches xs f = length (filter (\x -> x == True) (map f xs)) overlap xs = length (intersect (xs!!0) (xs!!1)) > 0 splitSections xs = (map (splitOn ",") (lines xs)) splitRange xs = (map . map) (splitOn "-") (splitSections xs) parseInput xs = (map . map) (fromRange) ((map . map . map) (toInt) (splitRange xs)) main = do infile <- readFile "i4.txt" print (countMatches (parseInput infile) partOf) print (countMatches (parseInput infile) overlap)Day 5
Part 1 - Python
with open('i5.txt') as f: inp = f.read().split('\n\n') stacks = [ [] for _ in range(int(inp[0][-2])) ] for row in reversed(inp[0].split('\n')[:-1]): c = 0 for i in range(1, len(row), 4): if row[i].isalnum(): stacks[c].append(row[i]) c += 1 for cmd in inp[1].split('\n')[:-1]: cmd = cmd.split(' ') for i in range(int(cmd[1])): crate = stacks[int(cmd[3])-1].pop() stacks[int(cmd[5])-1].append(crate) out = '' for stack in stacks: out = out + stack.pop() print(out)Part 2 - Python
with open('i5.txt') as f: inp = f.read().split('\n\n') stacks = [ [] for _ in range(int(inp[0][-2])) ] for row in reversed(inp[0].split('\n')[:-1]): c = 0 for i in range(1, len(row), 4): if row[i].isalnum(): stacks[c].append(row[i]) c += 1 for cmd in inp[1].split('\n')[:-1]: cmd = cmd.split(' ') tmp = [ [] for _ in range(int(cmd[1]))] for i in range(int(cmd[1])): tmp[i] = stacks[int(cmd[3])-1].pop() stacks[int(cmd[5])-1].extend(reversed(tmp)) out = '' for stack in stacks: out = out + stack.pop() print(out)Day 6
Part 1 & 2 - Python
with open('i6.txt') as f: text = f.read() marklen = 14 for i in range(marklen-1,len(text[:-1])): if (len(list(set(text[:i+1][-marklen:]))))>marklen-1: print(i+1) breakDay 7
I'm tired and this is haaaard :(
Update: I did come back to this in summer 2023, and while it still twisted my brain real good I did implement a solution for day 7. that one was really nasty.
def updateSize(node): size = 0 for child in [x for x in tree[node][3] if isinstance(x, tuple)]: size = size + updateSize(child[1]) files = sum([int(x.split()[0]) for x in tree[node][3] if isinstance(x,str)]) size = size + files tree[node][1] = size return size with open('i7.txt') as f: inp = f.read().split('\n') tree = [["/",0,None,[]]] current = 0 path = [] i = 1 while i < len(inp): if(inp[i].startswith("$ cd")): arg = inp[i].split()[2] if(arg == ".."): current = path.pop() else: path.append(current) current = next(x for x in tree[current][3] if isinstance(x,tuple) and x[0] == arg)[1] elif(inp[i].startswith("$ ls")): skip = 0 for element in inp[i+1:]: if("$" not in element): if(element.startswith("dir")): tree.append([element.split()[1],0,current,[]]) tree[current][3].append((element.split()[1],len(tree)-1)) elif(element): tree[current][3].append(element) skip = skip+1 else: i = i+skip break i = i+1 updateSize(0) print(sum([x[1] for x in tree if x[1] < 100000])) print(min([x[1] for x in tree if x[1] >= (30000000-(70000000 - tree[0][1]))]))