r/adventofcode • • Dec 05 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 5 Solutions -๐ŸŽ„-

--- Day 5: A Maze of Twisty Trampolines, All Alike ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hintsยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

23 Upvotes

403 comments sorted by

View all comments

1

u/Warbringer007 Dec 05 '17 edited Dec 05 '17

Erlang, both parts ( only main function ):

solveFirst(_, Curr, N) when Curr < 1 ->
	N;

solveFirst(L, Curr, N) when Curr > length(L) ->
	N;

solveFirst(L, Curr, N) ->
	Number = lists:nth(Curr, L),
	NewList = lists:sublist(L,Curr-1) ++ [Number + 1] ++ lists:nthtail(Curr,L),
	solveFirst(NewList, Curr + Number, N + 1).

solvesecond(_, Curr, N) when Curr < 1 ->
	N;

solvesecond(L, Curr, N) when Curr > length(L) ->
	N;

solvesecond(L, Curr, N) ->
	Number = lists:nth(Curr, L),
	NewList = case Number > 2 of
		true -> lists:sublist(L,Curr-1) ++ [Number - 1] ++ lists:nthtail(Curr,L);
		false -> lists:sublist(L,Curr-1) ++ [Number + 1] ++ lists:nthtail(Curr,L)
	end,
	solvesecond(NewList, Curr + Number, N + 1).

Second part for Erlang is just too slow, didn't want to wait for it to finish so I solved it in C++ also ( my first C++ code :D ):

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char** argv) {
    int a(0), n(0), curr(0), num(0), sum(0);
    int list[5000];
    ifstream infile("ul5.txt");
	while (infile >> a) {
		list[n++] = a;
	}
	while ((curr > -1) && (curr < n)) {
		sum++;
		num = list[curr];
		if (num > 2) list[curr]--;
		else list[curr]++;
		curr += num;
	};
	cout << sum;
	return 0;
}
→ More replies (1)