{"size": 1752, "ext": "js", "max_stars_repo_path": "Topics/13. Exam preparation/2016/Workshop/Greedy Dwarf/greedy-dwarf.js", "max_stars_repo_name": "babydon143/JavaScript-Fundamentals", "max_stars_repo_stars_event_min_datetime": "2015-05-15T02:18:27.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-07T10:04:48.000Z", "max_issues_repo_path": "Topics/13. Exam preparation/2016/Workshop/Greedy Dwarf/greedy-dwarf.js", "max_issues_repo_name": "babydon143/JavaScript-Fundamentals", "max_issues_repo_issues_event_min_datetime": "2015-05-17T16:47:21.000Z", "max_issues_repo_issues_event_max_datetime": "2017-01-04T16:19:12.000Z", "max_forks_repo_path": "Topics/13. Exam preparation/2016/Workshop/Greedy Dwarf/greedy-dwarf.js", "max_forks_repo_name": "babydon143/JavaScript-Fundamentals", "max_forks_repo_forks_event_min_datetime": "2015-05-14T14:55:26.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-04T10:03:26.000Z"}
{"max_stars_count": 63, "max_issues_count": 6, "max_forks_count": 156, "avg_line_length": 26.1492537313, "max_line_length": 73, "alphanum_fraction": 0.5234018265}
function solve(lines) {
    var valley = lines[0]
                    .split(',')
                    .map(Number);

    function isOutside(valley, index) {
        return valley[index] === undefined;
    }

    function coinsForPattern(valley, pattern) {
        var visited = [],
        coins = 0,
        index = 0,
        patternIndex = 0;

        // continue while not visited and inside valley
        while(!visited[index] && !isOutside(valley, index)) {
            coins += valley[index];
            visited[index] = true;

            index += pattern[patternIndex];

            patternIndex = (patternIndex + 1) % pattern.length;
        }

        return coins;
    }

    var results = [];

    for(var i = 2; i < lines.length; i += 1) {
        var pattern = lines[i]
                            .split(',')
                            .map(Number);

        results.push(coinsForPattern(valley, pattern));
    }

    var maxCoins = Math.max.apply(null, results);

    return maxCoins;

    // read input
        // read the valley, convert to numbers array
        // read the patterns, convert to numbers array
            // read a single patterns
    // find max coins that can be collected following a patterns
    // for every pattern, calculate the coins
        // find a way to calculate coins for a single pattern
            // start from 0
            // follow the pattern
            // collect coins on the way
            // if end of pattern, start from the beggining of the pattern
            // if outside of valley or on visited index, stop
    // find the max coins
    // return the max coins
}

var test1 = [
    '1, 3, -6, 7, 4 ,1, 12',
    '3',
    '1, 2, -3',
    '1, 3, -2',
    '1, -1',
];

solve(test1);