From c8c36a069e2771aca576243cebf2823d5dbd5472 Mon Sep 17 00:00:00 2001 From: setop Date: Sat, 3 Dec 2022 16:01:59 +0100 Subject: [PATCH] day 3 --- d03/part1.py | 16 ++++++++++++++++ d03/part2.awk | 25 +++++++++++++++++++++++++ d03/part2.py | 18 ++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 d03/part1.py create mode 100644 d03/part2.awk create mode 100644 d03/part2.py diff --git a/d03/part1.py b/d03/part1.py new file mode 100644 index 0000000..6fe8bbe --- /dev/null +++ b/d03/part1.py @@ -0,0 +1,16 @@ +import sys + +def p(c): + o = ord(c) + if o > 96: + return o-96 + else: + return o-64+26 + +def gen(): + for line in sys.stdin: + (l,r) = line[:len(line)//2], line[len(line)//2:-1] + b = set(l) & set(r) + yield list(b)[0] + +print(sum(p(c) for c in gen())) diff --git a/d03/part2.awk b/d03/part2.awk new file mode 100644 index 0000000..7e003a7 --- /dev/null +++ b/d03/part2.awk @@ -0,0 +1,25 @@ +NR %3 == 1 { split($0, arr, "") + for (a in arr) { + A[arr[a]]=1 + } +} +NR %3 == 2 { split($0, arr, "") + for (a in arr) { + B[arr[a]]=1 + } +} +NR %3 == 0 { + split($0, arr, "") + for (a in arr) { + c = arr[a] + if ((c in A) && (c in B)) { + comm = c + } + } + i = index("abcdefghijklmnopqrstuvwxyz", comm) + j = index("ABCDEFGHIJKLMNOPQRSTUVWXYZ", comm)+26 + S += i > 0 ? i : j + delete A + delete B +} +END { print S} diff --git a/d03/part2.py b/d03/part2.py new file mode 100644 index 0000000..510bf46 --- /dev/null +++ b/d03/part2.py @@ -0,0 +1,18 @@ +import sys + +def p(c): + o = ord(c) + if o > 96: + return o-96 + else: + return o-64+26 + +def gen(): + for _ in range(100): + g1 = next(sys.stdin)[:-1] + g2 = next(sys.stdin)[:-1] + g3 = next(sys.stdin)[:-1] + b = set(g1) & set(g2) & set(g3) + yield list(b)[0] + +print(sum(p(c) for c in gen()))