åèå ¥éã¬ã€ããå®äºããŠããªãå Žåã¯ãåç §ããŠãã ããã
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 ããããã«ã¯ãarray ã.span () ããå¿ èŠããããŸãã詳现ã«ã€ããŠã¯ãhttps://github.com/starkware-libs/cairo/blob/main/corelib/src/array.cairo ãåç §ããŠãã ããã
çµè«#
ã¢ã«ãŽãªãºã ã®åºç€ããªããšããã®ã¿ã¹ã¯ã¯ããªãé£ããã§ããæåã¯éååž°çãªæ¹æ³ã䜿çšããŸããããæçµçã«ã¯ ref array ã®åŸªç°ä»£å ¥ã«é¢ããèšèªã¬ãã«ã®ãšã©ãŒãã°ã«ééããŸãããåŸã§ååž°çãªæ¹æ³ãæžãããšãããå€ãã®ææ³ã®åé¡ã«ééããå®æãããŸã§ã«ããªãã®æéãããããŸããããå€ãã®ããšãåŠã³ãŸãããäžèšã®ã¢ãããŒãã圹ç«ã€ããšãé¡ã£ãŠããŸãã