from __future__ import annotations import json from pathlib import Path from awkweave.execution import run_awk LOGS_1 = """192.0.2.10 - - [28/Nov/2026:10:00:00 +0000] \"GET /index.html HTTP/1.1\" 200 512 192.0.2.11 - - [28/Nov/2026:10:00:01 +0000] \"POST /api/orders HTTP/1.1\" 201 128 192.0.2.10 - - [28/Nov/2026:10:00:02 +0000] \"GET /missing HTTP/1.1\" 404 64 192.0.2.12 - - [28/Nov/2026:10:00:03 +0000] \"GET /api/orders HTTP/1.1\" 500 32 192.0.2.13 - - [28/Nov/2026:10:00:04 +0000] \"GET /index.html HTTP/1.1\" 200 256 """ LOGS_2 = """198.51.100.20 - - [28/Nov/2026:11:00:00 +0000] \"GET /health HTTP/1.1\" 200 10 198.51.100.21 - - [28/Nov/2026:11:00:01 +0000] \"GET /api/items HTTP/1.1\" 503 0 198.51.100.20 - - [28/Nov/2026:11:00:02 +0000] \"POST /api/items HTTP/1.1\" 201 99 198.51.100.22 - - [28/Nov/2026:11:00:03 +0000] \"GET /health HTTP/1.1\" 200 20 """ SALES_1 = """order_id region amount status r001 east 12.50 paid r002 west 20.00 pending r003 east 7.50 paid r004 north 10.00 paid r005 west 5.00 paid """ SALES_2 = """order_id region amount status r101 south 8.00 paid r102 east 14.00 paid r103 south 6.00 pending r104 west 22.00 paid """ PASSWD_1 = """root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin alice:x:1000:1000:Alice:/home/alice:/bin/bash bruno:x:1001:1001:Bruno:/home/bruno:/bin/zsh chen:x:1002:1002:Chen:/home/chen:/bin/bash """ PASSWD_2 = """root:x:0:0:root:/root:/bin/bash dina:x:1000:1000:Dina:/home/dina:/bin/sh eli:x:1001:1001:Eli:/home/eli:/bin/bash """ EVENTS_1 = """2026-11-28T09:00:00Z level=info service=web latency=12 2026-11-28T09:01:00Z level=error service=db latency=40 2026-11-28T09:02:00Z level=INFO service=web latency=18 2026-11-28T09:03:00Z level=warn service=db latency=25 """ EVENTS_2 = """2026-11-28T12:00:00Z level=error service=api latency=33 2026-11-28T12:01:00Z level=info service=api latency=9 2026-11-28T12:02:00Z level=ERROR service=worker latency=51 """ ROWS_1 = """alpha 10 4 beta 20 0 alpha 30 8 gamma 5 2 beta 15 3 """ ROWS_2 = """red 8 2 red 12 4 blue 7 0 green 20 5 blue 9 3 """ def main() -> None: output = Path("challenge.jsonl") cases = [] def add(family: str, request: str, program: str, input_text: str, validation_input: str, difficulty: int = 3): expected = run_awk(program, input_text) validation_expected = run_awk(program, validation_input) if not expected.strip(): raise ValueError(f"Empty challenge output: {family}") index = len(cases) + 1 cases.append({ "id": f"awkweave-challenge-{index:04d}", "request": request, "program": program, "input_text": input_text, "expected_output": expected, "validation_input": validation_input, "validation_output": validation_expected, "family": family, "difficulty": difficulty, "split": "challenge", "source": "original-hand-authored", "license": "CC-BY-4.0", }) add("http_5xx_count", "Count server-error responses with HTTP status from 500 through 599; print 0 when none occur.", '$9 >= 500 && $9 < 600{n++}END{print n+0}', LOGS_1, LOGS_2) add("http_bytes_sum", "What is the total number of response bytes? Sum the final field and print one integer.", '{bytes+=$NF}END{print bytes+0}', LOGS_1, LOGS_2) add("http_unique_clients", "Count distinct client IP addresses, which appear in field 1.", '{if(!seen[$1]++)n++}END{print n+0}', LOGS_1, LOGS_2) add("http_api_errors", "Print the request path (field 7) for API routes with a 500 response.", '$7 ~ /^\/api\// && $9 == 500{print $7}', LOGS_1, LOGS_2) add("http_success_bytes", "For responses with status 200 only, print the average number of bytes to two decimal places.", '$9==200{bytes+=$10;n++}END{printf "%.2f\\n",bytes/n}', LOGS_1, LOGS_2) add("http_non_get", "Print complete request records for methods other than GET.", '$6 !~ /^"GET$/{print $0}', LOGS_1, LOGS_2) add("http_url_counts", "Count each request path from field 7 and print path then count in first-appearance order.", '{if(!($7 in counts))order[++n]=$7;counts[$7]++}END{for(i=1;i<=n;i++)print order[i],counts[order[i]]}', LOGS_1, LOGS_2) add("header_region_totals", "Use the header to find the region and amount columns. Print total paid amount per region in first-appearance order.", 'NR==1{for(i=1;i<=NF;i++)h[$i]=i;next}$h["status"]=="paid"{key=$h["region"];if(!(key in sums))order[++n]=key;sums[key]+=$h["amount"]}END{for(i=1;i<=n;i++)print order[i],sums[order[i]]}', SALES_1, SALES_2) add("header_pending_ids", "The first line is a header. Print order_id values for pending orders only.", 'NR==1{for(i=1;i<=NF;i++)h[$i]=i;next}$h["status"]=="pending"{print $h["order_id"]}', SALES_1, SALES_2) add("header_amount_average", "Read the amount column by its header name and print its mean over all data rows to two decimal places.", 'NR==1{for(i=1;i<=NF;i++)h[$i]=i;next}{s+=$h["amount"];n++}END{printf "%.2f\\n",s/n}', SALES_1, SALES_2) add("top_two_amounts", "The first row is a header. Print the two complete orders with the largest amounts, highest first. Keep file order for ties.", 'NR==1{next}{n++;v[n]=$3+0;line[n]=$0}END{for(i=2;i<=n;i++){x=v[i];s=line[i];j=i-1;while(j>=1&&v[j]=1000&&$3<1002{print $1,$3}', PASSWD_1, PASSWD_2) add("passwd_shell_counts", "For colon-delimited passwd records, count users per login shell in order of first appearance.", 'BEGIN{FS=":"}{if(!($7 in counts))order[++n]=$7;counts[$7]++}END{for(i=1;i<=n;i++)print order[i],counts[order[i]]}', PASSWD_1, PASSWD_2) add("keyvalue_errors", "Extract the service value from each record whose level value is error, ignoring level letter case.", '{service="";level="";for(i=1;i<=NF;i++){split($i,parts,"=");if(parts[1]=="service")service=parts[2];if(parts[1]=="level")level=tolower(parts[2])}if(level=="error")print service}', EVENTS_1, EVENTS_2) add("keyvalue_latency", "Print timestamps with latency above 20. The timestamp is field 1 and latency is stored as latency=N in the final field.", '{split($NF,parts,"=");if(parts[2]+0>20)print $1}', EVENTS_1, EVENTS_2) add("event_window", "Print only events with timestamps from 2026-11-28T09:01:00Z through 2026-11-28T09:02:00Z inclusive.", '$1>="2026-11-28T09:01:00Z"&&$1<="2026-11-28T09:02:00Z"', EVENTS_1, EVENTS_2) add("running_delta", "For each row after the first, print the group name and the change in column 2 from the previous row; for the first row print 0 as the change.", '{delta=$2-previous;if(NR==1)delta=0;print $1,delta;previous=$2}', ROWS_1, ROWS_2) add("group_maximum", "For each group in column 1, print its largest value from column 2 in first-appearance order.", '{if(!($1 in max)||$2>max[$1])max[$1]=$2;if(!($1 in seen)){order[++n]=$1;seen[$1]=1}}END{for(i=1;i<=n;i++)print order[i],max[order[i]]}', ROWS_1, ROWS_2) add("first_last_group", "Print each group from column 1 with its first and last value from column 2, in first-appearance order.", '{if(!($1 in first)){first[$1]=$2;order[++n]=$1}last[$1]=$2}END{for(i=1;i<=n;i++)print order[i],first[order[i]],last[order[i]]}', ROWS_1, ROWS_2) add("positive_ratio", "Print group and the ratio of column 3 to column 2 as a percentage with one decimal; skip zero denominators.", '$2!=0{printf "%s %.1f%%\\n",$1,100*$3/$2}', ROWS_1, ROWS_2) add("clamp_values", "Print every row with column 2 clamped to the inclusive range 8 through 18; leave other fields unchanged.", '{if($2<8)$2=8;else if($2>18)$2=18;print}', ROWS_1, ROWS_2) add("consecutive_dedup", "Suppress only consecutive duplicate group names in column 1, printing one complete row per run.", '$1!=previous{print}{previous=$1}', ROWS_1, ROWS_2) blank_1 = "alpha 1\n\nbeta 2\n \nalpha 3\n" blank_2 = "red 4\n\n\nblue 5\n" add("blank_filter", "Remove blank and whitespace-only lines, leaving all other lines unchanged.", '$0 ~ /[^[:space:]]/', blank_1, blank_2) add("blank_count", "Count blank or whitespace-only lines and print the count.", '$0 !~ /[^[:space:]]/{n++}END{print n+0}', blank_1, blank_2) add("line_numbers", "Prefix each non-empty line with its original line number, a colon, and one space.", '$0 ~ /[^[:space:]]/{print NR ": " $0}', blank_1, blank_2) add("character_count", "Print the total number of characters in the input, excluding newline characters.", '{total+=length($0)}END{print total+0}', blank_1, blank_2) add("comma_to_tab", "Convert simple comma-separated rows to tab-separated rows, preserving every field in order.", 'BEGIN{FS=",";OFS="\\t"}{$1=$1;print}', "a,1,x\nb,2,y\n", "c,3,z\nd,4,w\n") add("fixed_width", "Print column 1 right-aligned in a width of 8, followed by column 2 with no extra separator beyond the padding.", '{printf "%8s%s\\n",$1,$2}', ROWS_1, ROWS_2) add("absolute_difference", "For every record, print group and the absolute difference between columns 2 and 3.", '{difference=$2-$3;if(difference<0)difference=-difference;print $1,difference}', ROWS_1, ROWS_2) add("sorted_unique_words", "Print each distinct word from column 1 once, sorted in ascending lexical order.", '{seen[$1]=1}END{for(word in seen){value[++n]=word}for(i=2;i<=n;i++){x=value[i];j=i-1;while(j>=1&&value[j]>x){value[j+1]=value[j];j--}value[j+1]=x}for(i=1;i<=n;i++)print value[i]}', ROWS_1, ROWS_2) with output.open("w", encoding="utf-8") as handle: for case in cases: handle.write(json.dumps(case, ensure_ascii=False, separators=(",", ":")) + "\n") print(f"wrote {len(cases)} hand-authored challenge cases to {output}") if __name__ == "__main__": main()