From 06366944524a30fcc496c82d0ff0e888ce7b7d9e Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Mon, 2 Dec 2024 13:14:36 -0500 Subject: 2024.2 --- 2024/tcl/02.tcl | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 2024/tcl/02.tcl (limited to '2024/tcl/02.tcl') diff --git a/2024/tcl/02.tcl b/2024/tcl/02.tcl new file mode 100755 index 0000000..dac290e --- /dev/null +++ b/2024/tcl/02.tcl @@ -0,0 +1,47 @@ +#!/bin/env tclsh + +source lib.tcl +setup 2 + +puts {Part 1: Count "safe" ordered sequences with intervals such that 1 <= x <= 3.} + +proc row_errs {row} { + set report [lassign $row first second] + set neg [expr {abs($second - $first) != ($second - $first)}] + set prev $first + + set row_errs 0 + foreach num [concat $second $report] { + if {$neg != (abs($num - $prev) != ($num - $prev)) || + abs($num - $prev) > 3 || abs($num - $prev) < 1} { + incr row_errs + } else { + set prev $num + } + } + return $row_errs +} + +while {[gets $input report] != -1} { + if ![row_errs $report] { incr safe_count; continue } + # naive, but handles a case like [1 4 2 3] where removing 2 makes it + # safe but an iterative approach only trips on [2]. backtracking + # might be better but idc + for {set i 0} {$i < [llength $report]} {incr i} { + if ![row_errs [lreplace $report $i $i]] { + incr dampened_safe_count + break + } + } +} + +incr dampened_safe_count $safe_count + +close $input + +puts "Safe sequence count: $safe_count" + +puts "" +puts {Part 2: Tolerate 1 bad entry in a report.} + +puts "Dampened sequence count: $dampened_safe_count" -- cgit v1.2.3-70-g09d2