Skip to content

Instantly share code, notes, and snippets.

@theycallmeswift
Last active October 7, 2015 09:47
Show Gist options
  • Save theycallmeswift/3145373 to your computer and use it in GitHub Desktop.
Save theycallmeswift/3145373 to your computer and use it in GitHub Desktop.
Solutions to the circle hackathon challenge

There is a 7-digit number with all distinct digits. The product of the first 3 digits = product of the middle 3 digits = product of the last 3 digits. Find the number.

fits = (str) ->
p1 = str.charAt(0) * str.charAt(1) * str.charAt(2)
p2 = str.charAt(2) * str.charAt(3) * str.charAt(4)
p3 = str.charAt(4) * str.charAt(5) * str.charAt(6)
return p1 == p2 && p2 == p3
for x in [1000000..9999999]
if /^(?:(.)(?!.*?\1))*$/.test(x) && fits("#{x}")
console.log("#{x}".charAt(3))
return
% Swift's solution to the A16Z Hackathon challenge.
all_distinct([]).
all_distinct([_]).
all_distinct([Head|Tail]) :- not(member(Head, Tail)), all_distinct(Tail).
set_bounds(X) :- between(0,9,X).
has_property([A,B,C,D,E,F,G]) :-
P1A is A * B, P1 is P1A * C,
P2A is C * D, P2 is P2A * E,
P3A is E * F, P3 is P3A * G,
P1 = P2, P2 = P3.
a16z(List) :-
length(List, 7),
all_distinct(List),
has_property(List),
!.
solve(Puzzle) :-
maplist(set_bounds, Puzzle),
a16z(Puzzle).
@theycallmeswift
Copy link
Author

@JetFault
Copy link

Yo can you explain the regex on that?

@JetFault
Copy link

nvm got it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment