summaryrefslogtreecommitdiffstats
path: root/2024/tcl/01.tcl
diff options
context:
space:
mode:
authorAleteoryx <alyx@aleteoryx.me>2024-12-01 04:40:41 -0500
committerAleteoryx <alyx@aleteoryx.me>2024-12-01 04:45:25 -0500
commit61ec27be506c76f73eda8fdb216b82d8fcbf6ff4 (patch)
tree2f05234cef3e2f3840773e6c01344f5a3616779b /2024/tcl/01.tcl
parent7b3e48d29d080d2cd9b57258f670c18f6fefc7aa (diff)
downloadadventofcode-61ec27be506c76f73eda8fdb216b82d8fcbf6ff4.tar.gz
adventofcode-61ec27be506c76f73eda8fdb216b82d8fcbf6ff4.tar.bz2
adventofcode-61ec27be506c76f73eda8fdb216b82d8fcbf6ff4.zip
2024.1
Diffstat (limited to '2024/tcl/01.tcl')
-rwxr-xr-x2024/tcl/01.tcl33
1 files changed, 33 insertions, 0 deletions
diff --git a/2024/tcl/01.tcl b/2024/tcl/01.tcl
new file mode 100755
index 0000000..462a7a9
--- /dev/null
+++ b/2024/tcl/01.tcl
@@ -0,0 +1,33 @@
+#!/bin/env tclsh
+
+source lib.tcl
+setup 1
+
+puts {Part 1: Find the sum of distances between pairs of numbers with the same ordinal in each list.}
+
+while {[gets $input line] != -1} {
+ lassign [regexp -inline -all {\d+} $line] ln rn
+
+ lappend left_nums $ln
+ lappend right_nums $rn
+
+ dict incr right_num_counts $rn
+}
+
+close $input
+
+set left_nums [lsort -integer $left_nums]
+set right_nums [lsort -integer $right_nums]
+
+foreach ln $left_nums rn $right_nums {
+ incr total1 [expr {abs($ln - $rn)}]
+
+ if [dict exists $right_num_counts $ln] {
+ incr total2 [expr {$ln * [dict get $right_num_counts $ln]}]
+ }
+}
+
+puts "Sum of distances: $total1"
+puts ""
+puts {Part 2: Find the "Similarity Score" of the lists.}
+puts "Similarity Score: $total2"