Skip to content

Commit 39eb5a6

Browse files
authored
Merge branch 'mouredev:main' into main
2 parents d4e08ce + 6fd66a4 commit 39eb5a6

File tree

5 files changed

+1262
-172
lines changed

5 files changed

+1262
-172
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, 8, 1, 0, 1, 3,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 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+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# pylint: disable=missing-module-docstring,missing-function-docstring
2+
3+
from datetime import datetime
4+
from typing import Literal, Tuple, TypedDict, List
5+
6+
7+
# ---------------------------------------------------------------------------- #
8+
# FIRST CHALLENGE #
9+
# ---------------------------------------------------------------------------- #
10+
11+
12+
def get_batman_day_anniversary(*, anniversary: int) -> datetime:
13+
anniversary_date: datetime = datetime(year=1939, month=9, day=16)
14+
15+
if anniversary > 0:
16+
anniversary_date = datetime(
17+
year=anniversary_date.year + anniversary,
18+
month=anniversary_date.month,
19+
day=1,
20+
)
21+
22+
i: int = 1
23+
saturday_counter: int = 1 if anniversary_date.weekday() == 5 else 0
24+
25+
while saturday_counter < 3:
26+
anniversary_date = datetime(
27+
year=anniversary_date.year, month=anniversary_date.month, day=i
28+
)
29+
30+
if anniversary_date.weekday() == 5:
31+
saturday_counter += 1
32+
33+
i += 1
34+
35+
return anniversary_date
36+
37+
38+
batman_day_85th_anniversary: datetime = get_batman_day_anniversary(anniversary=85)
39+
batman_day_100th_anniversary: datetime = get_batman_day_anniversary(anniversary=100)
40+
41+
print("> First challenge...")
42+
43+
print(
44+
f"\n> The 85th anniversary of Batman day is on "
45+
f"{batman_day_85th_anniversary.month}/"
46+
f"{batman_day_85th_anniversary.day}/"
47+
f"{batman_day_85th_anniversary.year}."
48+
)
49+
50+
print(
51+
f"\n> The 100th anniversary of Batman day is on "
52+
f"{batman_day_100th_anniversary.month}/"
53+
f"{batman_day_100th_anniversary.day}/"
54+
f"{batman_day_100th_anniversary.year}."
55+
)
56+
57+
58+
# ---------------------------------------------------------------------------- #
59+
# SECOND CHALLENGE #
60+
# ---------------------------------------------------------------------------- #
61+
62+
63+
# ----------------------------------- Types ---------------------------------- #
64+
65+
66+
type ThreatLevel = Literal[
67+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
68+
]
69+
70+
type RowSensors = Tuple[
71+
ThreatLevel,
72+
ThreatLevel,
73+
ThreatLevel,
74+
ThreatLevel,
75+
ThreatLevel,
76+
ThreatLevel,
77+
ThreatLevel,
78+
ThreatLevel,
79+
ThreatLevel,
80+
ThreatLevel,
81+
ThreatLevel,
82+
ThreatLevel,
83+
ThreatLevel,
84+
ThreatLevel,
85+
ThreatLevel,
86+
ThreatLevel,
87+
ThreatLevel,
88+
ThreatLevel,
89+
ThreatLevel,
90+
ThreatLevel,
91+
]
92+
93+
type Sensors = Tuple[
94+
RowSensors,
95+
RowSensors,
96+
RowSensors,
97+
RowSensors,
98+
RowSensors,
99+
RowSensors,
100+
RowSensors,
101+
RowSensors,
102+
RowSensors,
103+
RowSensors,
104+
RowSensors,
105+
RowSensors,
106+
RowSensors,
107+
RowSensors,
108+
RowSensors,
109+
RowSensors,
110+
RowSensors,
111+
RowSensors,
112+
RowSensors,
113+
RowSensors,
114+
]
115+
116+
ThreatLevelPos = TypedDict("ThreatLevelPos", {"x": int, "y": int, "threat_level": int})
117+
118+
119+
# ----------------------------------- Main ----------------------------------- #
120+
121+
122+
def get_threat_levels(*, _sensors: Sensors) -> List[ThreatLevelPos]:
123+
_threat_levels: List[ThreatLevelPos] = []
124+
125+
for i in range(1, len(_sensors) - 1):
126+
for j in range(1, len(_sensors[i]) - 1):
127+
_threat_level: int = 0
128+
129+
_threat_level += _sensors[i - 1][j - 1]
130+
_threat_level += _sensors[i - 1][j]
131+
_threat_level += _sensors[i - 1][j + 1]
132+
133+
_threat_level += _sensors[i][j - 1]
134+
_threat_level += _sensors[i][j]
135+
_threat_level += _sensors[i][j + 1]
136+
137+
_threat_level += _sensors[i + 1][j - 1]
138+
_threat_level += _sensors[i + 1][j]
139+
_threat_level += _sensors[i + 1][j + 1]
140+
141+
_threat_levels.append({"x": j, "threat_level": _threat_level, "y": i})
142+
143+
return _threat_levels
144+
145+
146+
batman_cave_pos: Tuple[int, int] = (0, 0)
147+
148+
sensors: Sensors = (
149+
(1, 0, 1, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1),
150+
(2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0),
151+
(1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1),
152+
(0, 9, 8, 0, 2, 0, 9, 2, 8, 1, 0, 1, 3, 7, 8, 1, 0, 2, 7, 6),
153+
(1, 0, 1, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1),
154+
(3, 1, 8, 7, 9, 6, 1, 7, 9, 0, 1, 0, 1, 9, 2, 1, 3, 0, 8, 0),
155+
(1, 6, 9, 1, 2, 8, 0, 2, 1, 3, 2, 8, 7, 2, 8, 0, 1, 7, 9, 0),
156+
(1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 1, 0),
157+
(0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0),
158+
(2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2),
159+
(7, 8, 6, 1, 0, 9, 2, 8, 1, 3, 7, 1, 0, 2, 0, 9, 1, 6, 7, 8),
160+
(0, 1, 7, 9, 2, 8, 1, 6, 7, 8, 1, 2, 0, 3, 1, 8, 2, 7, 0, 1),
161+
(3, 0, 9, 1, 8, 6, 1, 0, 7, 9, 2, 0, 1, 2, 8, 0, 1, 9, 6, 1),
162+
(1, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0),
163+
(0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0),
164+
(2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2),
165+
(9, 6, 1, 0, 9, 8, 7, 2, 8, 0, 1, 0, 3, 6, 2, 7, 9, 2, 8, 1),
166+
(0, 7, 8, 9, 6, 2, 1, 9, 6, 7, 8, 0, 9, 7, 6, 8, 1, 0, 2, 8),
167+
(2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2),
168+
(3, 2, 1, 9, 6, 7, 0, 3, 1, 8, 2, 7, 9, 2, 0, 9, 7, 6, 8, 0),
169+
)
170+
171+
threat_levels: List[ThreatLevelPos] = get_threat_levels(_sensors=sensors)
172+
173+
threat_levels_greater_than_twenty = list(
174+
filter(lambda threat_level: threat_level["threat_level"] >= 20, threat_levels)
175+
)
176+
177+
print("\n> Second challenge...")
178+
179+
print("\n> Threat levels greater than 20 (security protocol activated)...")
180+
181+
for threat_level in threat_levels_greater_than_twenty:
182+
print(f"\n> Coordinates (x, y): ({threat_level['x']}, {threat_level['y']}).")
183+
print(f"> Threat level: {threat_level['threat_level']}.")
184+
185+
print("\n> Position with the maximum threat level...")
186+
187+
max_threat_level: ThreatLevelPos = max(
188+
iter(threat_levels_greater_than_twenty),
189+
key=lambda threat_level: threat_level["threat_level"],
190+
)
191+
192+
distance_to_batman_cave: int = abs(max_threat_level["x"] - batman_cave_pos[0]) + abs(
193+
max_threat_level["y"] - batman_cave_pos[1]
194+
)
195+
196+
print(f"\n> Coordinates (x, y): ({max_threat_level['x']}, {max_threat_level['y']}).")
197+
print(f"> Threat level: {max_threat_level['threat_level']}.")
198+
print(f"> Distance to batman cave: {distance_to_batman_cave} cells.")

0 commit comments

Comments
 (0)