Skip to content

Commit a926f1d

Browse files
author
Miguel Ángel Delgado
committed
add more solutions
1 parent 32f5629 commit a926f1d

File tree

5 files changed

+576
-0
lines changed

5 files changed

+576
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
// Definimos el tamaño de las cuadrículas y el rango de días
3+
const GRID_WIDTH = 4;
4+
const GRID_HEIGHT = 3;
5+
const DAYS = 24;
6+
7+
// Inicializamos los días descubiertos
8+
$discovered = array_fill(1, DAYS, false);
9+
10+
function drawCalendar($discovered) {
11+
for ($row = 0; $row < ceil(DAYS / 6); $row++) {
12+
// Líneas superiores de las cuadrículas
13+
for ($line = 0; $line < GRID_HEIGHT; $line++) {
14+
for ($col = 0; $col < 6; $col++) {
15+
$day = $row * 6 + $col + 1;
16+
if ($day > DAYS) break;
17+
18+
switch ($line) {
19+
case 0:
20+
case 2:
21+
echo str_repeat("*", GRID_WIDTH) . " ";
22+
break;
23+
case 1:
24+
echo "*";
25+
echo $discovered[$day]
26+
? str_repeat("*", GRID_WIDTH - 2)
27+
: str_pad(sprintf("%02d", $day), GRID_WIDTH - 2, " ", STR_PAD_BOTH);
28+
echo "* ";
29+
break;
30+
}
31+
}
32+
echo "\n";
33+
}
34+
}
35+
}
36+
37+
while (true) {
38+
// Dibujamos el calendario
39+
drawCalendar($discovered);
40+
41+
// Pedimos al usuario que elija un día
42+
echo "\nSeleccione un día (1-" . DAYS . ") para descubrir o escriba 0 para salir: ";
43+
$input = trim(fgets(STDIN));
44+
45+
if ($input == 0) {
46+
echo "¡Gracias por participar en el aDEViento!\n";
47+
break;
48+
}
49+
50+
if (!is_numeric($input) || $input < 1 || $input > DAYS) {
51+
echo "Por favor, elija un número válido entre 1 y " . DAYS . ".\n";
52+
continue;
53+
}
54+
55+
if ($discovered[$input]) {
56+
echo "El día $input ya ha sido descubierto.\n";
57+
} else {
58+
$discovered[$input] = true;
59+
echo "¡Has descubierto el día $input!\n";
60+
}
61+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import java.util.*;
2+
import java.util.stream.Collectors;
3+
4+
public class miguelex {
5+
6+
private static void displayTree(List<String> tree) {
7+
for (String line : tree) {
8+
System.out.println(line);
9+
}
10+
}
11+
12+
private static List<String> createTree(int height, boolean hasStar, Map<Integer, Map<Integer, String>> decorations, boolean lightsOn) {
13+
List<String> tree = new ArrayList<>();
14+
15+
// Estrella opcional
16+
if (hasStar) {
17+
tree.add(" ".repeat(height - 1) + "@");
18+
}
19+
20+
// Ramas
21+
for (int i = 1; i <= height; i++) {
22+
StringBuilder line = new StringBuilder(" ".repeat(height - i));
23+
for (int j = 0; j < 2 * i - 1; j++) {
24+
String deco = decorations.getOrDefault(i, Collections.emptyMap()).get(j);
25+
line.append(deco != null ? deco : "*");
26+
}
27+
tree.add(line.toString());
28+
}
29+
30+
// Tronco
31+
String trunkPadding = " ".repeat(height - 2);
32+
tree.add(trunkPadding + "|||");
33+
tree.add(trunkPadding + "|||");
34+
35+
return tree;
36+
}
37+
38+
private static void addRandomDecoration(Map<Integer, Map<Integer, String>> decorations, int height, String type, int count) {
39+
Random random = new Random();
40+
int added = 0;
41+
42+
while (added < count) {
43+
int row = random.nextInt(height) + 1;
44+
int col = random.nextInt(2 * row - 1);
45+
46+
decorations.putIfAbsent(row, new HashMap<>());
47+
if (!decorations.get(row).containsKey(col)) {
48+
decorations.get(row).put(col, type);
49+
added++;
50+
}
51+
}
52+
}
53+
54+
private static void removeRandomDecoration(Map<Integer, Map<Integer, String>> decorations, String type, int count) {
55+
int removed = 0;
56+
57+
for (Integer row : new ArrayList<>(decorations.keySet())) {
58+
for (Integer col : new ArrayList<>(decorations.get(row).keySet())) {
59+
if (decorations.get(row).get(col).equals(type) && removed < count) {
60+
decorations.get(row).remove(col);
61+
removed++;
62+
}
63+
}
64+
}
65+
}
66+
67+
private static void toggleLights(Map<Integer, Map<Integer, String>> decorations, boolean lightsOn) {
68+
for (Map<Integer, String> row : decorations.values()) {
69+
for (Map.Entry<Integer, String> entry : row.entrySet()) {
70+
if (entry.getValue().equals("+")) {
71+
entry.setValue(lightsOn ? "+" : "*");
72+
}
73+
}
74+
}
75+
}
76+
77+
public static void main(String[] args) {
78+
Scanner scanner = new Scanner(System.in);
79+
System.out.print("Ingrese la altura del árbol: ");
80+
int height = scanner.nextInt();
81+
82+
boolean hasStar = true;
83+
Map<Integer, Map<Integer, String>> decorations = new HashMap<>();
84+
boolean lightsOn = true;
85+
86+
while (true) {
87+
List<String> tree = createTree(height, hasStar, decorations, lightsOn);
88+
displayTree(tree);
89+
90+
System.out.println("\nOpciones:");
91+
System.out.println("1. Añadir/Eliminar estrella");
92+
System.out.println("2. Añadir bolas (o)");
93+
System.out.println("3. Eliminar bolas (o)");
94+
System.out.println("4. Añadir luces (+)");
95+
System.out.println("5. Eliminar luces (+)");
96+
System.out.println("6. Apagar/Encender luces");
97+
System.out.println("7. Salir");
98+
99+
System.out.print("Seleccione una opción: ");
100+
int option = scanner.nextInt();
101+
102+
switch (option) {
103+
case 1:
104+
hasStar = !hasStar;
105+
System.out.println(hasStar ? "Estrella añadida." : "Estrella eliminada.");
106+
break;
107+
case 2:
108+
addRandomDecoration(decorations, height, "o", 2);
109+
System.out.println("Bolas añadidas.");
110+
break;
111+
case 3:
112+
removeRandomDecoration(decorations, "o", 2);
113+
System.out.println("Bolas eliminadas.");
114+
break;
115+
case 4:
116+
addRandomDecoration(decorations, height, "+", 3);
117+
System.out.println("Luces añadidas.");
118+
break;
119+
case 5:
120+
removeRandomDecoration(decorations, "+", 3);
121+
System.out.println("Luces eliminadas.");
122+
break;
123+
case 6:
124+
lightsOn = !lightsOn;
125+
toggleLights(decorations, lightsOn);
126+
System.out.println(lightsOn ? "Luces encendidas." : "Luces apagadas.");
127+
break;
128+
case 7:
129+
System.out.println("¡Feliz Navidad!");
130+
scanner.close();
131+
return;
132+
default:
133+
System.out.println("Opción no válida.");
134+
}
135+
}
136+
}
137+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
const readline = require("readline");
2+
3+
const rl = readline.createInterface({
4+
input: process.stdin,
5+
output: process.stdout
6+
});
7+
8+
function createTree(height, hasStar, decorations, lightsOn) {
9+
let tree = [];
10+
11+
// Estrella opcional
12+
if (hasStar) {
13+
tree.push(" ".repeat(height - 1) + "@");
14+
}
15+
16+
// Ramas
17+
for (let i = 1; i <= height; i++) {
18+
let line = " ".repeat(height - i);
19+
for (let j = 0; j < 2 * i - 1; j++) {
20+
if (decorations[i] && decorations[i][j]) {
21+
line += decorations[i][j];
22+
} else {
23+
line += "*";
24+
}
25+
}
26+
tree.push(line);
27+
}
28+
29+
// Tronco
30+
const trunkPadding = " ".repeat(height - 2);
31+
tree.push(trunkPadding + "|||");
32+
tree.push(trunkPadding + "|||");
33+
34+
return tree;
35+
}
36+
37+
function displayTree(tree) {
38+
console.log(tree.join("\n"));
39+
}
40+
41+
function addRandomDecoration(decorations, height, type, count) {
42+
let added = 0;
43+
while (added < count) {
44+
const row = Math.floor(Math.random() * height) + 1;
45+
const col = Math.floor(Math.random() * (2 * row - 1));
46+
47+
if (!decorations[row]) decorations[row] = {};
48+
if (!decorations[row][col]) {
49+
decorations[row][col] = type;
50+
added++;
51+
}
52+
}
53+
}
54+
55+
function removeRandomDecoration(decorations, type, count) {
56+
let removed = 0;
57+
for (const row in decorations) {
58+
for (const col in decorations[row]) {
59+
if (decorations[row][col] === type && removed < count) {
60+
delete decorations[row][col];
61+
removed++;
62+
}
63+
}
64+
}
65+
}
66+
67+
function toggleLights(decorations, lightsOn) {
68+
for (const row in decorations) {
69+
for (const col in decorations[row]) {
70+
if (decorations[row][col] === "+") {
71+
decorations[row][col] = lightsOn ? "+" : "*";
72+
}
73+
}
74+
}
75+
}
76+
77+
function promptUser(query) {
78+
return new Promise(resolve => rl.question(query, resolve));
79+
}
80+
81+
(async function main() {
82+
const height = parseInt(await promptUser("Ingrese la altura del árbol: "), 10);
83+
let hasStar = true;
84+
let decorations = {};
85+
let lightsOn = true;
86+
87+
while (true) {
88+
const tree = createTree(height, hasStar, decorations, lightsOn);
89+
displayTree(tree);
90+
91+
console.log("\nOpciones:");
92+
console.log("1. Añadir/Eliminar estrella");
93+
console.log("2. Añadir bolas (o)");
94+
console.log("3. Eliminar bolas (o)");
95+
console.log("4. Añadir luces (+)");
96+
console.log("5. Eliminar luces (+)");
97+
console.log("6. Apagar/Encender luces");
98+
console.log("7. Salir");
99+
100+
const option = parseInt(await promptUser("Seleccione una opción: "), 10);
101+
102+
switch (option) {
103+
case 1:
104+
hasStar = !hasStar;
105+
console.log(hasStar ? "Estrella añadida." : "Estrella eliminada.");
106+
break;
107+
case 2:
108+
addRandomDecoration(decorations, height, "o", 2);
109+
console.log("Bolas añadidas.");
110+
break;
111+
case 3:
112+
removeRandomDecoration(decorations, "o", 2);
113+
console.log("Bolas eliminadas.");
114+
break;
115+
case 4:
116+
addRandomDecoration(decorations, height, "+", 3);
117+
console.log("Luces añadidas.");
118+
break;
119+
case 5:
120+
removeRandomDecoration(decorations, "+", 3);
121+
console.log("Luces eliminadas.");
122+
break;
123+
case 6:
124+
lightsOn = !lightsOn;
125+
toggleLights(decorations, lightsOn);
126+
console.log(lightsOn ? "Luces encendidas." : "Luces apagadas.");
127+
break;
128+
case 7:
129+
console.log("¡Feliz Navidad!");
130+
rl.close();
131+
return;
132+
default:
133+
console.log("Opción no válida.");
134+
}
135+
}
136+
})();

0 commit comments

Comments
 (0)