diff options
author | Aleteoryx <alyx@aleteoryx.me> | 2024-12-02 13:14:36 -0500 |
---|---|---|
committer | Aleteoryx <alyx@aleteoryx.me> | 2024-12-02 13:14:36 -0500 |
commit | 06366944524a30fcc496c82d0ff0e888ce7b7d9e (patch) | |
tree | 3f7ea27a12ff4a0b3a45ebb30af801f2a0b1b703 /2024/tcl/02.tcl | |
parent | 61ec27be506c76f73eda8fdb216b82d8fcbf6ff4 (diff) | |
download | adventofcode-06366944524a30fcc496c82d0ff0e888ce7b7d9e.tar.gz adventofcode-06366944524a30fcc496c82d0ff0e888ce7b7d9e.tar.bz2 adventofcode-06366944524a30fcc496c82d0ff0e888ce7b7d9e.zip |
2024.2
Diffstat (limited to '2024/tcl/02.tcl')
-rwxr-xr-x | 2024/tcl/02.tcl | 47 |
1 files changed, 47 insertions, 0 deletions
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" |