Skip to content

Commit 0917b1d

Browse files
committed
2017.13: calculate scanner positions without moving.
1 parent 0a08663 commit 0917b1d

File tree

1 file changed

+18
-59
lines changed

1 file changed

+18
-59
lines changed

13_move_through_scanners.go

+18-59
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package main
22

33
import (
44
"fmt"
5-
"metalim/advent/2017/lib/debug"
65
"metalim/advent/2017/lib/source"
7-
"time"
86

97
. "github.com/logrusorgru/aurora"
108
)
@@ -13,25 +11,8 @@ var test1 = `0: 3
1311
1: 2
1412
4: 4
1513
6: 4`
16-
var test2 = ``
17-
18-
type layer struct {
19-
x, y, d, l int
20-
}
21-
22-
func firewall(ssn [][]int) (map[int]*layer, int) {
23-
fw := map[int]*layer{}
24-
var max int
25-
for _, l := range ssn {
26-
fw[l[0]] = &layer{x: l[0], l: l[1], d: 1}
27-
max = l[0]
28-
}
29-
return fw, max
30-
}
3114

3215
func main() {
33-
// source.Dry()
34-
3516
var ins source.Inputs
3617

3718
ins = ins.Test(1, test1, `24`)
@@ -43,61 +24,39 @@ func main() {
4324
ssn := p.Lines().Ints()
4425
fmt.Println(len(ssn), Black(ssn[:3]).Bold())
4526

27+
type layer struct {
28+
x, l int
29+
}
30+
fw := map[int]layer{}
31+
for _, l := range ssn {
32+
fw[l[0]] = layer{x: l[0], l: l[1]}
33+
}
34+
4635
if p.Part(1) {
4736
var sev int
48-
fw, max := firewall(ssn)
4937

50-
for x := 0; x <= max; x++ {
51-
if s, ok := fw[x]; ok {
52-
if s.y == 0 { // cought?
53-
sev += s.x * s.l
54-
}
55-
}
56-
for _, s := range fw { // move scanners
57-
if s.y+s.d < 0 || s.y+s.d >= s.l {
58-
s.d = -s.d
59-
}
60-
s.y += s.d
38+
// instead of moving scanners, calculate their position when packet is passing.
39+
for _, s := range fw {
40+
if s.x%(2*s.l-2) == 0 {
41+
sev += s.x * s.l
6142
}
6243
}
6344
p.SubmitInt(1, sev)
6445
}
6546

6647
if p.Part(2) {
67-
fw, max := firewall(ssn)
6848
var x0 int
6949

70-
NEXT:
71-
for x0 = 0; ; x0++ {
72-
debug.LogT(time.Second, Black(x0).Bold())
50+
DELAY:
51+
for x0 = 0; ; x0++ { // try delays, until solution is found.
7352

74-
// pre-move scanners.
53+
// instead of moving scanners, calculate their position when packet is passing.
7554
for _, s := range fw {
76-
mod := (s.l - 1) * 2
77-
s.y = x0 % mod
78-
s.d = 1
79-
if s.y >= s.l {
80-
s.y = mod - s.y
81-
s.d = -1
82-
}
83-
}
84-
85-
for x := 0; x <= max; x++ {
86-
if x >= 0 {
87-
if s, ok := fw[x]; ok {
88-
if s.y == 0 { // cought?
89-
continue NEXT
90-
}
91-
}
92-
}
93-
for _, s := range fw { // move scanners
94-
if s.y+s.d < 0 || s.y+s.d >= s.l {
95-
s.d = -s.d
96-
}
97-
s.y += s.d
55+
if (x0+s.x)%(2*s.l-2) == 0 {
56+
continue DELAY
9857
}
9958
}
100-
break // found it.
59+
break // found it
10160
}
10261
p.SubmitInt(2, x0)
10362
}

0 commit comments

Comments
 (0)