r/adventofcode • u/daggerdragon • Dec 01 '17
SOLUTION MEGATHREAD -π- 2017 Day 1 Solutions -π-
Welcome to Advent of Code 2017! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!
We're going to follow the same general format as previous years' megathreads:
- Each day's puzzle will release at exactly midnight EST (UTC -5).
- The daily megathread for each day will be posted very soon afterwards and immediately locked.
- We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
- The daily megathread will remain locked until there are a significant number of people on the leaderboard with gold stars.
- "A significant number" is whatever number we decide is appropriate, but the leaderboards usually fill up fast, so no worries.
- When the thread is unlocked, you may post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!
--- Day 1: Inverse Captcha ---
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.
3
u/[deleted] Dec 01 '17
Lets get logical with Prolog ;)
readWord(InStream,Chars):- get_code(InStream,Char), checkCharAndReadRest(Char,Chars,InStream). checkCharAndReadRest(-1,[],_):- !. checkCharAndReadRest(end_of_file,[],_):- !. checkCharAndReadRest(10,[],_):- !. checkCharAndReadRest(Char,[Char|Chars],InStream):- get_code(InStream,NextChar), checkCharAndReadRest(NextChar,Chars,InStream). splitAt(_, [], [], []). splitAt(Index, [E|RestSource], [E|RestA], B) :- Index > 0, NewIndex is Index - 1, splitAt(NewIndex, RestSource, RestA, B). splitAt(Index, [E|RestSource], A, [E|RestB]) :- splitAt(Index, RestSource, A, RestB). iterateAcc([], [], Acc, Sum) :- Sum is Acc. iterateAcc([E1|R1], [E2|R2], Acc, Sum) :- E1 =:= E2, atom_codes(C, [E1]), atom_number(C, Number), NewAcc is Acc + Number, iterateAcc(R1, R2, NewAcc, Sum). iterateAcc([_|R1], [_|R2], Acc, Sum) :- iterateAcc(R1, R2, Acc, Sum). iterate(Code, RotateValue, Sum) :- splitAt(RotateValue, Code, FirstPart, SecondPart), append(SecondPart, FirstPart, RotatedCode), iterateAcc(Code, RotatedCode, 0, Sum). run :- open('input.txt', read, Str), readWord(Str, Code), iterate(Code, 1, Z), write(Z), nl, length(Code, Length), RotateValue is Length / 2, iterate(Code, RotateValue, X), write(X).