aboutsummaryrefslogtreecommitdiff
path: root/2018
diff options
context:
space:
mode:
Diffstat (limited to '2018')
-rwxr-xr-x2018/01/puzzle-1.awk4
-rwxr-xr-x2018/01/puzzle-2.awk14
-rwxr-xr-x2018/02/puzzle-1.pl19
-rwxr-xr-x2018/02/puzzle-2.py26
4 files changed, 63 insertions, 0 deletions
diff --git a/2018/01/puzzle-1.awk b/2018/01/puzzle-1.awk
new file mode 100755
index 0000000..128d7e2
--- /dev/null
+++ b/2018/01/puzzle-1.awk
@@ -0,0 +1,4 @@
+#!/usr/bin/awk -f
+
+{ x += $1 }
+END { print x }
diff --git a/2018/01/puzzle-2.awk b/2018/01/puzzle-2.awk
new file mode 100755
index 0000000..2236499
--- /dev/null
+++ b/2018/01/puzzle-2.awk
@@ -0,0 +1,14 @@
+#!/usr/bin/awk -f
+
+{ xs[NR] = $1 }
+
+END {
+ for (;;) {
+ for (i = 1; i <= length(xs); i++) {
+ if (ys[x += xs[i]]++) {
+ print x
+ exit
+ }
+ }
+ }
+}
diff --git a/2018/02/puzzle-1.pl b/2018/02/puzzle-1.pl
new file mode 100755
index 0000000..85a533b
--- /dev/null
+++ b/2018/02/puzzle-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my $file = "input";
+open my $in, "<", $file or die "Failed to open ‘$file’: $!";
+
+my ($x, $y) = (0) x 2;
+while (<$in>) {
+ chomp;
+ my %freq;
+ $freq{$_}++ for split //;
+ $x++ if grep { $_ == 2 } values %freq;
+ $y++ if grep { $_ == 3 } values %freq;
+}
+
+close $in;
+printf "%d\n", $x * $y;
diff --git a/2018/02/puzzle-2.py b/2018/02/puzzle-2.py
new file mode 100755
index 0000000..37ce920
--- /dev/null
+++ b/2018/02/puzzle-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/python3
+
+import itertools
+
+
+def main() -> None:
+ with open('input', 'r') as f:
+ xs = f.readlines()
+ print(next(
+ x for x in
+ itertools.starmap(common, itertools.combinations(xs, 2))
+ if x is not None
+ ), end='')
+
+
+def common(x: str, y: str) -> str | None:
+ it = (i for i, (a, b) in enumerate(zip(x, y)) if a != b)
+ i = next(it)
+ try:
+ next(it)
+ except StopIteration:
+ return x[:i] + x[i+1:]
+
+
+if __name__ == '__main__':
+ main()