Skip to content

Instantly share code, notes, and snippets.

@torkleyy
Forked from anonymous/playground.rs
Created December 8, 2017 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torkleyy/52aba8a89cc1331470b4460a875436ca to your computer and use it in GitHub Desktop.
Save torkleyy/52aba8a89cc1331470b4460a875436ca to your computer and use it in GitHub Desktop.
Rust code shared from the playground
const INPUT: &str = "6169763796227664136644229724736711773811471986347364813198\
2449728688116728695866572989524473392982963976411147683588415878214189996163533\
5845471757941581181487242988327988983333997865614591526441446699598873414819683\
1917298735798978579136673284993278834377211217661472385847495991971385539887695\
6427631354172668133549845585632211935573662181331613137869866693259374322169811\
6836353253215972428893581471233581177749146537873713685747843767216521817923716\
3528837672978496752682491519252674493518798957134774622211362557796347614192318\
7534658445615596987614385911513939292257263723518774888174635963254624769684533\
5314437457293443419737464693268381862484484835874775632858674999564462187752323\
7438343392183599313646338362886111557314285435894329114876629965363319558213593\
4544964657663198387794442443531964615169655243652696782443394639169687847463721\
5855279478399921824153931999648936583227576346752744229932379553541851948686384\
5489144289393569445432423596815591396328264264996815328462615411147838991431676\
5783434365458352785868895582488312334931317935669453447478936938533669921165437\
3737414483784773918127799715289754782986887549392164214292517275555964819433222\
6628952799667285638764867416699773134255898657525879326198681717748719751228216\
2964167151259485744835854547513341322647732662443512251886771887651614177679229\
9842711912923747559154573727758561785399651313195682782523262426151514127722542\
5784741379981141728748132174537287951376623574534787263294677653817366737122897\
7212143996391617974367923439923774388523845589769341351167311398787797583543434\
7253743436117243793995661974321541468813445283198264345542393736669625462712997\
1774359122556756465551135325519751651521396386238376225895995747478971856475884\
3367325794589886852413314713698911855183778978722558742329429867239261464773646\
3894843184465743753236741366384521738151767323854686752152647367862428662956489\
9736541263749969281774793798262851892638193927993599371241893856748828924677945\
8432179335139731952167527521377546376518126276";
fn circular(v: &[usize], mut i: usize) -> usize {
while i >= v.len() {
i -= v.len();
}
v[i]
}
fn main() {
assert!(INPUT.len() > 1);
assert_eq!(INPUT.len() & 0x1, 0);
let input: Vec<_> = INPUT
.chars()
.map(|c| c as usize - '0' as usize)
.collect();
let half = input.len() / 2;
let mut sum = 0;
for i in 0..input.len() {
if input[i] == circular(&input, i + half) {
sum += input[i];
}
}
println!("Sum: {}", sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment