Usate il linguaggio che vi pare, c’è una leaderboard mondiale virtualmente impossibile da scalfire perché solo i primi 100 che risolvono un quiz prendono punti, ma il quiz si sblocca tipo alle 4 del mattino , e purtroppo non tiene traccia di quanto tempo ognuno ci mette a risolvere un quiz, ma solo quanto tempo ci si mette da quando è uscito (quindi se risolvete oggi quello del primo giorno, ci avete messo 4 giorni )
Non l’ho mai fatto ma ne ho sempre sentito parlare molto bene, anche di recente, con persone che dicono che alla fine non e’ roba totalmente astratta ma potrebbero benissimo chiederteli in una interview.
a me la seconda parte del primo livello ha fatto sclerare perché avevo deciso di risolverla la prima parte con delle RegEx, che però in questa fase diventavano un tripudio di dolore.
Alla fine ho dovuto riscrivere tutto da capo con un approccio diverso
Io sto usando python perché è un po’ che non lo toccavo e volevo rinfrescarlo
per la prima parte del day 1 non ho più il codice perché l’ho riscritto, comunque avevo creato una stringa regex r’((\d).+(\d)|(\d))’ che facevo girare per ogni linea di testo, aggiungevo i numeri ecc ecc.
Per la seconda parte inizialmente avevo provato a sostituire tutte le stringhe one, two, three… nel valore numerico, ma il problema è che il giochino si rompeva con stringhe come eighthree, per cui mi trovavo con l’ultima cifra che poteva essere sbagliata.
Alla fine l’ho riscritta così:
import re
file = open('input.txt')
whole_strings = file.read()
strings = whole_strings.splitlines()
numbers_dict = {
'one': '1', 'two': '2', 'three': '3', 'four': '4',
'five': '5', 'six': '6', 'seven': '7', 'eight': '8',
'nine': '9'
}
numbers = [1,2,3,4,5,6,7,8,9]
string = "1ciaone"
total_sum = 0
for string in strings:
first_index = 100
last_index = -1
first_digit = 0
last_digit = 0
for number in numbers:
found_index = string.find(str(number))
if found_index != -1 and found_index < first_index:
first_index = found_index
first_digit = number
found_last_index = string.rfind(str(number))
if found_last_index > last_index:
last_index = found_last_index
last_digit = number
for (key, value) in numbers_dict.items():
found_index = string.find(str(key))
if found_index != -1 and found_index < first_index:
first_index = found_index
first_digit = int(value)
found_last_index = string.rfind(str(key))
if found_last_index > last_index:
last_index = found_last_index
last_digit = int(value)
total_sum += (first_digit * 10) + last_digit
print(f'string: {string}\n Numbers: {first_digit} {last_digit}')
print(total_sum)
EDIT: comunque finito pure il day 4 yeeeh, a domani
cheppalle ma non mi torna la seconda parte del day1
private int GetNumbers(string line)
{
if (string.IsNullOrEmpty(line)) return 0;
var valuepos = new List<(int n, int pos)>();
// Numbers è un Dictionary<string,int> fatto {"one",1}, {"two", 2} ecc...
foreach (var n in Numbers.Keys)
{
var i = line.IndexOf(n);
if (i >= 0)
valuepos.Add((Numbers[n], i));
}
for (var x = 0; x < 10; x++)
{
var i = line.IndexOf(x.ToString());
if (i >= 0)
valuepos.Add((x, i));
}
var sorted = valuepos.OrderBy(x => x.pos).ToList();
var first = sorted.First().n;
var last = sorted.Last().n;
return Convert.ToInt32($"{first}{last}");
}
la mia funzione è giusta, è la soluzione che è sbagliata