day 5: should have gone for python for both part

This commit is contained in:
setop 2024-12-06 11:35:13 +01:00
parent 1153fac5fe
commit 97c949ed32
2 changed files with 59 additions and 0 deletions

29
d05/part1.awk Normal file
View File

@ -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<i;j++) {
if ($i in O) {
if (index(O[$i],$j",")) {
correct = 0
}
}
}
}
if (!correct) {
print
}
S += correct * $((NF+1)/2)
}
END {
print S > "/dev/stderr"
}

30
d05/part2.py Normal file
View File

@ -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)