blob: e62ed69e4d346f8bd14db136220f49724d941b7a (
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#!/bin/env tclsh
source lib.tcl
setup 8 grid
puts {Part 1: Antinode count in map}
set antinodes {}
set resonant_antinodes {}
set antennae {}
for {set x 0} {$x < $input(w)} {incr x} {
for {set y 0} {$y < $input(h)} {incr y} {
set cell [lindex $input(grid) $y $x]
if {[regexp {[a-zA-Z0-9]} $cell]} {
if [dict exists $antennae $cell] {
foreach antenna [dict keys [dict get $antennae $cell]] {
lassign $antenna x2 y2
dict set resonant_antinodes [list $x $y] 1
dict set resonant_antinodes [list $x2 $y2] 1
set xoff [expr {$x - $x2}]
set yoff [expr {$y - $y2}]
set ax [expr {$x + $xoff}]
set ay [expr {$y + $yoff}]
if {$ax >= 0 && $ax < $input(w) && $ay >= 0 && $ay < $input(h)} {
dict set antinodes [list $ax $ay] 1
while {$ax >= 0 && $ax < $input(w) && $ay >= 0 && $ay < $input(h)} {
dict set resonant_antinodes [list $ax $ay] 1
set ax [expr {$ax + $xoff}]
set ay [expr {$ay + $yoff}]
}
}
set ax [expr {$x2 - $xoff}]
set ay [expr {$y2 - $yoff}]
if {$ax >= 0 && $ax < $input(w) && $ay >= 0 && $ay < $input(h)} {
dict set antinodes [list $ax $ay] 1
while {$ax >= 0 && $ax < $input(w) && $ay >= 0 && $ay < $input(h)} {
dict set resonant_antinodes [list $ax $ay] 1
set ax [expr {$ax - $xoff}]
set ay [expr {$ay - $yoff}]
}
}
}
}
dict set antennae $cell [list $x $y] 1
}
}
}
puts "Antinodes: [llength [dict keys $antinodes]]"
puts ""
puts {Part 2: Resonant antinode count in map}
puts "Resonant Antinodes: [llength [dict keys $resonant_antinodes]]"
#foreach ra [dict keys $resonant_antinodes] {
# lassign $ra x y
# lset input(debug) $y $x "#"
#}
#foreach line $input(debug) {
# puts [join $line ""]
#}
|