Calendrier de l'avent 🎄 (mais sans chocolats)

For Each Cell1 In Range(« D4:D200 Â»)
If (Cell1.Value) > 2020 Then
Debug.Print (Cell1.Value + Cell2.Value + Cell3.Value)
Debug.Print (« Exit1 Â»)
Exit For
End If
For Each Cell2 In Range(« D4:D200 Â»)
If (Cell1.Value + Cell2.Value) > 2020 Then
'Debug.Print (Cell1.Value + Cell2.Value + Cell3.Value)
'Debug.Print (« Exit2 Â»)
Exit For
End If
For Each Cell3 In Range(« D4:D200 Â»)
If (Cell1.Value + Cell2.Value + Cell3.Value) > 2020 Then
'Debug.Print (Cell1.Value + Cell2.Value + Cell3.Value)
'Debug.Print (« Exit3 Â»)
Exit For
End If
Debug.Print (Cell1.Value + Cell2.Value + Cell3.Value)
If (Cell1.Value + Cell2.Value + Cell3.Value) = 2020 Then
Debug.Print (Cell1.Value * Cell2.Value * Cell3.Value)
Debug.Print (« OK Â»)
End If

    Next

Next

Next

Le tri ? Jamais entendu parlé ! :ninja:

Bah ici, ça fait passer de 500ms à 20ms :sweat_smile:

1 « J'aime »

Ah ben j’avais la flemme de faire un truc au propre, j’aurais fait comme toi mais j’aurais commencĂ© ma 2eme boucle a $i non?

for ($j=$i;$j<count($t); $j++)

j’ai pas triĂ© mais j’ai le rĂ©sultat en 0.5ms

temps : 0.0421 sans tri (mais attention sandbox sur le web)
temps : 0.0003 avec tri

Tu as raison, j’ai codĂ© porc, je risque d’introduire un biais avec une donnĂ©e qui serait en double ou triple alors que non.

Ca m’avait paru un poil plus efficace en y rĂ©flĂ©chissant, c’est tout :wink: . Et comme tu exit() a la premiĂšre occurrence, bah tu devrais pas avoir de problĂšme avec une donnĂ©e en double :slight_smile:

Si. ProblÚme si la premiÚre donnée résoud le test en la prenant 2 foi, donc 1010.
1010*1010 ne donnerait pas la solution attendue au test dont la condition de sortie est la somme==2020.

ah en effet, donc tu es obligé de faire $j=$i+1 :wink:

et $k=$j+1 pour la deuxiĂšme partie.
On pourrait implémenter une fonction dynamique pour gérer autant de paramÚtres que nécessaire.

ha bin je viens de le commencer :slight_smile:

Ma solution en Python
f = open("Z:\donnees\developpement\Python\AdventOfCode\Day1.txt", "r")

listOfInt = []
for line in f:
    listOfInt.append(int(line))
f.close()

for i in range(0, len(listOfInt)-1):
    for j in range(i+1, len(listOfInt)):
        if (listOfInt[i]+listOfInt[j]) == 2020:
            print("Trouvé : %d, %d - %d"%(listOfInt[i],listOfInt[j],listOfInt[i]*listOfInt[j]))
            break

for i in range(0, len(listOfInt)-2):
    for j in range(i+1, len(listOfInt)-1):
        for k in range(j+1, len(listOfInt)):
            if (listOfInt[i]+listOfInt[j]+listOfInt[k]) == 2020:
                print("Trouvé : %d, %d, %d - %d"%(listOfInt[i],listOfInt[j], listOfInt[k],listOfInt[i]*listOfInt[j]*listOfInt[k]))
                break

Et le resultat :

Trouvé : 1359, 661 - 898299
Trouvé : 354, 1369, 297 - 143933922
[Finished in 0.244s]
1 « J'aime »

Ça manque de tri :face_with_monocle:

