This topic is about Day 6 of the Advent of Code 2022 .
Sorry for the delay in opening this post. Hope we already have some solutions to day 6
Link to our leaderboard:
https://adventofcode.com/2022/leaderboard/private/view/370884
The entry code is:
370884-a6a71927
Good luck!
3 Likes
I feel like we got an easy challenge today, and should be prepared for something tricky next time 
main(File) ->
{ok, RawData} = file:read_file(File),
Data = binary_to_list(RawData),
io:format("part 1: ~p~n", [solve1(Data)]),
io:format("part 2: ~p~n", [solve2(Data)]).
solve1(Data) ->
solve(Data, 4, 0).
solve2(Data) ->
solve(Data, 14, 0).
solve(List, N, Pos) ->
{Set, _} = lists:split(N, List),
case sets:size(sets:from_list(Set)) of
N -> Pos + N;
_ -> solve(tl(List), N, Pos + 1)
end.
2 Likes
Yeah it was easy! And I think I will do all remaining problems in Erlang now. Never thought I’d feel this at home this early.
-module(day_6).
-export([solve/0]).
input() ->
{ok, DataStream} = file:read_file("data/6.txt"),
DataStream.
solve() ->
ParsedStream = binary:bin_to_list(input()),
{solve1(ParsedStream), solve2(ParsedStream)}.
solve1(ParsedStream) ->
solve_for(ParsedStream, 4, 0).
solve2(ParsedStream) ->
solve_for(ParsedStream, 14, 0).
solve_for(ParsedStream, BufferSize, Counter) ->
{Buffer, _} = lists:split(BufferSize, ParsedStream),
case unique_members(Buffer) of
true ->
Counter + BufferSize;
false ->
solve_for(tl(ParsedStream), BufferSize, Counter + 1)
end.
unique_members(List) ->
length(List) == length(ordsets:from_list(List)).
2 Likes
Wow that was clever how you managed the uniqueness

2 Likes