Skip to content

Commit b514995

Browse files
authored
Merge pull request mouredev#6430 from hozlucas28/Solution-39-Go
mouredev#39 - Go
2 parents b1d0b5e + ec2f19c commit b514995

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"slices"
7+
"time"
8+
)
9+
10+
/* -------------------------------------------------------------------------- */
11+
/* FUNCTIONS */
12+
/* -------------------------------------------------------------------------- */
13+
14+
/* ----------------------------- First Challenge ---------------------------- */
15+
16+
func GetBatmanDayAnniversary(anniversary uint16) *time.Time {
17+
var anniversaryDate time.Time = time.Date(1939, 9, 16, 0, 0, 0, 0, time.Local)
18+
19+
if anniversary > 0 {
20+
anniversaryDate = time.Date(1939+int(anniversary), 9, 1, 0, 0, 0, 0, time.Local)
21+
22+
var saturdayCounter uint8 = 0
23+
if anniversaryDate.Weekday() == time.Saturday {
24+
saturdayCounter = 1
25+
}
26+
27+
for saturdayCounter < 3 {
28+
anniversaryDate = anniversaryDate.AddDate(0, 0, 1)
29+
30+
if anniversaryDate.Weekday() == time.Saturday {
31+
saturdayCounter += 1
32+
}
33+
}
34+
}
35+
36+
return &anniversaryDate
37+
}
38+
39+
/* ---------------------------- Second Challenge ---------------------------- */
40+
41+
type ThreatLevel struct {
42+
X uint
43+
Y uint
44+
Level uint
45+
_ struct{}
46+
}
47+
48+
type Sensors struct {
49+
Array2D [][]uint
50+
_ struct{}
51+
}
52+
53+
func (sensors *Sensors) GetThreatLevels() []*ThreatLevel {
54+
var threatLevels []*ThreatLevel = make([]*ThreatLevel, 0, (len(sensors.Array2D)-2)*(len(sensors.Array2D[0])-2))
55+
56+
var rows uint = uint(len(sensors.Array2D))
57+
var cols uint = uint(len(sensors.Array2D[0]))
58+
59+
for i := uint(1); i < rows-1; i++ {
60+
for j := uint(1); j < cols-1; j++ {
61+
var threatLevel uint = 0
62+
63+
threatLevel += sensors.Array2D[i-1][j-1]
64+
threatLevel += sensors.Array2D[i-1][j]
65+
threatLevel += sensors.Array2D[i-1][j+1]
66+
67+
threatLevel += sensors.Array2D[i][j-1]
68+
threatLevel += sensors.Array2D[i][j]
69+
threatLevel += sensors.Array2D[i][j+1]
70+
71+
threatLevel += sensors.Array2D[i+1][j-1]
72+
threatLevel += sensors.Array2D[i+1][j]
73+
threatLevel += sensors.Array2D[i+1][j+1]
74+
75+
threatLevels = append(threatLevels, &ThreatLevel{X: j, Y: i, Level: threatLevel})
76+
}
77+
}
78+
79+
return threatLevels
80+
}
81+
82+
/* -------------------------------------------------------------------------- */
83+
/* MAIN */
84+
/* -------------------------------------------------------------------------- */
85+
86+
func main() {
87+
fmt.Println("> First challenge...")
88+
89+
var batmanDay85thAnniversary *time.Time = GetBatmanDayAnniversary(85)
90+
var batmanDay100thAnniversary *time.Time = GetBatmanDayAnniversary(100)
91+
92+
fmt.Printf(
93+
"\n> The 85th anniversary of Batman day is on %d/%d/%d.\n",
94+
batmanDay85thAnniversary.Month(),
95+
batmanDay85thAnniversary.Day(),
96+
batmanDay85thAnniversary.Year(),
97+
)
98+
99+
fmt.Printf(
100+
"> The 100th anniversary of Batman day is on %d/%d/%d.\n",
101+
batmanDay100thAnniversary.Month(),
102+
batmanDay100thAnniversary.Day(),
103+
batmanDay100thAnniversary.Year(),
104+
)
105+
106+
fmt.Println("\n> Second challenge...")
107+
108+
var batmanCavePos [2]uint = [2]uint{0, 0}
109+
110+
var sensors Sensors = Sensors{
111+
Array2D: [][]uint{
112+
{1, 0, 1, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1},
113+
{2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0},
114+
{1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1},
115+
{0, 9, 8, 0, 2, 0, 9, 2, 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1, 8, 2, 7, 9, 2, 0, 9, 7, 6, 8, 0},
132+
},
133+
}
134+
135+
var threatLevels []*ThreatLevel = sensors.GetThreatLevels()
136+
137+
var threatLevelsGreaterThanTwenty []*ThreatLevel = slices.DeleteFunc(threatLevels, func(threatLevel *ThreatLevel) bool {
138+
return threatLevel.Level < 20
139+
})
140+
141+
fmt.Println("\n> Threat levels greater than 20 (security protocol activated)...")
142+
143+
for _, threatLevel := range threatLevelsGreaterThanTwenty {
144+
fmt.Printf("\n> Coordinates (x, y): (%d, %d).\n", threatLevel.X, threatLevel.Y)
145+
fmt.Printf("> Threat level: %d.\n", threatLevel.Level)
146+
}
147+
148+
fmt.Println("\n> Position with the maximum threat level...")
149+
150+
var maxThreatLevel *ThreatLevel = slices.MaxFunc(threatLevelsGreaterThanTwenty, func(a *ThreatLevel, b *ThreatLevel) int {
151+
return int(a.Level - b.Level)
152+
})
153+
154+
var distanceToBatmanCave uint = uint(math.Abs(float64(maxThreatLevel.X-batmanCavePos[0])) +
155+
math.Abs(float64(maxThreatLevel.Y-batmanCavePos[1])))
156+
157+
fmt.Printf("\n> Coordinates (x, y): (%d, %d).\n", maxThreatLevel.X, maxThreatLevel.Y)
158+
fmt.Printf("> Threat level: %d.\n", maxThreatLevel.Level)
159+
fmt.Printf("> Distance to batman cave: %d cells.\n", distanceToBatmanCave)
160+
}

0 commit comments

Comments
 (0)