以äžã®ããã¹ããæ¥æ¬èªã«ç¿»èš³ããŠãã ããïŒ
没æå®æå ¥éšä»»å¡çåèå®è£ å ¥éš
Racing Riverboats ã¯ãCairo ããŒã¿æ§é ãšã¢ã«ãŽãªãºã ã®ã¿ã¹ã¯ã§ãã Think Cairo ã® 3 çªç®ã®ã¿ã¹ã¯ã§ãããé åå ã®éè€å€ãæ€çŽ¢ããããã®èŸæžã¯ãšãªã®åé¡ãæ±ããŸãã2 ã€ã®å°åé¡ããããŸãã
ãªã¹ãå ã®éè€å€ã®ã€ã³ããã¯ã¹#
æåã®åé¡ãé±·ãæ¢ããã¯ãæ¬è³ªçã«ã¯é åå ã§éè€é ç®ãèŠã€ããã€ã³ããã¯ã¹ãå¥ã®é åã«ããŒã¯ãããã®ã§ãã
æ¯èŒããå¿
èŠã®ããé
åã®ãµãèŠçŽ ã¯ãu32ãu32ãfelt252 ãããªã struct ã§ããåæããŒã¿ã¯Array<Obstacle>
ã§ãã
#[derive(Drop)]
struct Obstacle {
length: u32,
width: u32,
description: Array<felt252>
}
æåã®ã¢ãããŒãã¯ãFelt252Dictã䜿çšããObstacle ãã«ãŒãããŠåãåºããFelt252Dict ã«é
眮ããããšã§ãããã㯠solidity ã®mapping(index => struct)
ã«äŒŒãŠããŸããå€ãååšããå Žåã¯ã€ã³ããã¯ã¹ãèšé²ããååšããªãå Žåã¯æ¿å
¥ããŸãã
æåã®ããŒãžã§ã³ã®ã³ãŒããæžããåŸãObstacle ã®ãµãèŠçŽ ã§ããArray<felt252>
ã Copy ã§ããªããããfelt252Dict.get(index) -> Obstacle
ã¯ç¡å¹ã§ãããããã£ãŠãã¢ãããŒããå€ããå¿
èŠããããŸãã
èŸæžã䜿çšããªãå Žåã2 ã€ã®ãã¹ããããã«ãŒãã䜿çšã㊠Obstacle ãæ¯èŒããéè€ãããã®ãäžæãªã¹ãã«è¿œå ãããã®åŸã€ã³ããã¯ã¹ãèŠã€ãåºãå¿ èŠããããŸãããããããã¹ãã®çµæãã¬ã¹ãäžè¶³ããŠããŸããå®éã«ã¯ 320 äžããããŸããããç®æšã® 180 äžãè¶ ããŠããŸããŸãããæé©åãå¿ èŠã§ãã
ãã¹ãã±ãŒã¹ã®ã¬ã¹å¶éãåæãããšã2 ã€ã® Obstacle ã 1 åæ¯èŒããã¬ã¹ã®ã³ã¹ã㯠26100 ã§ãããã¹ãã±ãŒã¹ã«ã¯ 13 åã® Obstacle ãå«ãŸããŠããããã1800000 ã¬ã¹ã§ 69 å以äžã®æ¯èŒããã§ããŸãããäžæ¹ãäžèšã®ã¢ã«ãŽãªãºã ã§ã¯ããã¹ãŠã® Obstacle ãæ¯èŒããããã« 78 åïŒ1+2+...+12ïŒã®æ¯èŒãå¿ èŠã§ãããããã£ãŠãæ¯èŒåæ°ãæžããå¿ èŠããããŸãã
ãã®ãããdict ã®ãã¹ãã±ãŒã¹ã®æ žå¿ã©ã€ãã©ãªã䜿çšããŠãFelt252Dict ã«ã€ã³ããã¯ã¹ãä¿åããååŸã§ããå Žåã¯ã¹ãããããããšã§ãäžèŠãªæ¯èŒãæžããããšãã§ããŸããããããæçµçã«ã¯ã¬ã¹ãäžè¶³ããŠããŸããæ¯èŒåæ°ãæžããããšã¯ãã¯ãæé©åã§ããŸããããããã£ãŠãåäžã®æ¯èŒã³ã¹ããå ·äœçã«ã¯ Obstacle.description ã®æ¯èŒãæé©åããå¿ èŠããããŸãã
ãã°ããã¢ã€ãã¢ãæµ®ãã°ãã«ç«ã¡åŸçããŠããŸã£ãã®ã§ãNG ã®ãã¥ãŒã¿ãŒã«çŽæ¥çžè«ããObstacle ãããã·ã¥åããããšã§ O (n) ã®è€éãã§ç®æšãéæã§ãããšããã¢ã€ãã¢ãããããŸããã
ã§ã¯ãstruct ãã©ã®ããã«ããã·ã¥åããã®ã§ããããïŒæåã« Obstacle ã®#[derive(Drop)]
ã#[derive(Drop,Serde)]
ã«å€æŽãã次ã®ã³ãŒãã䜿çšããŠããã·ã¥ãååŸããŸãã
let mut raw_data = Default::default();
obs.serialize(ref raw_data);
let hash = poseidon_hash_span(raw_data.span());
ãããããªã³ã©ã€ã³ãã¹ãã«åæ Œã§ããªãããšãããããŸããããªããªããObstacle ã¯å€æŽã§ããªãããã§ãã
Obstacle ãå€æŽããã«ããã·ã¥åããã«ã¯ãstruct ã®ãã¹ãŠã®ããããã£ãæåã§ããã±ãŒãžåããŠããããã·ã¥åããå¿ èŠããããŸãã
ããã·ã¥åãæåããããdict ã«insert (index, hash)
ãšget(index)
ãè¡ãã ãã§ãéè€ããã€ã³ããã¯ã¹ãååŸã§ããŸããã³ãŒãã¯ãã¹ããæ£åžžã«ãã¹ããŸãã
ãŸããu_size ãš felt252 ã®çžäºå€æã«é¢ããå€ãã®æäœããããinto
ãšunwarp
ã䜿çšããå¿
èŠããããŸããããã¥ã¡ã³ããåç
§ããŠè§£æ±ºããŠãã ããã
ãã 1 ã€ã®èœãšãç©Žã¯ãindex ã 0 ã§å€ã 0 ã®å Žåãdict.get(0)
ãšdict.get(ååšããªãã€ã³ããã¯ã¹)
ã®äž¡æ¹ã 0 ãè¿ããããå€æã誀ã£ãŠããŸããåŸã®åé¡ã®é¢é£ãããã¹ãã«åæ Œã§ããªããªãããšã§ãã
ããã«ã¯ãuse nullable::{NullableTrait, match_nullable, FromNullableResult};
ã©ã€ãã©ãªã䜿çšããŠè§£æ±ºããå¿
èŠããããŸãããã®éšåã¯ãnullable ã®ãœãŒã¹ã³ãŒããåç
§ããŠåŠç¿ããããšããå§ãããŸãã
ãŸãšãããšã第 1 ã®åé¡ã®ãã€ã³ãã¯æ¬¡ã®ãšããã§ãïŒ
- struct ãããã·ã¥åããæ¹æ³
- u_size ãš felt ã®çžäºå€æ
- Felt252Dict ã® newãgetãinsert
- nullable ã®äœ¿çš
ã«ãŒãå ã®æ¡ä»¶æ㧠Option ãè¿ã#
ãã®åé¡ã§ã¯ãé åïŒå·ïŒãè¶ ããããã«æå°éã®ã¹ãããæ°ãè¿ãå¿ èŠããããŸããã¹ãããã®é·ã㯠3 ãè¶ ããŠã¯ãããŸãããæåã®ã»ã«ãšæåŸã®ã»ã«ãé±·ã§ããå ŽåãOption::None ãè¿ããŸãã
ã¢ãããŒãïŒãã¹ããããã«ãŒãã䜿çšãããžã£ã³ãããšã« 3ã2ã1 ãå€æãããžã£ã³ãã§ããå Žåã¯ãžã£ã³ãããã¹ãããæ°ã + 1 ããŸãããã¹ãŠãžã£ã³ãã§ããªãå Žåã¯ãçŽæ¥ Option::None ãè¿ããè€æ°åã®ãžã£ã³ãã§ç®æšãéæããŸãã
ãã®åé¡ã®å®è£ ã¯æ¯èŒçç°¡åã§ããæåã®åé¡ã§ååŸããã€ã³ããã¯ã¹é åã䜿çšããŠã2 ã€ã®ã«ãŒãã§è§£æ±ºããŸããäž»ãªåé¡ã¯ããã¹ãã§ã¬ã¹ãäžè¶³ããããšã§ããã€ãŸãã空ã®å·ãå€æããããšãšãå§ç¹ãšçµç¹ãé±·ã§ããããšãå€æããããšãäºåã«å€æããŠãã¬ã¹ãç¯çŽããå¿ èŠããããŸãã
ãã¹ãã±ãŒã¹ãæ¯èŒããprint ã掻çšããŠãã©ãã«ãžã£ã³ããããã確èªããŠãã ããã
æåã®åé¡ã§æ¢ã«èª¬æããããã€ãã®èœãšãç©ŽããããŸãã®ã§ãäºåã«ä¿®æ£ããŠãããšè¯ãã§ãããã
æåŸã« verify ãæåºããåŸããã¹ãããã¹ã§ããªãå°ããªãã°ããããŸããã«ãŒãã®äžææ¡ä»¶ã¯ river.len ãªã®ã river.len-1 ãªã®ãã泚æããå¿ èŠããããŸãã
ãã®åé¡ã§ã¯ãå€ãã®ã«ãŒãå ã§ã®æ»ãå€ãæ±ãå¿ èŠããããŸãããcairo ã«ã¯ return ããªãããããšã©ãŒãçºçãããããå€ãã®ä¿®æ£ãå¿ èŠã§ãã
ãŸããOption ã«é¢é£ããæäœãå€ãåŠã¶ããšãã§ããŸãã
ãŸãšã#
cairo ã®ã¡ã¢ãªã¯äžå€ã§ãããã¹ããããäžæžãããããšã¯ã§ããªããããèŸæžã®å¹æãåŸãããã«ã¯ããã€ãã®ãã¯ããã¯ãå¿ èŠã§ããé¢é£ããæäœã«æ £ããããšã¯ãå°æ¥ã®éçºã«åœ¹ç«ã¡ãŸãã