summaryrefslogtreecommitdiffstats
path: root/2024/tcl/01.tcl
blob: bf7e82f9f33e2d3b7ce642beaed5bc16665f7143 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/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.}

foreach line $input {
  lassign $line ln rn

  lappend left_nums $ln
  lappend right_nums $rn

  dict incr right_num_counts $rn
}

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"