From b358e6c50cafa9b4c5e5ac42e41c885968cf655d Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Mon, 2 Dec 2024 06:29:12 +0100 Subject: [PATCH] Day 2: complicated conditionals --- 2024/03.pl | 26 ++++++++++++++++++++++++++ 2024/04.pl | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 2024/03.pl create mode 100755 2024/04.pl diff --git a/2024/03.pl b/2024/03.pl new file mode 100755 index 0000000..ef9e4fd --- /dev/null +++ b/2024/03.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl -w + +use v5.40; + +my $safe; + +LEVEL: +while (<>) { + my $inc; + my $prev; + for (/\d+/g) { + if (defined $prev) { + next LEVEL if abs($prev - $_) > 3; + next LEVEL if abs($prev - $_) < 1; + if (defined $inc) { + next LEVEL if $inc && $_ < $prev; + next LEVEL if !$inc && $_ > $prev; + } + $inc = $_ > $prev; + } + $prev = $_; + } + $safe++; +} + +say $safe; diff --git a/2024/04.pl b/2024/04.pl new file mode 100755 index 0000000..f04bbd4 --- /dev/null +++ b/2024/04.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl -w + +use v5.40; + +my $safe; + +LEVEL: +while (<>) { + my @l = /\d+/g; + if (is_safe(@l)) { + $safe++; + next LEVEL; + } + for my $i (0 .. $#l) { + my @l1 = @l; + splice (@l1, $i, 1); + if (is_safe(@l1)) { + $safe++; + next LEVEL; + } + } +} + +say $safe; + +sub is_safe { + my $inc; + my $prev; + for (@_) { + if (defined $prev) { + return 0 if abs($prev - $_) > 3; + return 0 if abs($prev - $_) < 1; + if (defined $inc) { + return 0 if $inc && $_ < $prev; + return 0 if !$inc && $_ > $prev; + } + $inc = $_ > $prev; + } + $prev = $_; + } + return 1; +} -- 2.43.5