defmoduleAdaptersdodefgetSortedAdapters()do{:ok,text}=File.read("10.txt")lines=String.split(text,"\n")lines=List.delete_at(lines,length(lines)-1)# remove empty linejoltages=Enum.map(lines,&String.to_integer/1)Enum.sort(joltages)enddefround1()doadapters=getSortedAdapters(){ones,threes,_prev}=Enum.reduce(adapters,{0,0,0},fn(x,{ones,threes,prev})->conddo(x-prev)==1->{ones+1,threes,x}(x-prev)==3->{ones,threes+1,x}true->{ones,threes,x}endend)threes=threes+1# hop from the last adapter to the built-in oneIO.puts("#{ones} * #{threes} = #{ones*threes}")enddefcount_connections_upto(current,sofar)doIO.puts(current)sofar|>Enum.take_while(fn{adapter,_ways}->adapter>=(current-3)end)|>Enum.map(fn{_adapter,ways}->waysend)|>Enum.sum()enddefcount_connections(adapters,sofar)do[current|remaining]=adaptersn=count_connections_upto(current,sofar)iflength(remaining)>0docount_connections(remaining,[{current,n}|sofar])elsenendenddefround2()doadapters=getSortedAdapters()IO.puts(count_connections(adapters,[{0,1}]))endendAdapters.round2()
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My Elixir solution: