Last active
May 4, 2017 07:53
-
-
Save paulmr/5deda4d9b5063c93d859fe00ce20255b to your computer and use it in GitHub Desktop.
Advent of Code 2015, Day 25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(advent25). | |
-export([main/1]). | |
-define(Start, {{1,1}, 20151125}). | |
nextN(Last) -> Last * 252533 rem 33554393. | |
nextXY({ X, 1 }) -> { 1, X + 1 }; | |
nextXY({ X, Y }) -> { X + 1, Y - 1 }. | |
nextZip(Fa, Fb) -> fun ({A,B}) -> {Fa(A), Fb(B)} end. | |
next() -> nextZip(fun nextXY/1, fun nextN/1). | |
find(Start, Fn, Pred) -> | |
case Pred(Start) of | |
true -> Start; | |
false -> find(Fn(Start), Fn, Pred) | |
end. | |
findXY(X,Y) -> find(?Start, next(), fun ({{Xx, Yy}, _}) -> (X =:= Xx) and (Y =:= Yy) end). | |
main([]) -> main(["3075", "2981"]); | |
main([X, Y]) -> | |
{ _, Part1 } = findXY(list_to_integer(X), list_to_integer(Y)), | |
io:format("Part 1: ~B~n", [Part1]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment