diff --git a/d05/part1.awk b/d05/part1.awk new file mode 100644 index 0000000..7c977c6 --- /dev/null +++ b/d05/part1.awk @@ -0,0 +1,29 @@ +NR == FNR { + # 47|53 + #print $0, $1, "before", $2 + O[$1] = O[$1] $2 "," + next +} + +{ + correct = 1 + for (i=2;i<=NF;i++) { + if ($i in O) + split(O[$i],A,",") + for(j=1;j "/dev/stderr" +} diff --git a/d05/part2.py b/d05/part2.py new file mode 100644 index 0000000..c307683 --- /dev/null +++ b/d05/part2.py @@ -0,0 +1,30 @@ +import sys +from collections import defaultdict as dd +from functools import cmp_to_key as ck + +# load rules +R = dd(list) +L = open("orders","rt").read().strip().split("\n") +for l in L: + [a,b] = l.split("|") + R[int(a)].append(int(b)) + + +def order(a,b): + if a in R: + for i in R[a]: + if (i==b): + return -1 + return 1 + +S = 0 +# load incorrect lines +I = open("input2","rt").read().strip().split("\n") +for l in I: + # sort line + l = map(int,l.split(",")) + l = sorted(l, key=ck(order)) + # take middle + S += l[(len(l)+1)//2 -1] +print(S) +