Browse Source

day 8 part 1

Mistivia 3 months ago
parent
commit
18c7e8c49e
2 changed files with 121 additions and 0 deletions
  1. 71 0
      08/1.tcl
  2. 50 0
      08/input

+ 71 - 0
08/1.tcl

@@ -0,0 +1,71 @@
+set fp [open "input"]
+set lines {}
+while {[gets $fp line] >= 0} {
+    lappend lines $line
+}
+
+close $fp
+
+set height [llength $lines]
+set width [string length [lindex $lines 0]]
+
+proc char_at {x y} {
+    global lines
+    return [string index [lindex $lines $y] $x]
+}
+
+proc in_range {x y} {
+    global height width
+    if {$x < 0 || $x >= $width} {
+        return 0
+    }
+    if {$y < 0 || $y >= $height} {
+        return 0
+    }
+    return 1
+}
+
+proc build_antenna_map {} {
+    global height width
+    set antenna_map {}
+    for {set x 0} {$x < $width} {incr x} {
+        for {set y 0} {$y < $height} {incr y} {
+            set c [char_at $x $y]
+            if {$c == "."} {
+                continue
+            }
+            dict update antenna_map $c site_list {
+                lappend site_list [list $x $y]
+            }
+        }
+    }
+    return $antenna_map
+}
+
+set antenna_map [build_antenna_map]
+set antinodes {}
+
+foreach k [dict keys $antenna_map] {
+    set site_list [dict get $antenna_map $k]
+    set len [llength $site_list]
+    for {set i 0} {$i < $len} {incr i} {
+        for {set j [expr {$i + 1}]} {$j < $len} {incr j} {
+            set x1 [lindex [lindex $site_list $i] 0]
+            set y1 [lindex [lindex $site_list $i] 1]
+            set x2 [lindex [lindex $site_list $j] 0]
+            set y2 [lindex [lindex $site_list $j] 1]
+            set x3 [expr {$x1 - ($x2 - $x1)}]
+            set y3 [expr {$y1 - ($y2 - $y1)}]
+            set x4 [expr {$x2 + ($x2 - $x1)}]
+            set y4 [expr {$y2 + ($y2 - $y1)}]
+            if {[in_range $x3 $y3]} {
+                dict set antinodes [list $x3 $y3] {}
+            }
+            if {[in_range $x4 $y4]} {
+                dict set antinodes [list $x4 $y4] {}
+            }
+        }
+    }
+}
+
+puts [dict size $antinodes]

+ 50 - 0
08/input

@@ -0,0 +1,50 @@
+...............e...........j6.....................
+.....1...............................t.....i......
+.....4.......3..............x..tL......m..........
+.......L.....................Dxj..................
+4....X..................F.....................m...
+.............4.......x....F........k..............
+......3...................t..........i.........Z..
+....L..................y.....F..e.....Z...........
+X.............1........C..........i...D...........
+........4.....................D.....k.X...m.......
+...1...............D........e......6..............
+...3.Y...................................m8.......
+..OL.........................x....Z....g..........
+....3......5.........................6j...........
+...................J..5r.F..k...y.................
+.......................................Z..a.......
+...........................5........j.........a.u.
+...p..............Y....X..........................
+...O.........................kd...................
+........................t.................i.......
+..................J..............u...........z....
+.O.....9.............J..............p..u..........
+.....9............................................
+l...6.....1........e......I................a......
+...................................az.............
+........M.......J...................gI....z.......
+.......Y...l...........p......g....d.......W......
+........5l....9................d.....g............
+.A....9.l.Y............I..............B.......s...
+..................................K.....B.........
+....M.............7.......8..........h.....K......
+.......0f...oc..............G...d7.......z...s..yW
+...M........0...........Gf.....................T..
+................r......G..................w....h..
+...........cP................G.8.R..............T.
+.................A.............N............u..B..
+..H.c..b............................K...CB.....y..
+......c...bP...2............7..K..................
+......b.o....0.......P.............s........h.R...
+......2........f..S........8.....................R
+U....2..............p..............7..............
+.HE..b......A.............N..............w....C...
+................................N.............w...
+.........E...........M................W.......T...
+......E...rS2...........W....................N....
+.....SP..n.....r..0...............................
+.....H..............A............................w
+..........n..U....................s...............
+..n.So.....U................f.....................
+Ho................................................