å ¥éã¿ã¹ã¯ãå®äºããŠããªãå Žåã®åç §å ¥éã¬ã€ã
Puzzling Pyramids 㯠Cairo ã®ããŒã¿æ§é ãšã¢ã«ãŽãªãºã ã®ã¿ã¹ã¯ã§ãThink Cairo ã®ç¬¬ 2 ç« ã§ããäž»ã«ãã€ããªããªãŒã®åé¡ã解決ããããã® 2 ã€ã®å°åé¡ããããŸãã
äžé DFS#
å³ã®ããã«ããã€ããªããªãŒãå·Šããå³ã«å€æããŠãªã¹ãã«ããå¿ èŠããããŸããã¢ã«ãŽãªãºã ã«è©³ãã人ãªããããã¯äžé DFSïŒæ·±ãåªå æ¢çŽ¢ïŒã§ããããšããããã§ããããã€ã³ã¿ãŒãããäžã«ã¯å€ãã®è§£èª¬ããããŸãã
å®éã« Cairo ã®ã³ãŒããæžãéã«ã空ã®é åã®å®çŸ©ãã€ã³ããã¯ã¹ã®æ¹æ³ãªã©ã«ã€ããŠçåãçããå Žåã¯ã次ã®ãªã³ã¯ãåèã«ããŠãã ããïŒhttps://book.cairo-lang.org/ch03-01-arrays.html
lower_chambers 㯠Option åã§ãããOption åã®æäœæ¹æ³ãããããªãå Žåã¯ã次ã®ãªã³ã¯ãåèã«ããŠãã ããïŒhttps://book.cairo-lang.org/ch06-02-the-match-control-flow-construct.html?highlight=Option#matching-with-options
ãã®åé¡ã¯ãååž°çãªæ¹æ³ãŸãã¯éååž°çãªæ¹æ³ã§è§£æ±ºããããšãã§ããŸãããã ããCairo ã®çµã¿èŸŒã¿é åé¢æ°ã¯ååã§ã¯ãªãããïŒåã®ã¿ã¹ã¯ã§æãããããããŸããïŒãå®éã®ã³ãŒãã£ã³ã°ã¯ããå°é£ã«ãªããŸãããããã£ãŠãååž°çãªæ¹æ³ã䜿çšããããšããå§ãããŸãã
ãŸããå ¬åŒã®ãã£ããããã®ååž°ããŒãžã§ã³ãèŠãŠã¿ããšãèãæ¹ã¯åãã§ãïŒhttps://github.com/starkware-libs/cairo/blob/main/examples/fib_array.cairo
ååž°é¢æ°ã¯ãåããŒãã§ãå·Šã®èãå€å®ããŠååž°çã«å®è¡ -> ããŒããè¿œå -> å³ã®èãå€å®ããŠååž°çã«å®è¡ããšããããžãã¯ãå®è¡ããå¿ èŠããããŸãã
ããã€ãã®éèŠãªãã€ã³ãïŒ
- ååž°çã«èŠçŽ ããªã¹ãã«è¿œå ããããã«ã¯ããã®ãªã¹ãã« ref ããŒã¯ãŒããè¿œå ããå¿ èŠããããŸããååž°äžã«åãé åãåç §ããããã§ãã
- ååž°é¢æ°ã«ã¯ Cloneãš Dropãå®è£ ããå¿ èŠããããŸããPyramidIntoArray ãšåãããã«æžãå¿ èŠããããŸãããGitHub ã® cairo ã®æ°ããããŒãžã§ã³ã§ã¯å°ãç°ãªãæžãæ¹ãããŠããŸãã
- chambers ãã¹ãããã·ã§ããã«å€æããŠããè€æ°ååç §ããããšãã§ããŸãããã®åŸãå ã®åã«ã¯ããŒã³ããŠãªã¹ãã«è¿œå ããŸãã
- ãã¹ããåæ Œããªãå Žåã¯ã
use debug::PrintTrait;
ãš[å€æ°].print();
ã䜿çšããŠãããã°ã§ããŸãã
ãã¹ã®æ€çŽ¢#
第 2 ã®å°åé¡ã§ã¯ããã¹ã®æ€çŽ¢ãå®è£ ããå¿ èŠããããŸããããã DFS ã§ãããæåã®åé¡ãããé£ããã§ãã
ãã¹ãã±ãŒã¹ã®äŸã以äžã«ç€ºããŸããk ãæ€çŽ¢ããå Žåã[h,l,j,k] ãè¿ãå¿ èŠããããŸãã
h
d l
b f j n
a c e g i k m o
ååž°çãªæ¹æ³ã䜿çšããŠè§£æ±ºããŸããçµæãæ ŒçŽããããã®ãªã¹ããäœæããååž°é¢æ°ãåŒã³åºããŸããååž°é¢æ°ã¯æ¬¡ã®ãããªããžãã¯ãå®è¡ããå¿ èŠããããŸãïŒ
- ä»»æã®ããŒãã§ãå€ãããŒãšäžèŽããå Žåã¯ãã€ã³ããã¯ã¹ãè¿ããŸãã
- å€ãããŒãšäžèŽããªãå Žåã¯ãleft ãš right ãæ€çŽ¢ããååž°çã«å®è¡ããé åãè¿ããããŸã㯠None ãè¿ããŸãã
- åèã®ååž°çãªæ»ãå€ã®é åãååŸããçŸåšã®ã€ã³ããã¯ã¹ãè¿œå ããŸãã
äžèšã®ãã¹ãã±ãŒã¹ã® j ãååž°çã«å®è¡ãããšãOption::Noneãš **[k].append (j)ãåŸãããŸããl ããŒãã«æ»ããšã[k,j].append(l)ãšOption::NoneãåŸãããŸããh ã«æ»ããšã[k,j,l].append (h)** ãåŸãããŸããïŒäžèšã®ãã¹ãŠãã€ã³ããã¯ã¹æäœã«å€æŽããå¿ èŠãããçç±ã«ã€ããŠã¯ã以äžã®æ³šæäºé ãåç §ããŠãã ããïŒ
Cairo 㯠append_front ããµããŒãããŠããªããããåŸãã«è¿œå ãããããããŸãããæåŸã«ãååŸãããã¹ã®é åãå転ãããããã®ã«ãŒããäœæããŸãã
泚æäºé ïŒ
- ã¬ã¹ãäžè¶³ããå Žåã¯ãååž°äžã«ã€ã³ããã¯ã¹ã®ã¿ãæäœããæåŸã®ã«ãŒãã§ã€ã³ããã¯ã¹ã䜿çšããŠå®éã®ã¢ã€ãã ãååŸããããšããå§ãããŸãã
- ã«ãŒãæã«ãarray ã¯.pop_back ã䜿çšããåã« .span () ããå¿ èŠããããŸãã詳现ã«ã€ããŠã¯ãhttps://github.com/starkware-libs/cairo/blob/main/corelib/src/array.cairo ãåç §ããŠãã ããã
çµè«#
ã¢ã«ãŽãªãºã ã®åºç€ãäžååãªå Žåããã®ã¿ã¹ã¯ã¯ããªãé£ããã§ããæåã¯éååž°çãªæ¹æ³ã䜿çšããŸããããæçµçã«ã¯ ref array ã®åŸªç°ä»£å ¥ã«é¢ããèšèªã¬ãã«ã®ãšã©ãŒãã°ã«ééããŸãããåŸã§ååž°çãªæ¹æ³ãæžãããšãããå€ãã®ææ³ã®åé¡ã«çŽé¢ããé·ãæéããããŠå®æãããŸããããéåžžã«æçãªçµéšã§ãããäžèšã®ã¢ã€ãã¢ã圹ç«ã€ããšãé¡ã£ãŠããŸãã