Bon oubliez ce code VB horrible, j’avais que excel sous la main. J’ai tentĂ© le python et ça marche. Par contre, truc relativement Ă©tonnant, aprĂšs tri, le temps d’exĂ©cution est plus grand si on rajoute les condition « i+j > 2020 Â» pour arrĂȘter la boucle. J’imagine que le dataset doit pas permettre de profiter de ces conditions (ou que ça optimise plus Ă  l’exĂ©cution avec les 3 boucles uniquement).

Hello, aujourd’hui c’est :

Pas encore eu le temps mais je m’y mets tout à l’heure :slight_smile:

C’est encore plutĂŽt simple, surtout en Python. La plus grande difficultĂ© provient Ă  mon avis du traitement de l’input que de l’algo derriĂšre.

Part 1
    with open("input.txt", 'r') as f:
        lines = [x.strip() for x in f.readlines() if x.strip()]
        valid = 0
        for line in lines:
            split_line = line.split(" ")
            password = split_line[2]
            letter = split_line[1][0]  # split_line 1 = a:, we only want a
            r = [int(x) for x in split_line[0].split("-")]
            letter_count = password.count(letter)
            if r[0] <= letter_count <= r[1]:
                valid += 1
        print(valid, "valid passwords")
Part 2
    with open("input.txt", 'r') as f:
        lines = [x.strip() for x in f.readlines() if x.strip()]
        valid = 0
        for line in lines:
            split_line = line.split(" ")
            password = split_line[2]
            letter = split_line[1][0]  # split_line 1 = a:, we only want a
            pos = [int(x) - 1 for x in split_line[0].split("-")]
            if (password[pos[0]] == letter and password[pos[1]] != letter) or \
                    (password[pos[0]] != letter and password[pos[1]] == letter):
                valid += 1
        print(valid, "valid passwords")

2 « J'aime »

oui ca va, pas trop violent encore :slight_smile:

Part1

advent/part1.ex at master · cblavier/advent · GitHub

Part2

advent/part2.ex at master · cblavier/advent · GitHub

Au passage je suis curieux de savoir : le code que j’ai Ă©crit est-il intelligible par quelqu’un qui n’a jamais vu ou codĂ© d’Elixir auparavant ?

Ça va. Pour l’instant c’est comprĂ©hensible :slight_smile: (sauf le /1 Ă  la fin de certaines lignes)

1 « J'aime »

le /1 c’est l’aritĂ©
&ma_fonction/2 c’est pour dĂ©signer la fonction qui s’appelle ma_fonction et qui prend 2 paramĂštres

1 « J'aime »

Day2 à la crados, je teste groovy par curiosité

La soluciĂłn
 File f = new File("../../resources/day2Input.txt")
     def lines = f.readLines()
 
     void phase1(){
         Integer numberOfValidPasswords = 0
         Integer lowBound = 0
         Integer highBound = 0
         String password
         String mandatoryLetter
 
         lines.forEach(){line ->
             lowBound = Integer.parseInt(line.split(':')[0].split(' ')[0].split('-')[0])
             highBound = Integer.parseInt(line.split(':')[0].split(' ')[0].split('-')[1])
             mandatoryLetter = line.split(':')[0].split(' ')[1]
             password = line.split(':')[1]
 
             if(password.count(mandatoryLetter) >= lowBound && password.count(mandatoryLetter) <= highBound){
                 numberOfValidPasswords++
             }
         }
         println  "Phase 1: Answer is " + numberOfValidPasswords
     }
 
     void phase2(){
         Integer numberOfValidPasswords = 0
         Integer firstPosition = 0
         Integer secondPosition = 0
         String password
         String mandatoryLetter
 
         lines.forEach(){line ->
             firstPosition = Integer.parseInt(line.split(':')[0].split(' ')[0].split('-')[0])
             secondPosition = Integer.parseInt(line.split(':')[0].split(' ')[0].split('-')[1])
             mandatoryLetter = line.split(':')[0].split(' ')[1]
             password = line.split(':')[1]
 
             if(password.getAt(firstPosition).concat(password.getAt(secondPosition)).count(mandatoryLetter) == 1){
                 numberOfValidPasswords++
             }
         }
         println  "Phase 2: Answer is " + numberOfValidPasswords
     }
1 « J'aime »