-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
114 lines (98 loc) · 1.93 KB
/
main.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"bufio"
lib "github.com/teivah/advent-of-code"
"io"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
m := make(map[int]int)
max := 0
for scanner.Scan() {
s := scanner.Text()
del := lib.NewDelimiter(s, " ")
k := lib.StringToInt(s[:del.Ind[0]-1])
v := del.GetInt(1)
m[k] = v
max = lib.Max(max, k)
}
layers := make([]int, max+1)
for i := 0; i <= max; i++ {
if v, exists := m[i]; exists {
layers[i] = v
}
}
severity := 0
for round := 0; round <= max; round++ {
if layers[round] != 0 {
if getScannerPosition(layers[round], round) == 0 {
severity += round * layers[round]
}
}
}
return severity
}
func getScannerPosition(layers int, round int) int {
delta := round % (layers - 1)
if round%((layers-1)*2) < layers-1 {
return delta
}
return layers - 1 - delta
}
type Scanner struct {
row int
up bool
}
func updateScanners(scanners []Scanner, layers []int) {
for i := 0; i < len(scanners); i++ {
if layers[i] == 0 || layers[i] == 1 {
continue
}
scanner := scanners[i]
if scanner.up {
if scanner.row == 1 {
scanners[i].up = false
}
scanners[i].row--
} else {
if scanner.row+2 == layers[i] {
scanners[i].up = true
}
scanners[i].row++
}
}
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
m := make(map[int]int)
max := 0
for scanner.Scan() {
s := scanner.Text()
del := lib.NewDelimiter(s, " ")
k := lib.StringToInt(s[:del.Ind[0]-1])
v := del.GetInt(1)
m[k] = v
max = lib.Max(max, k)
}
layers := make([]int, max+1)
for i := 0; i <= max; i++ {
if v, exists := m[i]; exists {
layers[i] = v
}
}
for delay := 0; ; delay++ {
caught := false
for layer := 0; layer <= max; layer++ {
picosecond := delay + layer
if layers[layer] != 0 {
if getScannerPosition(layers[layer], picosecond) == 0 {
caught = true
break
}
}
}
if !caught {
return delay
}
}
}