From a33ac3bbbc8f4bfc557ccbc2af642678e3a0b401 Mon Sep 17 00:00:00 2001 From: rick <rick@gnous.eu> Date: Wed, 24 Mar 2021 17:41:37 +0100 Subject: [PATCH 1/5] =?UTF-8?q?Ajout=20premi=C3=A8re=20partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jour11/jour11.lua | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 jour11/jour11.lua diff --git a/jour11/jour11.lua b/jour11/jour11.lua new file mode 100644 index 0000000..c00364f --- /dev/null +++ b/jour11/jour11.lua @@ -0,0 +1,91 @@ +local function suivant(mot) + local len = mot:len() + local ret = mot:sub(1, len - 1) + if mot:sub(len, len) == 'z' then + ret = suivant(ret) .. 'a' + else + ret = ret .. string.char(string.byte(mot:sub(len, len)) + 1) + end + return ret +end + +local function suite(mot) + local ret = false + local pred = nil + local i = 1 + local c = nil + while not (ret and pred == nil) and i <= mot:len() do + c = mot:sub(i, i) + if pred == nil then + pred = c + elseif pred ~= nil then + if string.byte(pred) == (string.byte(c) - 1) then + if ret then + pred = nil + else + pred = c + ret = true + end + else + ret = false + pred = c + end + end + i = i + 1 + end + if ret and pred == nil then + return true + else + return false + end +end + +local function double(mot) + local ret = 0 + local pred = nil + for c in mot:gmatch(".") do + if pred ~= nil and pred == c then + ret = ret + 1 + pred = nil + else + pred = c + end + end + return ret +end + +local function badChar(mot, char) + local ret = mot + local tmp = mot:find(char) + + if tmp ~= nil then + if tmp == 1 then + ret = string.char(string.byte(ret:sub(tmp, tmp)) + 1) .. string.rep('a', ret:len() - tmp) + elseif tmp == ret:len() then + ret = ret:sub(1, tmp-1) .. string.char(string.byte(ret:sub(tmp, tmp)) + 1) + else + ret = ret:sub(1, tmp - 1) .. string.char(string.byte(ret:sub(tmp, tmp)) + 1) .. string.rep('a', ret:len() - tmp) + end + end + return ret +end + +myInput = "cqjxjnds" +fin = false + +while not fin do + myInput = badChar(myInput, 'i') + myInput = badChar(myInput, 'l') + myInput = badChar(myInput, 'o') + if suite(myInput) and double(myInput) >= 2 then + print(myInput) + fin = true + else + myInput = suivant(myInput) + end +end + + +-- une suite de 3 caractères au moins (abc par ex) +-- ne doit pas contenir i l o +-- doit avoir 2 doubles (aa et cc par ex) From 4da02daa03dacca39a645900220a30cd98a7be1a Mon Sep 17 00:00:00 2001 From: rick <rick@gnous.eu> Date: Wed, 24 Mar 2021 17:44:11 +0100 Subject: [PATCH 2/5] Ajout jour11 --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a7d9ae..f48e082 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ mono jour10.exe | 8 | | | | | | | 9 | | | | | | | 10 |Visual Basic | vbnc | vbnc jour10.vbs | mono jour10.exe | | -| 11 | | | | | | +| 11 | Lua | | | lua jour11.lua | | | 12 | | | | | | | 13 | | | | | | | 14 | | | | | | @@ -124,6 +124,13 @@ mono jour10.exe ``` ## Jour 11 + +Ce jour utilise le Lua 5.2.4. Il se lance comme en Python avec une commande sur le fichier : + +```bash +lua jour11.lua +``` + ## Jour 12 ## Jour 13 ## Jour 14 From 57b9afa2b5b1108945be0494a4344c356cb10c4e Mon Sep 17 00:00:00 2001 From: rick <rick@gnous.eu> Date: Wed, 24 Mar 2021 18:14:50 +0100 Subject: [PATCH 3/5] =?UTF-8?q?Ajout=20doc=20et=20l=C3=A9g=C3=A8res=20modi?= =?UTF-8?q?fs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jour11/jour11.lua | 51 +++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/jour11/jour11.lua b/jour11/jour11.lua index c00364f..9f8f5cd 100644 --- a/jour11/jour11.lua +++ b/jour11/jour11.lua @@ -1,25 +1,40 @@ +-- Calcule le mot suivant +-- @param mot string : le mot à incrémenter +-- @return string : le mot incrémenté +-- exemple : abcz -> abda local function suivant(mot) local len = mot:len() local ret = mot:sub(1, len - 1) if mot:sub(len, len) == 'z' then - ret = suivant(ret) .. 'a' + -- utilisé si abzz par ex. s’appelle récursivment pour trouver acaa + ret = suivant(ret) .. 'a' else - ret = ret .. string.char(string.byte(mot:sub(len, len)) + 1) + ret = ret .. string.char(mot:sub(len, len):byte() + 1) end return ret end +-- Détecte s’il existe une suite de 3 lettres consécutives +-- @param mot string : mot où il faut vérifier s’il y a une suite +-- @return bool : true s’il y a une suite, false sinon +-- exemple : uabcie -> true; abecud -> false local function suite(mot) local ret = false local pred = nil local i = 1 local c = nil - while not (ret and pred == nil) and i <= mot:len() do + -- lorsqu’on trouve la première lettre, on met ret à true et pred à c + -- si on trouve la troisième lettre, on met pred à nil. + -- si ret est à true et pred à nil, on a trouvé une suite de 3 lettres + while not (ret and pred == nil) and i <= #mot do c = mot:sub(i, i) + -- première itération if pred == nil then pred = c elseif pred ~= nil then - if string.byte(pred) == (string.byte(c) - 1) then + if pred:byte() == (c:byte() - 1) then + -- si le pred est bien prédecesseur de c, on vérifie ret + -- pour modifier ensuite pred en conséquence (nil si fin) if ret then pred = nil else @@ -33,17 +48,18 @@ local function suite(mot) end i = i + 1 end - if ret and pred == nil then - return true - else - return false - end + return (ret and pred == nil) and true or false end +-- Cherche le nombre de double dans un mot +-- @param mot string : mot à vérifier +-- @return int : le nombre de double présent +-- exemple : aaabc -> 1; aacaauxxii -> 4 local function double(mot) local ret = 0 local pred = nil for c in mot:gmatch(".") do + -- pred ~= obligatoire car on ne peut pas comparer un char avec un nil if pred ~= nil and pred == c then ret = ret + 1 pred = nil @@ -54,17 +70,23 @@ local function double(mot) return ret end +-- incrémente le mot selon le mauvais caractère +-- @param mot string : le mot à modifier +-- @param char string : le caractère à trouver et incrémenter +-- @return string : le mot modifié ou non +-- exemple : avec char = i +-- abcidd -> abcjaa; abcde -> abcde local function badChar(mot, char) local ret = mot local tmp = mot:find(char) if tmp ~= nil then if tmp == 1 then - ret = string.char(string.byte(ret:sub(tmp, tmp)) + 1) .. string.rep('a', ret:len() - tmp) + ret = string.char(ret:sub(tmp, tmp):byte() + 1) .. string.rep('a', #ret - tmp) elseif tmp == ret:len() then - ret = ret:sub(1, tmp-1) .. string.char(string.byte(ret:sub(tmp, tmp)) + 1) + ret = ret:sub(1, tmp-1) .. string.char(ret:sub(tmp, tmp):byte() + 1) else - ret = ret:sub(1, tmp - 1) .. string.char(string.byte(ret:sub(tmp, tmp)) + 1) .. string.rep('a', ret:len() - tmp) + ret = ret:sub(1, tmp - 1) .. string.char(ret:sub(tmp, tmp):byte() + 1) .. string.rep('a', #ret - tmp) end end return ret @@ -84,8 +106,3 @@ while not fin do myInput = suivant(myInput) end end - - --- une suite de 3 caractères au moins (abc par ex) --- ne doit pas contenir i l o --- doit avoir 2 doubles (aa et cc par ex) From 248d35cfd732f8eba78d243e7ec9994509c57fa5 Mon Sep 17 00:00:00 2001 From: rick <rick@gnous.eu> Date: Wed, 24 Mar 2021 18:19:49 +0100 Subject: [PATCH 4/5] Transformation du main en fonction --- jour11/jour11.lua | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/jour11/jour11.lua b/jour11/jour11.lua index 9f8f5cd..0eb5445 100644 --- a/jour11/jour11.lua +++ b/jour11/jour11.lua @@ -92,17 +92,30 @@ local function badChar(mot, char) return ret end -myInput = "cqjxjnds" -fin = false - -while not fin do - myInput = badChar(myInput, 'i') - myInput = badChar(myInput, 'l') - myInput = badChar(myInput, 'o') - if suite(myInput) and double(myInput) >= 2 then - print(myInput) - fin = true - else - myInput = suivant(myInput) +-- Trouve le mot de passe correspondant aux critères suivants : +-- * une suite de 3 lettres consécutives au moins +-- * pas de lettre i, l ou o +-- * 2 pairs de lettres distinctes (aaa ne compte que pour une paire) +-- @param mot string : le mot à vérifier et modifier +-- @return string : le nouveau mot de passe +local function findPass(mot) + local fin = false + local ret = mot + + while not fin do + ret = badChar(ret, 'i') + ret = badChar(ret, 'l') + ret = badChar(ret, 'o') + if suite(ret) and double(ret) >= 2 then + fin = true + else + ret = suivant(ret) + end end + return ret end + +myInput = "cqjxjnds" +print("Traitement première partie…") +myInput = findPass(myInput) +print("Le nouveau mot de passe est : " .. myInput) From 8b0b6e1890b4b19fcd949fa0d4353b74db7296ad Mon Sep 17 00:00:00 2001 From: rick <rick@gnous.eu> Date: Wed, 24 Mar 2021 18:21:22 +0100 Subject: [PATCH 5/5] =?UTF-8?q?Ajout=20deuxi=C3=A8me=20partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jour11/jour11.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/jour11/jour11.lua b/jour11/jour11.lua index 0eb5445..599a77b 100644 --- a/jour11/jour11.lua +++ b/jour11/jour11.lua @@ -119,3 +119,6 @@ myInput = "cqjxjnds" print("Traitement première partie…") myInput = findPass(myInput) print("Le nouveau mot de passe est : " .. myInput) +print("Traitement deuxième partie…") +myInput = findPass(suivant(myInput)) +print("Le nouveau mot de passe est : " .. myInput)