re-organise repo

This commit is contained in:
2023-11-16 10:48:53 +00:00
parent f62c52d8a9
commit f133e157a1
87 changed files with 11792 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
numbers = []
rolling = []
while True:
a = input("")
if a == "a":
break
numbers.append(a)
print("Numbers are Inputed")
z = 0
while True:
if z == len(numbers)-2:
break
a = int(numbers[z])+int(numbers[z+1])+int(numbers[z+2])
rolling.append(a)
z = z + 1
inc = 0
x = 0
while True:
if x == len(rolling)+1:
break
if int(rolling[x]) > int(rolling[x-1]):
inc = inc + 1
x = x + 1
print(inc)

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,27 @@
lines = []
while True:
try:
a, b, c, d = map(int, input().replace(",", " ").replace(" -> ", " ").split())
lines.append([(a, b), (c, d)])
except ValueError:
break
x = 0
for line in lines:
if max(line[0]) > x or max(line[1]) > x:
x = [max(line[0]), max(line[1])][max(line[0]) < max(line[1])]
diagram = [[0 for i in range(x + 1)] for j in range(x + 1)]
for line in lines:
if line[0][1] == line[1][1]:
for i in range(min(line[0][0], line[1][0]), max(line[0][0], line[1][0])+1):
diagram[line[0][1]][i] += 1
elif line[0][0] == line[1][0]:
for i in range(min(line[0][1], line[1][1]), max(line[0][1], line[1][1])+1):
diagram[i][line[0][0]] += 1
count_bigger = 0
for line in diagram:
print(line)
for num in line:
count_bigger += [0, 1][num > 1]
print(count_bigger)

View File

@@ -0,0 +1,38 @@
lines = []
while True:
try:
a, b, c, d = map(int, input().replace(",", " ").replace(" -> ", " ").split())
lines.append([(a, b), (c, d)])
except ValueError:
break
x = 0
for line in lines:
if max(line[0]) > x or max(line[1]) > x:
x = [max(line[0]), max(line[1])][max(line[0]) < max(line[1])]
diagram = [[0 for i in range(x + 1)] for j in range(x + 1)]
for line in lines:
if line[0][1] == line[1][1]:
for i in range(min(line[0][0], line[1][0]), max(line[0][0], line[1][0]) + 1):
diagram[line[0][1]][i] += 1
elif line[0][0] == line[1][0]:
for i in range(min(line[0][1], line[1][1]), max(line[0][1], line[1][1]) + 1):
diagram[i][line[0][0]] += 1
else:
print(line)
xs, x, ys, y = min(line[0][0], line[1][0]),\
max(line[0][0], line[1][0]), \
[line[0][1], line[1][1]][line[0][0] > line[1][0]],\
[line[0][1], line[1][1]][line[0][0] < line[1][0]]
yp = ys
for i in range(xs, x+1):
diagram[yp][i] += 1
yp += [-1, 1][ys < y]
count_bigger = 0
for line in diagram:
print(line)
for num in line:
count_bigger += [0, 1][num > 1]
print(count_bigger)

View File

@@ -0,0 +1,18 @@
#it is very not efficient for pt2 but I didn't have much time today to think of a better solution
fish = [2,3,1,3,4,4,1,5,2,3,1,1,4,5,5,3,5,5,4,1,2,1,1,1,1,1,1,4,1,1,1,4,1,3,1,4,1,1,4,1,3,4,5,1,1,5,3,4,3,4,1,5,1,3,1,1,1,3,5,3,2,3,1,5,2,2,1,1,4,1,1,2,2,2,2,3,2,1,2,5,4,1,1,1,5,5,3,1,3,2,2,2,5,1,5,2,4,1,1,3,3,5,2,3,1,2,1,5,1,4,3,5,2,1,5,3,4,4,5,3,1,2,4,3,4,1,3,1,1,2,5,4,3,5,3,2,1,4,1,4,4,2,3,1,1,2,1,1,3,3,3,1,1,2,2,1,1,1,5,1,5,1,4,5,1,5,2,4,3,1,1,3,2,2,1,4,3,1,1,1,3,3,3,4,5,2,3,3,1,3,1,4,1,1,1,2,5,1,4,1,2,4,5,4,1,5,1,5,5,1,5,5,2,5,5,1,4,5,1,1,3,2,5,5,5,4,3,2,5,4,1,1,2,4,4,1,1,1,3,2,1,1,2,1,2,2,3,4,5,4,1,4,5,1,1,5,5,1,4,1,4,4,1,5,3,1,4,3,5,3,1,3,1,4,2,4,5,1,4,1,2,4,1,2,5,1,1,5,1,1,3,1,1,2,3,4,2,4,3,1]
day = 0
index = 0
while True:
if day == 256:
break
if index == len(fish):
index = 0
day = day + 1
print("Day",day)
if fish[index] == 0:
fish[index] = 6
fish.append(9)
elif fish[index]!=0:
fish[index]= fish[index]-1
index = index + 1
print("Number fo fish ",len(fish))

View File

@@ -0,0 +1,7 @@
numbers = []
while True:
a = input("")
if a == "a":
break
numbers.append(a)
print(numbers)

File diff suppressed because one or more lines are too long

29
2021/day1/day1.erl Normal file

File diff suppressed because one or more lines are too long

16
2021/day1/day1_p1.hs Normal file
View File

@@ -0,0 +1,16 @@
import Data.List
main :: IO ()
main = do
input <- readFile "input.txt"
print . count . map read $ words input
count :: [Int] -> Int
count (x:xs) = count_f x xs
count_f :: Int -> [Int] -> Int
count_f _ [] = 0
count_f nb (x:xs)
| x > nb = 1 + count_f x xs
| otherwise = count_f x xs

20
2021/day1/day1_p2.hs Normal file
View File

@@ -0,0 +1,20 @@
import Data.List
main :: IO ()
main = do
input <- readFile "input.txt"
print . count . average . map read $ words input
average :: [Int] -> [Int]
average (x:y:[]) = []
average (x:y:z:xs) = x+y+z:average (y:z:xs)
count :: [Int] -> Int
count (x:xs) = count_f x xs
count_f :: Int -> [Int] -> Int
count_f _ [] = 0
count_f nb (x:xs)
| x > nb = 1 + count_f x xs
| otherwise = count_f x xs

2001
2021/day1/input.txt Normal file

File diff suppressed because it is too large Load Diff

10
2021/day1/test.txt Normal file
View File

@@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263

101
2021/day10/day10.erl Normal file
View File

@@ -0,0 +1,101 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day3 solve
%%
-module(day10).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' -> Input;
Line -> read_input(IO, Input ++ [Line])
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> [ X || X <- Line, [X] /= "\n"]
end.
solution1(Input) ->
lists:sum([parse(X) || X <- Input]).
solution2(Input) ->
List = [incomplete(X) || X <- Input],
Filter_fail = lists:filter(fun(X) -> X /= 'failed' end, List),
Scores = lists:sort([lists:foldl(fun(X, Acc) -> (Acc * 5) + value(X) end, 0, F) || F <- Filter_fail]),
lists:nth(length(Scores) div 2 + 1, Scores).
incomplete([H|T] = Input) ->
io:format("~p~n", [Input]),
try
incomplete(T, [partner(H)])
of
ok -> 'ok';
{incomplete, Rest} -> Rest
catch
_:_ -> 'failed'
end.
incomplete([], Expected) -> {incomplete, Expected};
incomplete(Rest, []) -> {incomplete, incomplete(Rest)};
incomplete([H|T], [H|Rest]) -> incomplete(T, Rest);
incomplete([H|T], Expected) -> incomplete(T, [partner(H)|Expected]).
parse([H|T] = Input) ->
io:format("~p~n", [Input]),
try
parse(T, [partner(H)])
of
_ -> 0
catch
_:{invalid_partner,")"} -> 3;
_:{invalid_partner,"]"} -> 57;
_:{invalid_partner,"}"} -> 1197;
_:{invalid_partner,">"} -> 25137
end.
parse([], Expected) -> {parsed, Expected};
parse(Rest, []) -> {incomplete, parse(Rest)};
parse([H|T], [H|Rest]) -> parse(T, Rest);
parse([H|T], Expected) -> parse(T, [partner(H)|Expected]).
partner($() -> $);
partner($[) -> $];
partner(${) -> $};
partner($<) -> $>;
partner(X) -> throw({invalid_partner, [X]}).
value($)) -> 1;
value($]) -> 2;
value($}) -> 3;
value($>) -> 4.

94
2021/day10/input.txt Normal file
View File

@@ -0,0 +1,94 @@
([{{(([{<<([<[{}{}][{}<>]>{(()())(<>)}]{[(()[])<[][]>]<[()[]]<{}<>>>}){{<<[]<>>(()[])>{({}(
{<({[{[({(<[({[][]}(()<>))(({}<>)<()[]>)][<{{}[]}[{}()]><[(){}][<><>]>]><{{({}<>){<><>}}[(<><
<{({{{{{{<[([([]<>)]{({}())<()()>})]<[([<>[]][()[]]){[(){}]<[]{}>}]{<[<>{}][(){}]><{[][]}{<><>}>}>>}<<
([([{[[{{[((((()[]))<[()<>]{(){}}>))[[({()()}{[][]})(({}[])(<>{}))][{<(){}><(){}>}]]]<[[{[<>[]]}
{{[{{{<(([{[<{[]{}}<[]{}>>([<>[]][{}[]])]<[(<>[])[<>[]]]({[]<>]{()<>})>}{{<<[]()>[<>[]]>({()<>}<[]<>>)}[
<<{[<[<<[{[[<<<>[]>[[][]]>]]}]>[({[[{([]<>){[]()}}{{()<>}[{}()]}][[(()<>)(<>{})]{[<><>]({}<
{[[<<({<[[[<[[<>{}]{{}[]}]<(<>())>>]]>{<{{<[{}[]][<>[]]>[(<><>)<(){}>]}<(<{}[]><{}()>)([()<>]
([<({<(<{[<<<({}{})({})>((()()))>[(<[]{}>{[]()}>]>]}([({<<<>[]>[{}()]><{[]()}(<>[])>})[([[[][]][[]()]]({()}
[{<{<<{[<({{{[[]()]<[]{}>}(<(){}>([]{}))}}[[<<[]()>{()<>}>]{[<()>({}{})]({<><>}([]{}))}])[<[<
({<<<{<[[(<[<[[][]]([]<>)><{<>{}}[{}{}]>]>{[<[{}]>({[][]}<()()>)]<[{<>[]}[[]{}]]>}){[<[(())<{}{}>]<(<>())[[]{
<(<<(({<<[((<[[]()]{{}{}}>[<<>{}>([]<>)]){{<[]<>>([]<>)}<([]())<{}>>})<[[(()())]([{}()](<>
[[<{([({[{{{<{()()}{{}{}}>{[[]<>](()())}}}<[<(<><>)(<><>)>{{{}<>}(<>())}]{(<<>()>([]{}))({()<>}[<>{}])}
<{{(<<{{([<[<{()[]}>][<<{}<>>>[([]<>)({}[])]]>])}<{(<<([[]{}])<[[]{}]{{}{}}>><{{(){}}<()<>>}([{}{}][
[[<(([{((<{([{{}<>}[<>[]]]<[()()]<[][])>)}[<{[[]]<{}<>>}([<><>]{[][]})>]>(<[<<<><>>>(<<>{}>
{{{{[[<<<{{<({{}}<()<>>)[{(){}}(<>())]>{[<<>[]>{()()}]{(()[]){<><>}}}}<{([(){})){{[]}<{}{}
(<{<[<<{(<{({<{}()>{<>{}}}{[()[]]{<>[]}})<({{}}{<><>})[<(){}>[<><>]]>}>)}>>]><<({<[{([{{()<>}[[]{}
[{{<{(({[<([[[()][<><>]][[<>[]]<<>()>]]<[{{}[]}({}<>)]([[][]]<<>{}})>)>]})(<([([(([][])[()<>]){[<>[]]({}
[[{[{{[[((<{<{<>()}([]<>)>([<>{}]<[]()>)})({[{<>[]}<{}<>>](<(){}>(<>{}))}({((){})({}<>)}<<<>
<{(<<<{[[(<(<[{}()]{[][]}>[{<>}[<>{}]])((<()()>[[]{}]))>([<([][]){[]<>}>([(){}]{(){}})]<({[
[(([([[{(([{[{<>{}}<{}<>>][[(){}]([]{})]}([<[]<>>{[][]}][[{}]{[]()}])})<<({({}{})<[]{}>}<{{}<>}<{}[]>>
<({<<<<({<[{[<{}()>](<<>{}>[[]<>])}]<[{(()<>)([]())}(({}<>){{}<>})]{({(){}}[{}[]]>({<><>}<{}{}>)}>><{<<(
[[<<<(<{([(<<{{}()}<{}()>>(([][])[[]{}])>{<{<>[]}><[<>{}]>})(([[<>{}]([]())]))]>}<<{<{(([]{}){()<>})<<{}<>>
<{<[{[<([(<<{{{}()}({}())}<((){})<[]{}>}>({<[]()><()<>>}[{()()}[<>]])>(([{<>()}<<>()>][([][])])(((()[]))<((
<{[<{{[<(<<{[([][])<{}[]>]<<[]<>>>}{({<>()}<[]<>>){{()()}{[]{}}}}>>[<<<({}[])>{[{}[]](()[])}>>
(<(<[{{{[((({[{}{}]<<>()>}]{([<>[]](()[]))})[[{<<>[]>[{}()]}[[{}[]][{}[]]]][{(<><>)[<><>]}[<<><>>[
([([<[<{<[<<[<[]{}>({}())][<{}[]>({})]>[({[]}{[]<>})<<{}[]>[{}{}]>]>[[{<{}()>[<><>]}{([]<>)([]())}]{(
({[((({(<[{[([{}<>](()<>))[(()())[()()]]]}]>){({(<<(()())[[]<>]>{{(){}}{<>}}>(<(<>())[(){}]>))<{{[<>()][(
{<{<{({<([({<(<><>){<><>}>(<[][]>{<>()})}<<([]()){<>()}><([][])[(){}]>>){(<<()()>}<{{}{}}({}{})>)((<{}{}>(
<{{(<([{<(([(<{}<>>[{}{}])([[][]]{[]<>})]({<()[]>})){<(([]<>)({}[]))><[({}<>)]([()<>](()()))>})>[<{(<<[](
<{<<(({{<{<{([<><>]({}[]))[<<>[]>(()<>)]}{[{()}][<[]>{{}()}]}>}<<[{{(){}}{()[]}}[<[]<>>{[]<>}]]([<{}{}>{<>[]
<<<[[((<({<<(([][])<<>[]>)[[[]<>]<{}{}>]>({{()<>}}{<[]{}><()<>>})>}<(<<([]<>)<[]>>{[<><>]}>[[[()[]]<{}{}>]({[
[[({{((({(<(<([]{})([]{})>({{}[]}([]()]))>({<{(){}}[[][]]>(([]{})[<><>])}([{[][]}<<>()>])))[(<<[[]<>][{}<>]>
({[{{(({{[<(<[<>{}]((){})><[<><>]([]())>)<{({}<>)<{}()>}{{(){}}<[]{}>}>>[{{<<>{}>{{}}}<[{}[]][{
{[{{(<(([([{[[(){}](()<>)]<[[][]]([][])>}]{{(((){})([]<>))(([]<>)[()<>])}{[<{}[]>]<[<><>](<>())>}
({({{[<<{((((<[]{}>{<><>})))({<<<>{}>({}())>}<<<[]()>{{}()}>[<{}[]>[<><>]]>))[(<(({}[])<<>()>>({{}<>}<[]<
{{[{<((<{<(({[()()]{{}[]}}{([]()){<>{}}>)<[[[][]]({}[])]>)<({(()<>)[<>]})<[[()<>]]<[()()]<[]<>>>>>>{[((
[(<<<<<[<(([[{()()}[()<>]]]{(<{}{}>{{}[]})<<[]()>([][])>}){[{<[]()>{{}<>}}{[[][]][{}]>]})>]<{{(<<[[]{}
(({[<[<([[<({[{}<>]}[(()()){<>{}}])<(<<>()>)[(()<>){{}{}}]>>]{<(([()()][[]<>]){{[]{}}([][])})>}][[
[<((<{([(({{{{{}}{[]()}}<{()<>}{(){}}>}}[(<<{}{}>><[<><>]{[]<>}>)<{<<><>>{()<>}}<[()<>]{[]{}}>>]))]{(({{
{[[<([<[{{(<[([]<>)([]<>)][[<>{}]]>){({{()<>}<[]{}>}<{<>{}}(<><>)>)>}{<<{((){}){<>{}}}<[<><>]([]())>
{[{([<<{<((<(<(){}>([]()))<{()<>}<{}()>>><<[{}()]{()()}>>)[({<{}{}>{{}<>}}<[[]()](<>())>)(([()<>][[]<
<<[<{[<<[<<(<<()>{()<>}>{[[]<>]<<>[]>})>{<<{<>()}(()<>)><(()<>)<()<>>>>{[([]())([]<>)][[{}()]
({<{<[<{{([[[{[]()}[[]<>]]][<<{}{}><()()>>[({}[])(<>{})]]>)[{(((()<>)([][]))){<<[]>(()())>}}((
[<{<<({(((<<<[()<>][()<>]>>[(<[][]>[[]<>])]>){{[((()<>)<{}<>>)[[<>[]]((){})]]][([{{}()}[[]]](<<>
<{{{[<(([([[<({}<>)[<><>]>([[][]](()))]]]])(<{([<[()<>][<>[]]>{[<>{}]([]<>)}](<{(){}}<()()>><<()()>[[]{}]
{{[({([{{({<((()<>){()[]})>[[{<>[]}]{[{}<>]{<>()}}]}<<<<[]>[{}[]]>{({}<>)[()<>]}>(<[<>()]{<>[]}>)>)
<[<(([{{(<([(([]<>)([]()))<(<>{}){{}<>}>][[<[]{}>{<>{}}]{<<>{}>}])([[<{}{}>(<>{})]<[[]()>>]<([(){}]([]()))
<[{(([<{{<{{<[()()]><(<>())>}}<{[<()()>]<[()[]][()[]]>}<([()[]]({}<>)){{<>[]}[<>()]}>>}}[(<<[([]()){{}
<{{[<<[[<([<<[()[]][()<>]>(<<>[]>[()<>])>])([{{[[]{}]<{}[]>}({<>[]})}]<<[(<>[])([][])][<<>()>]>>)><[
<<{{[({{{{<[<{[]<>}({}{})>{(<>())(<>[])}]({([][])(()<>)})>(<{([][])<[][]>}([[]]<()()>]>{{[()
[[{[{([[[{[{[([]<>){<>{}}](<<>()>)}{[<[]{}>[{}[]]]}]}[([([<>()])]<<[<>][[]<>]>{({})[<>{}]}>]<({
(([[{(<{{<([[(<>){<>()}]<[[]{}]<()<>>>]{({{}[]}<<>{}>)<([]{})[(){}]>})>({<(({}[])<()<>>)>}((<<()()>
<{(<<(<[<{{<<{()[]}{{}()}><<<>][[]{}]>>([([]<>)<<>{}>])}[{[<{}[]>]<<{}[]>{<>[]}>}]}[{{{<<><>>{()<>}}}
(({(<<((<{<(<<()>([]())>){({[][]}<[]()>){<<>{}>([])}}>{(<([][])<<>()>>)}}{[<((<><>)([]<>)){(()[])}>{<<[
{[{[([((((([([[]{}][()[]])<(()())[<>[]]>]))[([[<<>()>{{}<>}](({}<>))][[({}[])]])]))[{({[[<<><>>][(()<>)<<>()
<<(<([([({(({{{}()}(())}[(<>[])({}<>)])([([][]){[]<>}]{{[][]}[(){}]})){(<<<>()>[[][]]><<[][]>
[<<{{{({[<{[<[[]{}]<{}<>>>[[[]<>][<>{}]]]<{{<>{})({}[])}>}([{<<>()><{}()>}]([{{}[]}{[]<>}][(<>())[[]{}]]))
[([({{[<{<<((([]{})({}<>))[<<>[]><[]{}>])><<<((){})[()()]>>[{<{}{}><<>[]>}<<<>><<>{}>>]>>[{(
[({{(<{<([({[<<><>>({}[])]({<>()}<{}[]>]})])>}><([<<<[{<<><>>[[][]]}<[{}[]]<<><>>>]([{()<>
<{<<[((<{<{<<{[]<>}{[][]}>{[{}[]][<>{}]}>[{{[]<>}<<>[]>}<[<>()][()<>]>]}(<{(<>[])(<>())}>{{(()[]
[[[{({<(({[{([<>{}]{<>})(([]{})(()[]))}]}<[((<<>{}}[<><>])<<{}[]>>)(<<(){}>(()<>)>[<()()>([]<>)])]({([{}[]]
{{<[<[({[([[{[{}{}][[]()]}{(()())}]{[<()()>{{}<>}]({[]<>}[()()])}])]{[<{[<()[]>]}({[<>{}][(){}]}[<[]<>>[()[]
{[{{{([(<([(<{()}[{}()]>{{<>{}}{(){}}})[<<<>{}><<><>>>]])<([(<{}[]>)][(<()[]}[()<>])[(()())<{}[]>]])[(
<({([{<<<<{[([<>{}]<{}{}>)(<[]<>>[()<>])]}>><[{[<[[]{}]{(){}}>[<()[]][<>]]](<[[]()]([]{})>)}({((<><>))<{<><>}
[({<[{<<(([<({[]<>}<()()>)[{<>()}]>>{[[<<><>>{[][]}]([{}()](<>[]))]}))>>}]({(<{[(<<(()[])[{}<>]>
<([<<(({<{[[[{{}<>}([])]<{<><>}>]<<({}<>)<<><>>><<[]<>]<{}[]>>>]}{[{<{()}{()}>(<[]{}>{<>[]})}[(<<><>>{<>()})[
({<{[(<<[<{([{<>{}}{<>()}]{{[][]}[[]<>]})}{({(<>())(())}<{()()}<()()>>){<({}{})<{}{}}>[({}<>
{{<<<[({({[{<({}())({}())>}{<{{}[]}[[]()]><(()<>)(<><>)>})})})<<<{{(<{()<>}{(){}}><{()()}([]<>)>)([(
{{[{[[[({<{{[(<>)[()[]]]}<<{<>[]}[<>[]]><[<>]<{}>>>}({[{[][]}([]())]}[[{{}{}}[()[]]]<[(){}]>])
<{[([{((<[[<<<<><>><{}()>>[<{}()><<>[]>]>[<<{}()>{{}<>}><<{}<>>{[][]}>]]<[{({}[])}{[{}<>][<>
[{<(([<{{{([{{<>{}}{(){}}}<[{}()]<<>()>)][{[{}{}]({}<>)}{(<>[])<()[]>}])<<[{{}[]}[[][]]]{[{}<>]<
({{[[[{<([({[<()<>><{}[]>][([]{})({}())]}<[<[]{}>]{{[]{}}({}{})}>)<{[[[]()]<[]{}>][[<>[]]{[][]}]}([{[][]}<[]
<((({{{[{(<{<[()[]]<{}[]>>[{(){}}({}[])]}<<([])<(){}>>(([]<>)[<>[]])>>)}<([({(()<>)<()<>>}{<[][]>[[][
[<({<[{[[{{(<[<>{}]({}())>({{}{}}{[]()})){[(<>()){()()}]{([][])[<>{}]}}}([<[[]()]([])>([[]()]<<><>>)]([<<>()>
[<{[(<(<<({<{<{}()>[<>{}]}{[{}{}]{<>[]}}>{[{<><>}<[]{}>](({}<>)([]{}))}}([{<<>{})({}<>)}<{{}<>}{{}{}}>])){<[
[{<{{{{{{{<<({[][]}([]<>))[<()[]><<>[]>]><<(<><>)[{}[]]>((()[]))>>{{({<>[]}<<>()>)[[<>()]<<>{}>]}}}[<(
<(<[((<{[[[<[[<><>]([]())]{{()[]}([][])}>]](([([{}]{{}()}){[<><>]<[]<>>}]<((<>{}){{}<>})<{
([[(((((<<{({<{}[]>((){})}(<{}<>>[()[]])){<<{}()>(<>())>[({}[])<<>()>]}}>(<(<(()[])[[]()]>[<(
{({{{{<[<<(<<[[]()]{{}[]}>[<[]<>>[[]<>]]>(<<()<>>(<>())><([]<>)>})><<<({{}{}}<[]()>){{()()}[{}[]]}
[<[[({({[{[(({<>[]}{()<>})[({}())[[]()]]){[{()<>}(<>)](<<>>)}]}][(([[<{}()>]{([]{}){[]}}])[
(<{[<({[<<{{(<{}<>><<>{}>)[[{}()][{}[]]]}<[{[][]}](([]{})(()[]))>}<([{<><>}[[][]]][<[]{}><[]{}>]}(<{()
[(<(<([[[[<(({()[]}<<>()>)[[()<>]({}<>)])>]]]{(([(<(()<>)[[]<>]>{[<>()]{<>{}}})({<()()>}<<<><>>
<(<[<{([<[{{<{()}[<><>]>(<()[]>(<><>))}[[<{}{}><[][]>]<((){})[<><>]>]}(((<[][]><[]{}>)[[{}{}]{()[]}]){[(()()
({<[<[{({{{(({{}<>}{<>[]})<{()[]}<{}{}>>)[((()<>)<<><>>]<[{}]<<><>>>]}[({[{}()][[]{}]}[[<><
[[(({[{[[<{[{({}())<<>[]>}{{{}[]}<(){}>}]{<[(){}]<{}<>]>}}([{{()[]}<<>()>}([()[]]<[]>)][<([]<>)[[]{
[<(<((({[[{([{<>[]}<<>()>]([[]()]))}]([(<{()[]>[{}{}]><{()()}<()[]>>)](<<[()[]]{<><>}>[<()[]>(<
({({[<{<(<(([[[]{}]({}<>)])<{{{}{}}<{}{}>}{{{}<>}[{}()]}>)((<[<><>][(){}]><[{}<>](()())>)({{<
[{({({(<(<{({<<>[]><{}>}<<{}{}>{<>{}}>)(<[{}[]]([]())>{<(){}>{<><>}})}<{[{<>}{[]{}}]([{}{}]<<><>>)
({{[({[<[<({[[[]{}](()())])<{<(){}>[()()]}<[<>()][(){}]>>)>({<<[[][]]<<><>>><<()[]>>><<<()[]>{[]()}
({({{[[{((([{({}()){<>()}}]([[<>{}]<()[]>][[()]<[]<>>]))))}]]}[<({[<<{<[<>()]<<>{}>}<<{}{}>[<>()]>}([(()
[[{([{(<[{((({(){}])(<{}<>>[<>{}]))<<(<>())<[]{}>><(()[])<()[]>>>)([({<><>}<<>{}>)({<>{}}(<>{}))])}]>(
([<<[<[[({{[[({}{}){()<>]]{[[]<>]}]({<()()>}{<[][]>})}})]({<{({[{}[]][()[]]}[{{}{}}{(){}}])<([[][]]{<>{}})
[{[{[({<([{[({()<>}{{}{}})<<[]{}>>][({{}[]}[()[]])[({}{})<<>{}>]]}]{{{<([]())(<>[])><<[]()>({}{})>}}((<<(){}>
(<(<{({{<{[{([{}()]{(){}})[[<>{}](<><>)]})(<[{{}()}]<[[][]]{()<>}>><{{[]<>}{<>()}}{<{}<>>[[]<>]}>)}>}[[<(

144
2021/day11/day11.erl Normal file
View File

@@ -0,0 +1,144 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day3 solve
%%
-module(day11).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
-compile([export_all]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' -> Input;
Line -> read_input(IO, Input ++ [Line])
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> [ X - $0 || X <- Line, [X] /= "\n"]
end.
solution1(Input) ->
Coords = coords(Input),
Data = [ {X, init_data(X, Input)} || X <- Coords],
put(flash, 0),
sn1_step(Data, 0),
get(flash).
sn1_step(Data, 100) -> Data;
sn1_step(Data, Count) ->
Step1 = step1(Data),
Step2 = step2(Step1),
sn1_step(Step2, Count + 1).
solution2(Input) ->
Coords = coords(Input),
Data = [ {X, init_data(X, Input)} || X <- Coords],
put(flash, 0),
sn2_step(Data, 0).
sn2_step(Data, Count) ->
Step1 = step1(Data),
Step2 = step2(Step1),
case all_flash(Step2) of
'true'-> Count + 1;
'false' -> sn2_step(Step2, Count + 1)
end.
all_flash(Data) ->
lists:all(fun({_,V}) -> V == 0 end, Data).
coords([H|_] = Table) ->
X = length(H),
Y = length(Table),
lists:flatten([[{A,B} || A <- lists:seq(1,X)] || B <- lists:seq(1,Y)]).
step1(Data) ->
lists:map(fun({C, X}) -> case X of 9 -> incr_flash(), {C, 'flash'}; _ -> {C, X + 1} end end, Data).
step2(Data) ->
case is_flash(Data) of
'true' -> flash(Data);
'false' -> clear_flashed(Data)
end.
is_flash(Data) ->
lists:any(fun({_, X}) -> X == 'flash' end, Data).
flash(Data) ->
NewData = flash(Data, Data),
step2(flashed(Data, NewData)).
flash([], Acc) -> Acc;
flash([{{X,Y},V}|T], Acc) when V == 'flash' ->
flash(T, increase_neighbors({X,Y}, Acc));
flash([_|T], Acc) -> flash(T, Acc).
clear_flashed(Data) ->
clear_flashed(Data, []).
clear_flashed([], Acc) -> lists:reverse(Acc);
clear_flashed([{C, V}|T], Acc) when V == 'flashed' ->
clear_flashed(T, [{C, 0}|Acc]);
clear_flashed([H|T], Acc) ->
clear_flashed(T, [H|Acc]).
flashed(OldData, NewData) ->
lists:zipwith(fun({C, V}, N) -> case V of 'flash' -> {C, 'flashed'}; _ -> N end end, OldData, NewData).
increase_neighbors({X,Y}, Data) ->
Neigbours = [{X+1,Y},{X-1,Y},{X,Y+1},{X,Y-1},{X+1,Y+1},{X+1,Y-1},{X-1,Y+1},{X-1,Y-1}],
increase_neighbor(Neigbours, Data).
increase_neighbor([], Data) -> Data;
increase_neighbor([{X,Y}|T], Data) when X < 1; Y < 1; X > 10; Y > 10 -> io:format("ignore: ~p~n",[{X,Y}]), increase_neighbor(T,Data);
increase_neighbor([H|T], Data) ->
case get_value(H, Data) of
'flash' -> increase_neighbor(T, Data);
'flashed' -> increase_neighbor(T, Data);
9 -> incr_flash(), increase_neighbor(T, set_value(H, Data, 'flash'));
V -> increase_neighbor(T, set_value(H, Data, V + 1))
end.
get_value(C, Table) ->
{C, V} = lists:keyfind(C, 1, Table),
V.
set_value(C, Table, V) ->
lists:keyreplace(C, 1, Table, {C, V}).
init_data({X,Y}, Table) ->
lists:nth(X, lists:nth(Y, Table)).
incr_flash() ->
put(flash, get(flash) + 1).

10
2021/day11/input.txt Normal file
View File

@@ -0,0 +1,10 @@
6617113584
6544218638
5457331488
1135675587
1221353216
1811124378
1387864368
4427637262
6778645486
3682146745

100
2021/day12/day12.erl Normal file
View File

@@ -0,0 +1,100 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day12).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' -> Input;
Line -> read_input(IO, Input ++ [Line])
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> parse_line(Line)
end.
parse_line(Line) ->
[Cave1, Cave2] = string:tokens(Line, "-\n"),
{Cave1, Cave2}.
solution1(Caves) ->
CaveMap = insert(Caves),
count_paths("start", #{}, 'some', CaveMap).
solution2(Caves) ->
CaveMap = insert(Caves),
count_paths("start", #{}, 'none', CaveMap).
insert(Caves) -> insert(Caves, maps:new()).
insert([], CaveMap) -> CaveMap;
insert([{Cave1, Cave2}|Rest], CaveMap) ->
NewMap =
case maps:is_key(Cave1, CaveMap) of
'false' -> maps:put(Cave1, [Cave2], CaveMap);
'true' -> maps:put(Cave1, [Cave2|maps:get(Cave1, CaveMap)], CaveMap)
end,
NewMap2 =
case maps:is_key(Cave2, NewMap) of
'false' -> maps:put(Cave2, [Cave1], NewMap);
'true' -> maps:put(Cave2, [Cave1|maps:get(Cave2, CaveMap)], NewMap)
end,
insert(Rest, NewMap2).
visit_vertex(Name, Visited, VisitedTwice, Graph) ->
NewVisited =
case hd(Name) >= $a of
true -> Visited#{Name => true};
false -> Visited
end,
#{Name := Neighbours} = Graph,
lists:sum([count_paths(N, NewVisited, VisitedTwice, Graph) || N <- Neighbours]).
count_paths("end", _, _, _) ->
% We have found an entire path through the graph.
1;
count_paths("start", Visited, _, _) when map_size(Visited) > 0 ->
% We cannot visit the start cave more than once.
0;
count_paths(Name, Visited, VisitedTwice, Graph) when hd(Name) >= $a andalso is_map_key(Name, Visited) ->
% We can visit only one small cave twice.
case VisitedTwice of
'none' -> visit_vertex(Name, Visited, Name, Graph);
_ -> 0
end;
count_paths(Name, Visited, VisitedTwice, Graph) ->
% Small caves can only be visited once, but large caves may be visited any
% number of times.
visit_vertex(Name, Visited, VisitedTwice, Graph).

21
2021/day12/input.txt Normal file
View File

@@ -0,0 +1,21 @@
TR-start
xx-JT
xx-TR
hc-dd
ab-JT
hc-end
dd-JT
ab-dd
TR-ab
vh-xx
hc-JT
TR-vh
xx-start
hc-ME
vh-dd
JT-bm
end-ab
dd-xx
end-TR
hc-TR
start-vh

12
2021/day13/commmands.txt Normal file
View File

@@ -0,0 +1,12 @@
fold along x=655
fold along y=447
fold along x=327
fold along y=223
fold along x=163
fold along y=111
fold along x=81
fold along y=55
fold along x=40
fold along y=27
fold along y=13
fold along y=6

88
2021/day13/day13.erl Normal file
View File

@@ -0,0 +1,88 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day13).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' -> Input;
Line -> read_input(IO, Input ++ [Line])
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> parse_line(Line)
end.
parse_line(Line) ->
Points = string:tokens(Line, " ,\n"),
[X, Y] = [list_to_integer(X) || X <- Points],
{X, Y}.
solution1(Input) ->
io:format("Input ~p~n", [Input]),
F1 = fold(x, 655, Input),
length(F1).
solution2(Input) ->
I = fold(y,6,fold(y,13,fold(y,27,fold(x,40,fold(y,55,fold(x,81,fold(y,111,fold(x,163,fold(y,223,fold(x,327,fold(y,447,fold(x,655, Input)))))))))))),
MaxX = lists:foldl(fun({T, _}, Acc) -> case T > Acc of 'true' -> T; _ -> Acc end end, 0, I),
MaxY = lists:foldl(fun({_, T}, Acc) -> case T > Acc of 'true' -> T; _ -> Acc end end, 0, I),
Coords = [{X,Y} || Y <- lists:seq(0,MaxY), X <- lists:seq(0,MaxX)],
output(Coords, I, MaxX).
output([], I, MaxX) -> ok;
output([{X,Y}|Rest], I, MaxX) ->
case lists:member({X,Y}, I) of
'true' -> io:format("#");
'false' -> io:format(" ")
end,
case X == MaxX of
'true' -> io:format("~n");
'false' -> ok
end,
output(Rest, I, MaxX).
fold(y, N, Input) ->
L1 = [ {X, (Y * -1) + N} || {X, Y} <- Input],
{Sat, NotSat} = lists:partition(fun({_,Y}) -> Y < 0 end, L1),
Sat2 = [{X, Y * -1}|| {X, Y} <- Sat],
L2 = lists:usort(NotSat ++ Sat2),
[ {X, (Y - N) * -1} || {X,Y} <- L2];
fold(x, N, Input) ->
L1 = [ {(X * -1) + N, Y} || {X, Y} <- Input],
{Sat, NotSat} = lists:partition(fun({X,_}) -> X < 0 end, L1),
Sat2 = [{X * -1, Y}|| {X, Y} <- Sat],
L2 = lists:usort(NotSat ++ Sat2),
[ {(X - N) * -1, Y} || {X,Y} <- L2].

907
2021/day13/input.txt Normal file
View File

@@ -0,0 +1,907 @@
949,224
398,211
402,700
900,890
1197,304
333,809
681,705
769,864
975,465
639,523
445,313
912,99
502,894
703,343
572,598
1232,759
277,640
700,761
919,429
678,141
1054,795
934,750
760,73
268,627
336,859
1096,135
646,176
55,404
932,872
1168,338
569,842
904,863
647,168
509,606
42,525
607,343
947,861
1002,537
1230,368
186,274
363,690
1073,663
868,312
646,718
385,210
961,660
1277,0
470,228
127,705
894,89
500,704
994,361
87,582
985,378
557,607
75,872
1068,647
686,138
358,84
957,415
325,516
711,169
1287,252
619,242
329,516
821,714
514,844
632,358
549,252
442,302
619,652
5,809
276,530
15,75
653,266
632,536
731,648
237,231
1266,19
966,205
360,37
522,3
1255,725
833,840
898,728
805,162
294,261
1250,833
403,151
1288,564
7,465
350,274
1133,478
408,526
1079,493
310,205
420,507
1310,596
639,824
328,257
30,417
845,511
196,683
1002,201
719,96
15,457
276,812
760,43
979,95
319,429
661,429
654,298
584,421
372,355
351,252
430,453
113,304
701,772
107,628
549,700
629,551
492,851
427,101
278,666
246,856
194,82
417,439
433,712
425,229
308,89
1275,652
1193,316
482,190
955,245
465,511
688,37
788,525
92,235
644,417
1247,877
25,327
1255,404
502,0
960,760
431,570
741,702
728,606
1086,453
1198,694
181,208
401,327
959,194
729,863
1295,819
7,271
263,456
132,868
318,849
242,726
1133,392
631,294
410,302
654,582
649,465
622,709
805,284
1300,533
731,22
579,470
177,392
1079,849
252,397
898,871
634,618
427,653
433,630
1228,851
686,865
1079,32
105,252
372,761
15,864
644,222
1134,122
572,851
1303,429
991,726
304,620
962,446
899,586
667,44
786,228
719,93
411,742
1031,110
33,0
646,483
482,288
885,47
1250,609
632,330
594,833
1178,868
68,542
482,480
818,851
278,592
898,726
141,660
360,857
515,651
30,532
865,540
1168,500
1022,525
80,526
975,429
401,159
1256,523
1300,809
410,598
537,540
482,606
25,551
229,709
1096,555
952,756
23,70
584,38
810,288
495,416
278,358
900,43
550,43
895,582
1221,213
1205,252
1180,732
524,3
850,641
1054,421
865,581
376,37
751,233
72,64
969,717
1032,666
1170,50
112,200
279,784
818,43
810,704
972,222
738,296
1285,791
162,403
333,85
1168,786
1161,51
840,498
3,351
966,644
522,891
666,222
751,592
842,50
480,446
885,847
7,623
137,638
1208,703
668,716
477,416
1307,351
981,555
117,578
430,254
1280,417
428,809
743,312
634,620
1225,724
10,869
631,700
87,312
1237,689
388,285
358,810
1111,894
1133,499
113,752
666,670
1198,200
196,451
1079,302
1098,521
987,95
1295,523
1275,591
1131,855
440,583
1171,691
955,201
112,135
291,861
50,588
1032,358
678,330
689,446
30,815
411,152
505,610
251,246
1052,430
427,793
1173,383
1143,431
607,791
189,32
1057,7
1307,543
1173,374
430,677
313,289
624,756
1048,429
1205,70
231,862
947,690
966,250
1220,851
1253,210
269,427
850,701
1034,812
748,211
868,73
279,110
664,624
971,506
1009,327
214,555
80,632
1136,648
1133,854
557,5
997,605
1079,45
174,648
159,351
341,717
934,218
549,194
1079,862
774,467
423,177
1126,50
1039,255
154,717
1256,75
1006,620
1136,757
73,831
65,441
95,415
0,596
455,33
460,641
528,427
15,437
627,201
142,786
714,386
922,144
97,606
455,57
1084,309
840,725
840,396
428,533
679,600
607,103
965,662
674,542
199,894
691,425
728,382
636,352
251,85
149,527
181,686
253,119
1124,172
1059,757
25,383
15,371
540,347
793,638
1305,809
1221,231
681,479
870,107
818,512
427,457
174,757
559,233
1263,343
11,626
174,246
44,875
211,453
1000,250
160,714
802,698
181,9
883,101
994,415
1262,106
715,548
934,228
378,82
262,429
23,252
495,351
885,665
656,582
1111,837
609,324
704,788
994,733
348,620
877,642
971,850
1242,430
513,234
341,157
221,476
443,441
159,767
907,295
1287,824
622,485
1297,659
863,455
1266,133
596,386
1042,85
566,805
711,404
1019,204
904,479
1310,760
1164,877
1054,235
986,392
952,84
468,50
231,302
1159,716
979,799
1178,815
686,29
336,35
796,844
1059,375
445,130
644,533
865,537
542,86
1130,129
1196,750
162,627
95,849
339,492
1173,256
489,597
972,415
877,630
885,717
726,38
1288,834
934,302
870,364
711,649
773,130
410,43
1133,395
1091,714
900,296
258,240
1144,852
982,596
1129,885
960,386
786,443
378,872
1130,765
348,274
1230,526
415,312
729,31
1180,162
793,704
448,525
149,630
816,180
555,121
974,45
1143,687
214,87
48,788
499,367
412,728
796,396
1260,812
679,376
65,383
591,96
512,52
420,828
589,889
435,649
149,591
1114,666
231,849
1151,351
64,205
1240,856
970,436
145,457
579,648
50,193
1168,108
22,834
885,495
1218,235
70,347
1293,353
632,834
278,857
1213,453
971,268
514,396
957,479
619,649
490,736
1302,849
1086,640
890,380
619,591
671,70
159,655
10,421
862,369
441,192
818,64
1183,705
679,824
1196,185
114,302
633,660
1228,40
435,245
256,235
651,351
410,296
947,21
992,859
89,213
482,177
112,459
999,630
774,203
763,628
985,516
1261,640
828,187
562,211
923,10
87,309
1146,736
512,500
305,234
1295,371
647,271
1096,584
508,353
1297,812
972,448
688,485
1081,512
104,403
401,703
1073,231
311,883
833,812
1032,857
239,582
503,716
343,152
664,176
1168,672
167,207
401,775
1116,474
398,683
331,799
952,532
1228,647
1287,600
908,642
402,642
355,245
335,465
788,891
678,536
8,493
318,859
1235,872
177,873
748,659
242,168
796,641
440,331
162,491
1171,203
477,840
350,162
524,666
591,877
22,330
962,274
629,567
977,809
828,480
291,413
1211,569
1193,764
468,274
805,591
395,45
589,511
1299,388
883,437
1302,483
1097,702
868,821
423,401
972,446
30,79
376,218
899,742
339,44
721,63
67,54
795,651
353,415
805,723
246,247
261,5
1047,456
706,82
344,644
1170,760
984,596
493,297
253,735
349,234
155,840
349,660
736,695
994,670
199,837
221,712
423,65
1083,101
386,732
346,245
1133,873
1121,63
1255,714
880,441
514,641
1299,836
1108,33
1278,400
241,500
788,79
1056,569
810,190
584,856
1043,548
1285,103
1288,249
1004,274
1034,530
244,890
1192,691
401,31
155,591
72,736
351,642
487,416
934,37
319,255
1136,137
442,73
169,170
1295,75
862,525
388,526
831,58
1119,649
411,84
902,247
253,7
840,617
1000,196
821,138
1000,205
710,819
13,235
914,654
1161,527
1262,261
492,830
455,705
622,185
1047,793
140,760
1275,649
316,733
345,662
267,884
1141,618
1299,58
440,107
991,175
852,451
23,824
375,110
468,172
541,590
1001,801
773,481
442,373
788,369
500,288
960,610
894,252
224,631
1111,189
1059,533
1029,716
114,709
768,883
541,752
1076,514
1277,894
1133,306
202,861
798,52
845,383
647,726
833,416
584,473
841,234
1245,5
30,756
818,267
885,512
572,488
417,614
1155,840
774,691
149,51
786,732
10,701
689,33
440,364
681,63
643,44
294,633
786,666
411,586
256,795
793,714
32,494
768,135
378,530
196,228
164,736
822,490
495,767
105,264
1073,679
445,242
719,877
146,698
1262,358
736,526
1237,831
965,93
177,588
877,712
75,22
887,65
915,849
1016,261
22,141
227,457
540,99
8,411
701,570
629,343
261,453
154,707
117,255
72,830
691,652
751,302
447,455
890,507
209,354
1238,736
338,448
574,526
1000,698
324,502
1066,4
44,19
632,60
768,459
25,567
1034,364
949,670
231,829
924,610
166,42
703,103
738,851
212,521
898,168
664,718
683,201
358,756
45,527
1235,22
666,533
686,756
132,369
1252,715
1016,270
828,288
226,309
358,138
1066,247
960,162
517,399
401,607
301,327
8,483
482,704
882,585
744,805
406,863
1148,627
44,133
1253,684
1129,9
1193,191
815,351
738,406
388,592
515,243
155,303
447,439
917,677
1041,609
10,361
137,383
858,861
1012,441
1173,404
1041,427
137,520
442,750
166,852
147,263
835,396
912,211
209,481
879,570
959,600
495,655
1119,21
253,241
631,642
492,512
719,798
344,205
599,245
997,289
703,791
1124,274
1071,582
909,607

168
2021/day14/day14.erl Normal file
View File

@@ -0,0 +1,168 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day14).
-export ([solve/0, solve/1, solve/2]).
-export ([expand_pair/4, read_input/0]).
-compile ([export_all]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' -> make_map(Input);
Line -> read_input(IO, Input ++ [Line])
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> parse_line(Line)
end.
parse_line(Line) ->
string:tokens(Line, " ->\n").
make_map(Input) -> make_map(Input, maps:new()).
make_map([], Map) -> Map;
make_map([[P, [Res]]|Rest], Map) ->
NewMap = maps:put(P, Res, Map),
make_map(Rest, NewMap).
solution1(PolyMap) ->
Ets = ets:new(polymer, [set, public, named_table]),
Polymer = "FNFPPNKPPHSOKFFHOFOC",
init_counters(Ets, PolyMap),
io:format("~p~n", [ets:tab2list(Ets)]),
Pairs = make_pairs(Polymer, []),
io:format("expand pairs: ~p~n",[Pairs]),
count_pairs(Pairs, Ets),
[count_elems(Pair, Ets, 1) || Pair <- Pairs],
loop(Ets, PolyMap, 10),
get_max_elm(Ets) - get_min_elm(Ets).
solution2(PolyMap) ->
Ets = ets:new(polymer, [set, public, named_table]),
Polymer = "FNFPPNKPPHSOKFFHOFOC",
init_counters(Ets, PolyMap),
io:format("~p~n", [ets:tab2list(Ets)]),
Pairs = make_pairs(Polymer, []),
io:format("expand pairs: ~p~n",[Pairs]),
count_pairs(Pairs, Ets),
[count_elems(Pair, Ets, 1) || Pair <- Pairs],
loop(Ets, PolyMap, 40),
get_max_elm(Ets) - get_min_elm(Ets).
get_max_elm(Ets) ->
Ents = ets:tab2list(Ets),
{V, _} = lists:max([{V,X} || {{elem, X},V} <- Ents]),
V.
get_min_elm(Ets) ->
Ents = ets:tab2list(Ets),
{V, _} = lists:min([{V,X} || {{elem, X},V} <- Ents]),
V.
loop(Ets, _PolyMap, 0) -> ets:tab2list(Ets);
loop(Ets, PolyMap, Count) ->
Counters = get_counters(Ets),
[update_ets(Ets, PolyMap, Counter) || Counter <- Counters],
loop(Ets, PolyMap, Count - 1).
init_counters(Ets, PolyMap) ->
maps:foreach(fun([X,Y] = Key, _Value) -> ets:insert(Ets, {{elem,X},0}), ets:insert(Ets, {{elem,Y},0}), ets:insert(Ets, {{pair, Key}, 0}) end, PolyMap).
update_ets(_Ets, _PolyMap, {_Key, 0}) -> ok;
update_ets(Ets, PolyMap, {[X,Y] = Key, Value}) ->
V = maps:get(Key, PolyMap),
count_pair([X,V],Ets, Value),
count_pair([V,Y],Ets, Value),
count_pair(Key,Ets, -1 * Value),
count_elem(V, Ets, Value).
get_counters(Ets) ->
Ents = ets:tab2list(Ets),
[{X,V} || {{pair, X},V} <- Ents].
make_pairs([_], Pairs) -> Pairs;
make_pairs([X,Y|Rest], Pairs) -> make_pairs([Y|Rest], [[X,Y]|Pairs]).
count_pairs([], _Ets) -> ok;
count_pairs([P|Rest], Ets) ->
count_pair(P, Ets, 1),
count_pairs(Rest, Ets).
count_pair(P, Ets, V) ->
ets:update_counter(Ets, {pair,P}, {2,V}).
count_elems([X,Y], Ets, V) ->
count_elem(X, Ets, V),
count_elem(Y, Ets, V).
count_elem(Elem, Ets, V) ->
ets:update_counter(Ets, {elem,Elem}, {2,V}).
expand_pair(_P, _PolyMap, _Ets, 0) -> ok;
expand_pair([P1,P2], PolyMap, Ets, Count) when is_list(P1), is_list(P2) ->
expand_pair(P1,PolyMap, Ets, Count - 1);
expand_pair([X,Y] = P, PolyMap, Ets, Count) ->
[V] = maps:get(P, PolyMap),
ets:update_counter(Ets, [V], {2,1}),
[expand_pair(Pa, PolyMap, Ets, Count) || Pa <- [[X,V]] ++ [[V,Y]]].
pairs([], _PolyMap, Pairs, _Ets, 0) ->
Pairs;
pairs([], PolyMap, Pairs, Ets, Count) ->
io:format("new pairs: ~p~n", [Pairs]),
pairs(Pairs, PolyMap, [], Ets, Count - 1);
pairs([[X,Y] = P|Rest], PolyMap, Pairs, Ets, Count) ->
[V] = maps:get(P, PolyMap),
ets:update_counter(Ets, [V], {2,1}),
pairs(Rest, PolyMap, Pairs ++ [[X,V]] ++ [[V,Y]], Ets, Count).
count(Polymer) ->
Ps = lists:usort(Polymer),
[ {count_p(P, Polymer), [P]} || P <- Ps].
count_p(P, Polymer) ->
lists:foldl(fun(X, Acc) -> case X == P of 'true' -> Acc + 1; _ -> Acc end end, 0, Polymer).
steps(Polymer, _PolyMap, 0) -> Polymer;
steps(Polymer, PolyMap, Count) ->
Insert = get_insert_list(Polymer, PolyMap),
steps(lists:flatten(lists:zipwith(fun(A, B) -> [A,B] end, Polymer, Insert)), PolyMap, Count - 1).
get_insert_list(Polymer, PolyMap) ->
get_insert_list(Polymer, PolyMap, []).
get_insert_list([_], _PolyMap, InsertList) -> lists:reverse([[]|InsertList]);
get_insert_list([P1,P2|Rest], PolyMap, InsertList) ->
V = maps:get([P1,P2], PolyMap),
get_insert_list([P2|Rest], PolyMap, [V|InsertList]).

100
2021/day14/input.txt Normal file
View File

@@ -0,0 +1,100 @@
VS -> B
SV -> C
PP -> N
NS -> N
BC -> N
PB -> F
BK -> P
NV -> V
KF -> C
KS -> C
PV -> N
NF -> S
PK -> F
SC -> F
KN -> K
PN -> K
OH -> F
PS -> P
FN -> O
OP -> B
FO -> C
HS -> F
VO -> C
OS -> B
PF -> V
SB -> V
KO -> O
SK -> N
KB -> F
KH -> C
CC -> B
CS -> C
OF -> C
FS -> B
FP -> H
VN -> O
NB -> N
BS -> H
PC -> H
OO -> F
BF -> O
HC -> P
BH -> S
NP -> P
FB -> C
CB -> H
BO -> C
NN -> V
SF -> N
FC -> F
KK -> C
CN -> N
BV -> F
FK -> C
CF -> F
VV -> B
VF -> S
CK -> C
OV -> P
NC -> N
SS -> F
NK -> V
HN -> O
ON -> P
FH -> O
OB -> H
SH -> H
NH -> V
FF -> B
HP -> B
PO -> P
HB -> H
CH -> N
SN -> P
HK -> P
FV -> H
SO -> O
VH -> V
BP -> V
CV -> P
KP -> K
VB -> N
HV -> K
SP -> N
HO -> P
CP -> H
VC -> N
CO -> S
BN -> H
NO -> B
HF -> O
VP -> K
KV -> H
KC -> F
HH -> C
BB -> K
VK -> P
OK -> C
OC -> C
PH -> H

166
2021/day15/day15.erl Normal file
View File

@@ -0,0 +1,166 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day3 solve
%%
-module(day15).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
-compile([export_all]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' -> Input;
Line -> read_input(IO, Input ++ [Line])
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> [ X - $0 || X <- Line, [X] /= "\n"]
end.
solution1(Input) ->
Graph = astar:graph(Input),
astar:search(Graph, {0, 0}, {99, 99}).
solution2(Input) ->
Graph = astar:graph(Input),
astar:search(Graph, {0, 0}, {499, 499}).
coords([H|_] = Table) ->
X = length(H),
Y = length(Table),
lists:flatten([[{A,B} || A <- lists:seq(1,X)] || B <- lists:seq(1,Y)]).
%init_data({X,Y}, Table) when X =< 100 andalso Y =< 100 ->
% lists:nth(X, lists:nth(Y, Table));
init_data({X,Y}, Table) ->
X1 =
case (X rem 100) of
0 -> 100;
A -> A
end,
Y1 =
case (Y rem 100) of
0 -> 100;
B -> B
end,
D = lists:nth(X1, lists:nth(Y1, Table)),
D1 = (D + ((X-1) div 100) + ((Y-1) div 100)) rem 9,
D2 =
case D1 of
0 -> 9;
C -> C
end,
%% io:format("~p -> ~p -> ~p~n", [{X,Y},{X1,Y1},{D,D2}]),
D2.
get_value({X,Y}, _Table) when X < 1; Y < 1; X > 500; Y > 500 -> 'invalid';
get_value({X,Y}, Table) ->
%% {C, V} = lists:keyfind(C, 1, Data),
%% V.
X1 =
case (X rem 100) of
0 -> 100;
A -> A
end,
Y1 =
case (Y rem 100) of
0 -> 100;
B -> B
end,
D = lists:nth(X1, lists:nth(Y1, Table)),
D1 = (D + ((X-1) div 100) + ((Y-1) div 100)) rem 9,
D2 =
case D1 of
0 -> 9;
C -> C
end,
%% io:format("~p -> ~p -> ~p~n", [{X,Y},{X1,Y1},{D,D2}]),
D2.
get_neighbors({X,Y}, Data) ->
Neigbours = [{X+1,Y},{X-1,Y},{X,Y+1},{X,Y-1}],
lists:filter(fun({_,E}) -> E /= 'invalid' end, [{C, get_value(C, Data)} || C <- Neigbours]).
build_graph(Coords, Data) ->
Graph = digraph:new(),
[digraph:add_vertex(Graph, C) || C <- Coords],
add_edges(Graph, Data, Coords).
make_coords(Coords, Loop) ->
[{X * Loop , Y * Loop} || {X,Y} <- Coords].
add_edges(Graph, Data, []) -> Graph;
add_edges(Graph, Data, [C|Rest]) ->
Neighbors = get_neighbors(C, Data),
[digraph:add_edge(Graph, C, X, V) || {X,V} <- Neighbors],
add_edges(Graph, Data, Rest).
dijkstra(Graph,Start_node_name) ->
Paths = dict:new(),
Unvisited = gb_sets:new(),
Unvisited_nodes = gb_sets:insert({0,Start_node_name,root},Unvisited),
Paths_updated = loop_through_nodes(Graph,Paths,Unvisited_nodes),
Paths_updated.
loop_through_nodes(Graph,Paths,Unvisited_nodes) ->
%% We need this condition to stop looping through the Unvisited nodes if it is empty
case gb_sets:is_empty(Unvisited_nodes) of
false ->
{{Current_weight,Current_name,Previous_node}, Unvisited_nodes_updated} = gb_sets:take_smallest(Unvisited_nodes),
case dict:is_key(Current_name,Paths) of
false ->
Paths_updated = dict:store(Current_name,{Previous_node,Current_weight},Paths),
Out_edges = digraph:out_edges(Graph,Current_name),
Unvisited_nodes_updated_2 = loop_through_edges(Graph,Out_edges,Paths_updated,Unvisited_nodes_updated,Current_weight),
loop_through_nodes(Graph,Paths_updated,Unvisited_nodes_updated_2);
true ->
loop_through_nodes(Graph,Paths,Unvisited_nodes_updated)
end;
true ->
Paths
end.
loop_through_edges(Graph,[],Paths,Unvisited_nodes,Current_weight) ->
Unvisited_nodes;
loop_through_edges(Graph,Edges,Paths,Unvisited_nodes,Current_weight) ->
[Current_edge|Rest_edges] = Edges,
{Current_edge,Current_node,Neighbour_node,Edge_weight} = digraph:edge(Graph,Current_edge),
case dict:is_key(Neighbour_node,Paths) of
false ->
Unvisited_nodes_updated = gb_sets:insert({Current_weight+Edge_weight,Neighbour_node,Current_node},Unvisited_nodes),
loop_through_edges(Graph,Rest_edges,Paths,Unvisited_nodes_updated,Current_weight);
true ->
loop_through_edges(Graph,Rest_edges,Paths,Unvisited_nodes,Current_weight)
end.

10
2021/day15/input.test Normal file
View File

@@ -0,0 +1,10 @@
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

100
2021/day15/input.txt Normal file
View File

@@ -0,0 +1,100 @@
6919598227838199913855119231126554696792992136695118448313191841922775524417825151216891429923213541
9837948917619787189935571922197132977185355128371858691255934311214863828372926993213996998139912118
9712819911516295249274925911896922213911165843181262181868447395254293349493421938929117229988638933
2476951876931175825312533142569137931721739713725799446851119715122115753938166842994429692731365577
3691396937919199853599315812613951281125263711868971256541511653441136543245312424117567151668999674
9294972854229491498411271321351998983919929272128753711198282397882539157287554149474186951291188213
1135428214811459993621216218435832221856114139399136248687314682119118892393618575918692412341569411
2929921158912711182518311271272489732271565129299979967487113812154971756485549913921162939114391994
5735115881479239217981525718898929221191614222551693914913779439617933359521941311111857328992347824
1324749414111313198724813856727141934339899617632138746132396173529281922347716643138211113979119317
3263594941789195782731783751728147419342113519153994137499331274281145119193372917298632271917871525
5479228191148416221119313323857818519129966727666999721348148577292717477671479138864751785262494159
7796954213299199792437719283546721912268871561199118418913219166184792518146351339119739123994185981
9629911853953289553892412891922852724926162185929113819714489559692115112436841311998121591212249911
2328397217172391111288411918123121111329776726473176893942742194591917882178943718116232271214679483
8741141538382111791315111111217879577863971972167912427154411516117129128518217911997124249159922143
1112181727959169888722989121578121121151936427211518922174664147225132238128294879313132991431289481
3747188357529199195611794131377544398823262189627134618228417441399177613261778632518726968681261168
2481629985392983495331692197839119129614185425229598989835611557184114492293122195456638299631886877
9147391211998228191112524911758739341511911573198239147932292971618769152596224169136819211191864121
5339661183639791381383226488915834355926218173956911914294222986717347842733923158213319912313779114
5243921116119511826295361157329514912924232445973183228141342999868999739939499749595714929628949881
1717822297616989111681634446964759597245148141299649442375393616675299961783422585213969182389813979
9223718278792883969179179831171225547156272493833129568562192687845341671181118782551343181335147329
2949471233325124981953749291711218145971445981149411417976569225114219698356762679863952545995247124
5813891161912133536215537881164329141679198579211417483995393171751229139515148551616451451699471962
9119929859113721717292587192359667211794197356859166313415988219993559879467128483625838972963998323
3895252513734165951664259349139491161549113632418318988572115611474628679584659333822336264442417161
1321331549981649293931789932115621285146921639311329258269531211367526813124695472815288349283153957
9422143143834321616229212182481242959489182311359152235561234325491988881677689362294958313949842287
2933761954489328463913592166236523429629187282841912685667184712699837199911744132197611144273159928
9154223693315635418997851912395881642814193154944229782123418943428143338499556944668943742669929821
7139199155869873192534891585999171659544292341128472349512234761139621941897245995229121715921128221
6121476298449615119179315279931141828241794783538353767662277163821548137442111115139516117411432154
2721461614458352311522959322145231893925321191149936561182971937587678991778119969116199785178211249
2487528361345231828917513711239639891526811157458691118988793531821939487349991122139681411829212122
8196784988189916854591415361246889241677468137989661782462692224114367584511836128916614929335987963
1428262253911238199139222239127987864548198367553991618567992112895712539846223268343291112612477881
4311151291221542315119195139919638851217471142313611628999671999898223151846129413511721416129385444
9223929419139196512617954741195118176182238863959367576997199569784421619179812898678142615193816961
3739957281196232999991168188488998188924191959881122119917862162473281529652914854499515417543148211
1169927398515215962874725929618422947517373431917599117392259119991783219193841762221551623313812112
1779521141994763421294833117197299835113728789243129423866794976144315588151693917287864658143995545
1159529199535138199525951914847892791117273918293125411365889356724137416981217218648382141469719663
1441364449664913814639642968343767978911851669683136385812815592539216123382714745918827782372785978
6318957234985141489118421889191571779911215699151212991414982923995699851169649235572492514461152781
7412156483212717161922514861277814977411642186522941564119197181981973336314373991372153411986995122
8897991112125241713878834617986429813629284553444782988769119316585492828131921114225955339181119115
7125797134691266318245163258271753447285747349147564189981125236198721219161583235846326176241283216
9525321171524757914385236188595897464111726911869954146959524288653932322972921175292381219559151292
3194414111319217199913317211199349199115439486152468914947617154512189983891512878579271819721897263
2491297212663627482329238221159172168981257191127613528687982291228575192766566677617649837761491989
1121617881229121329144711662747663125911994277165915132843328895684121414218596361211122111151522917
1162139629471896711146596151128354981329212568252314552651841992789919922154257915321939255343321996
8957911396911314198222223175527176652889162319834385792196891372879635461922911398421776885879797419
7322963541269954962232189653112791497811679512721118141634598993612942952829283995138219829599889896
1569981859182313594573978911399869123929216923327226499129485126875325886837652725166648848111998112
1288121399799771382611161143238967371619191852948117758798124866815182266412112994241352156467592687
9782613685639942816392876435869597448311475929186556881281992238479474821817198267189176811611345259
8297985291593249832442977492543196751332831189541119469439887728521432991519343374611228237815267521
4523444992382282221121212292493497226928544945515424945782491119898752191347166219638988944397819329
1194152129661126528417217522222823524312191626887412455713921129631844996221721711241299731957631929
1781148994623342655141379243991813598498169894269285915113668982571141718118953985931825748891192931
7833796895687353814141792216629798237329497911457911619894313529713318488672351472297297179132273189
9926932996114129354986957691171917584919694139244291829925819193912511541921314292341872312211981115
2226319158194127721821519888721116941969121519196797413362226317939127758283193749485693516626493951
3879811393834677889446991895193312948783741921874992118657173911651519457119526249932991374694221158
3978213824919914931146214425131552342993968699129184834945118939217714393215687129491255112693917989
2158824391323818951718191147228251313192392116229926329592191642213389282938125431317515594129855892
2451172759927711235717449878121951114556117149429163518191331765282139179182223474536877622144431352
2329714311789716815472699771921951445911563498316498268435161242192511134612561842937129461433486692
9129479162281325294261155111911112642211493851211871231213119335358891917149811789318556889897859341
3131721472764729312915112753898151151419189179139342154299471832731815961421341134532764798679814835
2217431514225191812445931219331361111219893144143242969585922236148982273386967781788171191337195416
1488382763322171811791496513637271436981329581196884183931387898461131393171817212859131421121588819
3919585217425217131651936339227398135223761428891742188679177616726195628645462878211341521251196219
1912957445827151319119291131211879781559926431573113691921911897931126976153352174896222328435211744
8323394392199159213314229519951213933192989466113182874918939393519799992338482811596611494443251292
9984916453772991188215712124613985265583131698589662217749113142971842194779131373528882783188719171
3913899817991926921213654613559196179434112197559381516917322458299126529795921728131229913442812145
5261955321792184295644715972199271318912197112951581231816254223846887618715212638468619569563671225
9917489994523968382612199487887184382861222418313922422473349162379172311619221982567741824957119114
9968113457218183711391471141522113356149799928981688713318973119319334121489381212791911983159993251
5249142618957491198134219822522879839256251251436381811362976119514169213884284373893443751176727125
2392796498327162272298169491815314812269718889131918964451489919132342117242821633487187151658118395
2249487296138221889993479576818554862713916146291439161279886228579321318212135384293111391332314217
8897514868449987835128195533898941832793897879991713214683219511961725984113517647629518912372993316
1222365183812221239236493126981329745983157171198118191517444419871498512716164392661878952149182524
4629547769226193136881192975138212468396429891334928131276933521182542469561849129314987648579679814
4749932538499119949794629935997432846571872718898849574149926819691699199813998733192839462193345835
9591811516417941413919173999998125925256922487211975464577143226941827972121634167224618848544866381
1199639929547111431756233793468711319694611716322117618413912797189249351723264174991798896126128931
9148618989823822894126381717841681947679515811112512179728652138551112276241388773414937399838169113
2116766171211521721492313143976327275198231584381986141259139981152183551411875421171911111231991471
9989831137686686132681193914183774211627118849879121936974812614974731981977534928127129133558532219
4741174282322196153167332691899826553963194736319492127196372119621459827218931141194633228429712327
8911816487224543464638721721725597897179615822212948111174159112193164482824873198414218689949816972
9435522297626213549132166931213828698726112979125659542928778787719513418334539737811551126112515196
7992595817832629549819319199131117919697522116215291142239947517891729618121315294175432217519225146
1211761163187331332248351681117516631676788555961569685827143481198812137751721875926238173159343258

179
2021/day16/day16.erl Normal file
View File

@@ -0,0 +1,179 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day16).
-compile(export_all).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
{ok, Line} = file:read_line(IO),
file:close(IO),
hexstr_to_bin(string:strip(Line, 'both', $\n)).
hexstr_to_bin(S) ->
hexstr_to_bin(S, []).
hexstr_to_bin([], Acc) ->
list_to_binary(lists:reverse(Acc));
hexstr_to_bin([X,Y|T], Acc) ->
{ok, [V], []} = io_lib:fread("~16u", [X,Y]),
hexstr_to_bin(T, [V | Acc]);
hexstr_to_bin([X|T], Acc) ->
{ok, [V], []} = io_lib:fread("~16u", lists:flatten([X,"0"])),
hexstr_to_bin(T, [V | Acc]).
bin_to_hexstr(Bin) ->
lists:flatten([io_lib:format("~2.16.0B", [X]) ||
X <- binary_to_list(Bin)]).
solution1(Input) ->
D = unpack(Input, [], not is_zero_bitstring(Input)),
io:format("Pkt: ~p~n", [D]),
count_versions(D, 0).
solution2(Input) ->
D = unpack(Input, [], not is_zero_bitstring(Input)),
io:format("Pkt: ~p~n", [D]),
calc(D).
calc({_,literal, V}) ->
V;
calc({_,Op, V}) ->
?MODULE:Op(V);
calc([{_,Op, V}]) ->
?MODULE:Op(V).
sum(Args) ->
io:format("sum: ~p~n", [Args]),
lists:foldl(fun(X, Acc) -> calc(X) + Acc end, 0, Args).
product(Args) ->
io:format("product: ~p~n", [Args]),
lists:foldl(fun(X, Acc) -> calc(X) * Acc end, 1, Args).
min(Args) ->
io:format("min: ~p~n", [Args]),
lists:min([calc(X) || X <- Args]).
max(Args) ->
io:format("max: ~p~n", [Args]),
lists:max([calc(X) || X <- Args]).
lt([A,B] = Args) ->
io:format("lt: ~p~n", [Args]),
case calc(A) < calc(B) of
'true' -> 1;
'false' -> 0
end.
gt([A,B] = Args) ->
io:format("gt: ~p~n", [Args]),
case calc(A) > calc(B) of
'true' -> 1;
'false' -> 0
end.
eq([A,B] = Args) ->
io:format("eq: ~p~n", [Args]),
case calc(A) == calc(B) of
'true' -> 1;
'false' -> 0
end.
count_versions([{Ver, Type, Rest}|T], Acc) ->
count_versions(T, count_versions(Rest, Acc) + Ver);
count_versions(_, Acc) ->
Acc.
is_zero_bitstring(BitString) ->
Size = erlang:bit_size(BitString),
<<0:Size>> =:= BitString.
unpack(Data, [], 'false') ->
Data;
unpack(Input, Acc, 'false') ->
Acc;
unpack(Input, Acc, 'true') ->
{{Ver, Type, Data}, Rest} = unpack(Input),
io:format("unpacking data: ~p~n", [Data]),
lists:reverse(unpack(Rest, Acc ++ [{Ver, Type, unpack(Data, [], is_bitstring(Data) andalso not is_zero_bitstring(Data))}], not is_zero_bitstring(Rest))).
%% Literal
unpack(<<V:3, 4:3, Rest/bitstring>>) ->
io:format("V:~p, T:literal~n", [V]),
unpack_literal(V, Rest);
%% Operator length type id 0
unpack(<<V:3, T:3, 0:1, Rest/bitstring>>) ->
io:format("V:~p, T:operator I:0~n", [V]),
unpack_operator_0(V, T, Rest);
%% Operator length type id 1
unpack(<<V:3, T:3, 1:1, Rest/bitstring>>) ->
io:format("V:~p, T:operator I:1~n", [V]),
unpack_operator_1(V, T, Rest).
unpack_literal(V, Pkt) ->
unpack_literal(V, Pkt, <<>>, 0).
unpack_literal(V, <<1:1, Value:4, Rest/bitstring>>, Acc, Count) ->
unpack_literal(V, Rest, <<Acc/bitstring, Value:4>>, Count + 1);
unpack_literal(V, <<0:1, Value:4, Rest/bitstring>>, Acc, Count) ->
L = <<Acc/bitstring, Value:4>>,
Size = (Count + 1) * 4,
io:format("unpack_literal value: ~p size:~p rest:~p~n", [Value, Size, Rest]),
<<X:(Size)>> = L,
{{V, literal, X}, Rest}.
unpack_operator_0(V, T, <<Length:15, Rest0/bitstring>>) ->
io:format("operator0 length:~p~n", [Length]),
<<Pkt:(Length)/bitstring, Rest/bitstring>> = Rest0,
{Rest1, Decoded} = unpack_len(Length, Rest0, []),
{{V, operator_type(T), Decoded}, Rest1}.
unpack_operator_1(V, T, <<Num:11, Rest/bitstring>>) ->
{Rest1, Decoded} = unpack_count(Num, Rest, []),
io:format("operator1 num of packets:~p Decoded:~p, Rest:~p~n", [Num,Decoded,Rest1]),
{{V, operator_type(T), Decoded}, Rest1}.
unpack_len(0, Pkt, Acc) ->
{Pkt, Acc};
unpack_len(Length, Pkt, Acc) ->
{L, R} = unpack(Pkt),
io:format("unpack_len: length:before ~p after: ~p~n", [Length, Length - bit_size(Pkt) + bit_size(R)]),
unpack_len(Length - bit_size(Pkt) + bit_size(R), R, Acc ++ [L]).
unpack_count(0, Pkt, Acc) ->
{Pkt, Acc};
unpack_count(Num, Pkt, Acc) ->
{L, R} = unpack(Pkt),
unpack_count(Num - 1, R, Acc ++ [L]).
operator_type(0) -> 'sum';
operator_type(1) -> 'product';
operator_type(2) -> 'min';
operator_type(3) -> 'max';
operator_type(5) -> 'gt';
operator_type(6) -> 'lt';
operator_type(7) -> 'eq'.

1
2021/day16/input.txt Normal file
View File

@@ -0,0 +1 @@
420D50000B318100415919B24E72D6509AE67F87195A3CCC518CC01197D538C3E00BC9A349A09802D258CC16FC016100660DC4283200087C6485F1C8C015A00A5A5FB19C363F2FD8CE1B1B99DE81D00C9D3002100B58002AB5400D50038008DA2020A9C00F300248065A4016B4C00810028003D9600CA4C0084007B8400A0002AA6F68440274080331D20C4300004323CC32830200D42A85D1BE4F1C1440072E4630F2CCD624206008CC5B3E3AB00580010E8710862F0803D06E10C65000946442A631EC2EC30926A600D2A583653BE2D98BFE3820975787C600A680252AC9354FFE8CD23BE1E180253548D057002429794BD4759794BD4709AEDAFF0530043003511006E24C4685A00087C428811EE7FD8BBC1805D28C73C93262526CB36AC600DCB9649334A23900AA9257963FEF17D8028200DC608A71B80010A8D50C23E9802B37AA40EA801CD96EDA25B39593BB002A33F72D9AD959802525BCD6D36CC00D580010A86D1761F080311AE32C73500224E3BCD6D0AE5600024F92F654E5F6132B49979802129DC6593401591389CA62A4840101C9064A34499E4A1B180276008CDEFA0D37BE834F6F11B13900923E008CF6611BC65BCB2CB46B3A779D4C998A848DED30F0014288010A8451062B980311C21BC7C20042A2846782A400834916CFA5B8013374F6A33973C532F071000B565F47F15A526273BB129B6D9985680680111C728FD339BDBD8F03980230A6C0119774999A09001093E34600A60052B2B1D7EF60C958EBF7B074D7AF4928CD6BA5A40208E002F935E855AE68EE56F3ED271E6B44460084AB55002572F3289B78600A6647D1E5F6871BE5E598099006512207600BCDCBCFD23CE463678100467680D27BAE920804119DBFA96E05F00431269D255DDA528D83A577285B91BCCB4802AB95A5C9B001299793FCD24C5D600BC652523D82D3FCB56EF737F045008E0FCDC7DAE40B64F7F799F3981F2490

68
2021/day17/day17.erl Normal file
View File

@@ -0,0 +1,68 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day17).
-export ([solve/0, solve/1, solve/2]).
-compile ([export_all]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
solution1({X,Y}) ->
Xs = find_x(X),
Ys = find_y(Y),
{T, X1, Y1} = lists:max([{Key, X1, proplists:get_value(Key,Ys)} || {Key, X1} <- Xs, lists:keysearch(Key, 1, Ys) /= false]),
y_max(T,Y1,0).
solution2({X,Y}) ->
Xs = find_x(X),
Ys = find_y(Y),
length(lists:usort([{X,Y} || {Ty, Y} <- Ys, {T,X} <- lists:filter(fun({T,X}) -> T == Ty end, Xs)])).
read_input() ->
{{124,174},{-123, -86}}.
%% {{20,30},{-10, -5}}.
find_x({Xmin, Xmax}) ->
[ {T, Xstart} || Xstart <- lists:seq(0,400), T <- lists:seq(1,1000), x(Xstart, T) >= Xmin, x(Xstart, T) =< Xmax ].
find_y({Ymin, Ymax}) ->
[ {T, Ystart} || Ystart <- lists:seq(-400,400), T <- lists:seq(1,1000), y(Ystart, T) >= Ymin, y(Ystart, T) =< Ymax ].
x(Xstart, T) ->
Last =
case (Xstart - T + 1) of
L when L < 0 -> 0;
L -> L
end,
(Xstart - Last + 1) * (Xstart + Last) div 2.
y(Ystart, T) ->
Last = (Ystart - T + 1),
(Ystart - Last + 1) * (Ystart + Last) div 2.
y_max(0, _, Ymax) -> Ymax;
y_max(T, Y, Ymax) ->
NewY = y(Y, T),
NewMax =
case NewY of
M when M > Ymax -> M;
_ -> Ymax
end,
y_max(T-1, Y, NewMax).

26
2021/day17/diagram.txt Normal file
View File

@@ -0,0 +1,26 @@
1 2 3
4 0123456789012345678901234567890
3 .............#....#............
2 .......#..............#........
1 ...............................
0 S........................#.....
-1 ...............................
-2 ...............................
-3 ...........................#...
-4 ...............................
-5 ....................TTTTTTTTTTT
-6 ....................TTTTTTTTTTT
-7 ....................TTTTTTTT#TT
-8 ....................TTTTTTTTTTT
-9 ....................TTTTTTTTTTT
-10 ....................TTTTTTTTTTT
T X y
0 0 0 = 0
1 7 +6 = 7
2 13 +5 = 7 + 6
3 18 +4 = 7 + 6 + 5
4 22 +3 = 7 + 6 + 5 + 4
5 25 +2
6 27 +1
7 28 +0

1
2021/day17/input.txt Normal file
View File

@@ -0,0 +1 @@
target area: x=124..174, y=-123..-86

130
2021/day18/day18.erl Normal file
View File

@@ -0,0 +1,130 @@
-module(day18).
-export ([solve/0, solve/1]).
-compile ([export_all]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(['1']) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1)]);
solve(1) ->
solution1();
solve(['2']) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2)]);
solve(2) ->
solution2().
solution1() ->
{ok, IO} = file:open("input.txt", 'read'),
Ans = maybe_reduce(read_line(IO), IO),
file:close(IO),
magnitude(Ans).
solution2() ->
{ok, IO} = file:open("input.txt", 'read'),
Numbers = read_all_lines(read_line(IO), IO, []),
file:close(IO),
io:format("total:~p lines: ~p~n", [length(Numbers), Numbers]),
max_magnitude(Numbers).
read_all_lines('eof', _IO, Acc) ->
Acc;
read_all_lines(Line, IO, Acc) ->
read_all_lines(read_line(IO), IO, [Line|Acc]).
read_line(IO) ->
case file:read_line(IO) of
{ok, Line} ->
{ok, Tokens, _} = erl_scan:string(Line ++ "."),
{ok, Term} = erl_parse:parse_term(Tokens),
Term;
'eof' -> 'eof'
end.
max_magnitude(Numbers) when is_list(Numbers) ->
lists:max([ max_magnitude(lists:split(I, Numbers)) || I <- lists:seq(1, length(Numbers))]);
max_magnitude({List1, List2}) ->
[H|T] = lists:reverse(List1),
max_magnitude(H, T ++ List2).
max_magnitude(Number, Rest) ->
lists:foldl(fun(N, MaxMag) ->
case magnitude(reduce([Number, N])) of
Mag when Mag > MaxMag -> Mag;
_ -> MaxMag
end
end, 0, Rest).
magnitude(A) when is_integer(A) -> A;
magnitude([A,B]) ->
(3 * magnitude(A)) + (2 * magnitude(B)).
maybe_reduce(A, IO) ->
case read_line(IO) of
'eof' -> A;
B ->
maybe_reduce(reduce([A,B]), IO)
end.
reduce(N) ->
case maybe_explode(N, 0) of
{true, New, _, _} ->
reduce(New);
false ->
case maybe_split(N) of
{true, New} ->
reduce(New);
false ->
N
end
end.
maybe_explode([A,B], Depth) when Depth >= 4, is_integer(A), is_integer(B) ->
{true, 0, A, B};
maybe_explode([A,B], Depth) ->
case maybe_explode(A, Depth+1) of
{true, New, AddA, AddB} ->
{true, [New, add_explode({b, AddB}, B)], AddA, 0};
false ->
case maybe_explode(B, Depth+1) of
{true, New, AddA, AddB} ->
{true, [add_explode({a, AddA}, A), New], 0, AddB};
false ->
false
end
end;
maybe_explode(N, _) when is_integer(N) ->
false.
add_explode({_, Add}, Num) when is_integer(Num) ->
Add + Num;
add_explode({b, Add}, [A,B]) ->
[add_explode({b, Add}, A), B];
add_explode({a, Add}, [A,B]) ->
[A, add_explode({a,Add}, B)].
maybe_split(N) when is_integer(N) ->
case N > 9 of
true ->
Left = N div 2,
Right = case Left*2 == N of true -> Left; false -> Left+1 end,
{true, [Left, Right]};
false ->
false
end;
maybe_split([A, B]) ->
case maybe_split(A) of
{true, NewA} ->
{true, [NewA, B]};
false ->
case maybe_split(B) of
{true, NewB} ->
{true, [A, NewB]};
false ->
false
end
end.

100
2021/day18/input.txt Normal file
View File

@@ -0,0 +1,100 @@
[[[9,2],[[2,9],0]],[1,[[2,3],0]]]
[[[[2,0],2],[[6,4],[7,3]]],[0,[[3,0],[0,6]]]]
[[[[7,2],2],[9,[6,5]]],[[2,4],5]]
[[[[7,8],2],1],[[[5,4],[2,9]],[7,8]]]
[[[0,7],[1,[6,6]]],[[[0,7],9],4]]
[[[3,[9,6]],[5,1]],[[[0,1],6],[[7,6],0]]]
[[[3,0],[7,[4,0]]],[4,[[6,6],[5,3]]]]
[[[1,[4,8]],[2,[5,8]]],[[[3,6],[2,2]],[[3,8],[7,0]]]]
[9,[[[5,0],[0,3]],[2,[2,6]]]]
[[[3,[8,2]],[[8,0],5]],[[[7,6],[4,9]],[7,5]]]
[[7,[[4,1],9]],[5,1]]
[[[5,[7,5]],1],[8,[5,8]]]
[[[[0,2],7],[[1,4],[9,8]]],[[3,[0,3]],7]]
[[[[4,3],[7,4]],[6,[6,4]]],[8,0]]
[[[1,1],1],[[5,[2,7]],7]]
[[[5,4],5],[[7,[6,3]],[[8,4],6]]]
[[[7,9],[[4,4],[0,0]]],[[[8,6],6],[2,[6,4]]]]
[[[[4,7],[4,9]],3],[[[7,1],[8,6]],[9,[8,2]]]]
[6,[6,[2,9]]]
[[4,[[5,5],[5,0]]],[[[3,4],[9,5]],[8,6]]]
[2,[0,[2,5]]]
[[[4,[7,1]],[2,8]],[[7,0],[[1,6],1]]]
[[[3,4],[[7,8],[6,7]]],[[[6,2],[1,2]],5]]
[[[8,[0,8]],[[9,9],0]],[[[3,5],[4,2]],7]]
[[0,[[0,3],2]],[4,1]]
[[[[0,4],6],7],[[4,[9,1]],3]]
[[0,[[7,0],8]],[2,[8,[8,2]]]]
[[[[3,6],2],[9,4]],[6,[[7,9],[4,5]]]]
[[[[4,9],1],[[9,6],[8,8]]],[[7,[7,6]],[[8,3],[9,0]]]]
[2,0]
[[[[8,2],0],[3,5]],[[7,2],0]]
[[[[1,9],9],6],[9,[[9,3],[8,7]]]]
[[[9,[4,0]],[[7,1],[4,4]]],[[4,[2,3]],[8,7]]]
[[[[9,7],[5,6]],[4,[6,7]]],7]
[5,[[[8,2],8],[6,[7,9]]]]
[0,[[9,[0,1]],[[8,3],7]]]
[[[[4,5],[4,2]],[[5,2],[3,1]]],[[[3,1],[8,5]],8]]
[[0,4],[[2,[2,6]],[[1,1],3]]]
[[[0,8],[7,[5,8]]],7]
[[[7,2],[[6,6],[2,7]]],[[0,[9,3]],2]]
[[[[0,9],2],[[6,0],4]],3]
[[5,[[9,6],9]],[[6,[1,2]],[1,[6,2]]]]
[[[[3,9],5],[9,[7,2]]],[5,[[3,4],[0,6]]]]
[[2,[6,7]],[0,[[2,0],7]]]
[[2,[[5,4],[2,1]]],[2,[[8,7],[5,3]]]]
[[[[0,4],[2,5]],[1,2]],[5,[8,[0,3]]]]
[[[[9,2],[3,2]],[[2,9],4]],5]
[[[[8,9],5],1],[9,3]]
[[5,2],[3,[[8,5],2]]]
[[[0,1],[7,8]],[[[6,2],4],[[6,2],[9,5]]]]
[[[[9,6],5],2],2]
[[[[3,2],3],3],[[[0,1],1],[[8,4],8]]]
[[4,[2,[3,0]]],[[6,[7,0]],6]]
[[6,[[7,8],3]],[[[2,7],4],9]]
[0,2]
[[[9,1],[[3,7],[6,0]]],[[0,[4,1]],[[5,4],7]]]
[[[3,[9,4]],8],[[5,3],2]]
[[6,6],[[[0,5],[0,9]],[[5,5],4]]]
[[[[1,2],4],[[2,4],[8,0]]],[0,[[4,4],[5,8]]]]
[0,[[[9,0],3],[8,4]]]
[[4,5],[[[9,9],[3,5]],[8,[1,4]]]]
[[7,8],[[[3,1],[7,0]],[[4,7],[9,1]]]]
[[4,[2,[1,9]]],[[6,[6,1]],[[0,3],3]]]
[[[5,[0,9]],6],[[[3,4],[9,6]],[[4,0],[0,4]]]]
[[[1,5],[8,[2,8]]],[[5,[0,8]],[[0,7],[4,6]]]]
[[9,[0,2]],[[3,3],[3,1]]]
[[[[2,8],[5,9]],[2,[1,5]]],9]
[[3,[[8,9],[3,1]]],[[[9,0],7],[[0,4],3]]]
[[[[1,5],2],[5,[5,9]]],[5,[[0,1],[0,2]]]]
[6,[[[0,4],8],[[8,2],[5,5]]]]
[[[[7,7],5],[[8,2],7]],[2,5]]
[[[1,1],[[7,8],0]],3]
[[6,[[4,2],9]],[[[5,4],4],3]]
[[[[5,8],3],[[0,4],9]],[[[2,9],2],[3,4]]]
[[0,[4,8]],6]
[[[[9,5],[1,9]],[[3,7],[5,5]]],8]
[[1,9],6]
[[[4,[1,5]],3],0]
[[[2,[6,9]],5],[[5,7],[5,[7,1]]]]
[[[[3,1],[7,3]],[[1,0],[4,6]]],[[[4,9],[4,1]],[9,[2,0]]]]
[[[5,0],[[9,4],6]],[1,[[0,4],[9,9]]]]
[[[[9,8],3],[7,5]],[[[9,5],2],[9,9]]]
[[8,[[8,0],[2,3]]],[[[3,8],[2,6]],[[1,0],0]]]
[[[7,[7,1]],[[6,6],[2,9]]],[[5,[2,0]],[[3,9],[7,4]]]]
[1,[4,[[9,7],[1,3]]]]
[[0,3],[[[4,1],7],[[4,1],[3,0]]]]
[[0,[[7,7],6]],[[4,9],2]]
[[0,8],[4,[4,5]]]
[[[8,[0,5]],[[1,3],[0,5]]],[[2,6],[1,5]]]
[[[[7,6],8],[0,[2,7]]],8]
[8,[[[5,4],8],[[2,1],[7,5]]]]
[[[[7,3],[7,1]],0],[[[7,9],2],3]]
[[8,5],[6,6]]
[[[[5,2],8],7],[[[6,8],[1,0]],[[0,0],1]]]
[[[[1,0],1],6],[9,8]]
[[[[1,2],7],[1,[2,8]]],[[8,1],[[7,5],2]]]
[[0,6],[[2,8],[9,0]]]
[[[0,[7,7]],[2,[0,8]]],[[[7,4],4],[7,[4,0]]]]
[[[2,[9,3]],[[3,7],3]],[[[9,7],[5,6]],8]]
[[2,[[8,7],2]],[[8,[1,8]],[[7,2],1]]]

1050
2021/day2/day2.erl Normal file

File diff suppressed because it is too large Load Diff

BIN
2021/day3/day3 Executable file

Binary file not shown.

1104
2021/day3/day3.erl Normal file

File diff suppressed because it is too large Load Diff

69
2021/day3/day3.hs Normal file

File diff suppressed because one or more lines are too long

600
2021/day4/boards.txt Normal file
View File

@@ -0,0 +1,600 @@
68 73 98 51 49
82 56 87 64 8
46 7 21 38 30
66 5 86 97 74
60 63 76 55 39
92 20 87 77 52
72 29 81 24 64
26 16 19 79 68
8 53 90 14 74
28 89 78 54 15
13 17 35 2 85
37 87 57 74 65
60 21 18 98 96
4 51 46 84 0
90 75 80 41 64
83 98 4 78 45
90 62 71 68 39
47 81 96 73 43
9 94 65 99 60
44 5 29 50 6
23 43 42 35 9
82 90 49 70 59
58 38 44 55 85
3 99 88 65 61
71 53 17 37 4
58 38 47 84 89
60 87 80 92 75
57 83 40 62 25
11 7 33 76 70
82 31 53 56 90
96 40 50 21 38
58 77 64 98 86
9 68 78 55 10
51 74 71 28 16
49 45 32 7 57
71 98 26 5 95
21 53 48 35 92
4 90 9 45 82
67 28 33 34 58
85 41 80 64 19
98 36 33 34 55
15 81 97 56 96
91 73 32 63 83
31 3 85 80 38
71 93 87 86 27
19 15 97 31 26
56 58 54 87 1
9 28 23 7 20
38 67 52 73 88
95 77 27 91 81
81 64 19 67 10
97 73 46 31 3
93 77 8 7 57
0 28 87 42 66
17 79 58 14 18
43 17 63 7 49
28 27 31 23 41
65 34 51 70 77
73 56 74 96 72
94 86 48 71 40
97 43 56 0 30
60 71 34 1 96
10 72 28 82 84
36 22 9 20 67
8 29 92 5 41
28 49 8 78 53
41 32 3 4 88
56 68 18 65 54
24 5 71 21 75
52 15 47 83 79
26 47 2 22 9
6 81 53 17 88
58 25 55 31 19
42 27 85 97 14
64 52 66 63 51
80 43 88 42 51
17 18 84 60 24
34 48 89 33 10
7 21 83 0 26
28 86 25 58 16
71 77 2 99 16
90 59 54 27 10
78 51 26 12 80
25 58 53 33 83
31 7 96 38 95
87 91 7 76 5
96 1 16 62 35
22 61 29 27 12
57 50 78 4 65
3 97 73 82 94
93 63 43 22 71
12 74 41 64 62
81 2 37 99 27
26 39 66 30 20
77 78 18 53 5
72 61 21 59 41
18 49 95 51 30
63 75 10 47 27
83 11 8 39 68
9 99 67 44 77
29 98 74 11 17
44 80 1 78 6
73 20 32 0 27
76 37 77 41 24
36 97 87 89 68
12 35 84 98 87
33 78 64 85 3
18 14 71 54 93
63 34 61 57 22
36 20 91 69 55
84 74 25 67 38
59 60 56 68 81
41 51 23 98 92
30 35 45 17 83
71 97 21 5 94
95 10 92 27 34
15 67 76 14 98
85 81 40 38 58
8 17 6 59 48
78 73 84 11 46
25 50 40 69 27
89 59 17 86 20
3 98 76 90 9
41 75 21 29 55
47 94 23 6 68
82 90 99 65 52
81 18 87 42 6
56 79 36 96 60
70 20 39 22 14
71 88 86 46 48
99 37 16 39 46
82 27 12 19 47
25 80 38 22 55
76 1 7 54 28
60 97 56 32 17
79 5 56 14 51
86 57 18 89 81
97 88 54 60 34
50 25 62 66 98
23 65 87 63 11
71 10 5 63 73
14 70 7 36 20
38 75 59 33 17
18 3 72 46 76
98 80 64 48 30
54 21 56 86 61
71 45 16 5 66
0 12 94 70 1
37 98 26 93 18
87 90 24 44 91
18 10 72 81 48
67 80 42 25 33
92 34 0 27 26
77 99 57 9 46
82 15 54 21 52
73 18 80 27 50
58 97 84 81 21
94 68 8 57 9
56 98 67 36 92
86 53 35 41 89
86 65 2 74 5
3 80 19 41 48
98 99 24 50 57
8 59 53 67 91
62 83 72 9 14
56 69 7 38 87
39 71 84 41 88
23 94 1 97 33
73 14 2 79 72
26 91 45 9 6
17 40 97 51 57
92 25 38 26 23
32 44 83 87 49
54 74 33 7 12
14 36 30 8 98
56 26 3 73 32
90 80 23 0 27
59 19 33 78 52
21 1 20 86 34
85 48 37 97 67
23 13 90 35 44
30 96 18 14 92
80 5 97 11 58
12 50 33 65 78
48 26 86 15 7
70 32 62 92 13
23 1 21 50 59
79 26 11 65 76
90 54 64 63 31
0 19 86 33 9
69 60 3 94 20
35 72 82 42 45
66 6 52 64 62
19 28 83 1 98
76 74 12 9 54
32 29 46 57 50
80 10 42 24 82
56 41 90 7 12
89 78 53 95 9
26 52 67 27 83
85 83 2 65 71
35 66 55 87 39
90 56 97 81 9
73 32 98 84 95
88 33 34 25 86
78 8 34 21 23
2 86 10 4 58
31 62 35 91 63
81 90 69 15 47
41 12 61 72 36
10 95 68 87 25
59 45 54 92 82
34 81 12 61 83
89 69 98 72 44
84 20 51 36 1
59 94 26 76 54
39 21 86 13 48
58 61 75 80 62
8 88 74 24 19
42 38 72 60 93
82 46 54 48 20
53 94 57 98 83
15 10 39 17 91
89 47 4 35 27
61 42 92 13 8
97 73 9 67 58
87 49 96 88 94
68 38 11 41 43
26 51 4 22 0
18 2 84 62 33
79 25 51 78 93
76 49 69 87 26
63 71 67 28 34
48 38 99 66 11
44 61 75 96 23
66 79 37 23 98
43 94 49 27 36
21 86 34 24 42
4 8 85 63 32
74 82 68 92 72
12 77 87 53 76
71 66 10 68 36
74 49 27 16 34
98 21 54 93 18
95 61 97 65 32
60 91 68 11 76
66 62 65 3 41
6 84 61 58 73
7 28 63 75 55
35 45 98 47 81
85 75 46 45 58
95 92 73 99 47
25 97 76 15 23
19 5 37 36 65
96 8 24 49 61
53 60 16 94 59
10 47 82 17 89
86 91 1 40 45
34 76 38 97 63
25 85 57 27 93
77 95 20 24 93
37 4 23 39 35
81 2 56 18 87
46 75 52 51 50
88 8 83 80 27
20 47 27 52 2
97 0 17 64 11
53 9 69 88 77
13 89 28 21 36
71 33 31 6 68
45 25 6 18 7
51 84 82 83 81
13 65 34 93 71
87 92 49 8 24
76 29 53 96 58
15 41 65 85 29
78 30 93 98 67
36 58 12 1 25
3 10 88 9 96
8 53 7 14 6
78 37 50 99 51
42 19 40 62 54
89 38 64 70 56
96 72 41 43 95
90 91 17 28 59
23 5 45 80 93
14 75 78 25 86
21 24 73 30 34
39 74 22 19 49
72 31 1 77 57
59 8 57 68 32
71 19 9 29 63
3 92 40 79 31
87 75 99 88 2
15 20 85 89 44
7 41 16 15 46
76 5 51 11 38
28 70 68 91 66
56 94 59 34 86
72 80 42 75 65
92 95 48 65 89
27 64 69 15 33
93 47 30 7 63
91 35 29 42 72
90 23 11 50 54
97 17 66 92 91
81 23 12 6 93
48 25 27 16 54
21 32 50 94 98
15 55 40 11 84
66 56 95 44 33
26 11 41 22 86
2 1 50 79 32
70 74 84 5 90
92 0 6 73 40
34 48 28 98 88
66 60 46 54 91
43 21 81 95 33
53 87 82 79 92
45 62 58 99 96
6 16 70 86 9
19 95 37 96 62
3 42 24 60 15
55 56 92 80 26
72 85 91 73 94
72 73 94 15 4
3 40 67 21 84
45 33 60 82 10
48 6 29 79 70
27 66 13 38 47
31 0 56 89 61
62 49 37 73 1
63 68 38 83 44
70 17 69 14 81
72 58 52 50 79
46 18 61 11 14
51 58 94 98 69
2 0 93 6 95
92 17 3 37 33
48 20 45 16 13
98 74 36 23 6
65 17 78 95 96
68 63 47 16 18
87 30 53 51 57
69 11 44 75 89
25 56 5 53 3
58 1 91 2 47
72 75 44 96 70
30 63 10 93 74
67 55 82 32 61
31 97 28 14 48
87 50 95 23 83
33 34 4 46 94
43 84 86 13 40
52 64 16 88 81
40 30 14 36 90
2 25 12 10 33
18 27 78 73 60
11 92 52 69 93
19 22 35 17 61
42 45 49 8 14
21 18 69 12 27
25 76 63 28 64
52 7 77 58 39
87 89 88 38 4
40 57 53 48 46
13 59 23 55 61
1 50 83 73 31
18 47 29 65 27
76 49 33 51 26
65 76 78 90 30
25 83 4 23 3
53 34 20 36 37
18 66 12 45 59
68 50 74 96 48
39 51 92 7 22
11 47 44 26 55
73 52 38 45 59
72 76 17 56 97
83 13 70 29 37
25 12 84 51 14
2 22 85 13 10
73 93 67 72 80
56 90 60 42 61
36 16 50 70 97
1 60 44 32 47
71 76 69 27 54
22 43 12 72 9
17 90 53 19 95
41 65 62 11 63
76 64 33 3 81
28 20 95 98 79
58 23 87 69 29
31 72 55 49 36
15 67 83 37 52
52 87 40 67 91
19 7 80 88 29
97 28 50 63 53
43 89 35 69 75
79 65 58 78 86
21 52 43 71 69
26 47 81 91 20
70 90 6 49 78
11 72 82 83 2
62 64 66 93 48
47 78 38 10 82
12 32 71 41 46
18 13 74 63 90
86 96 17 97 9
70 4 59 52 6
29 49 54 76 5
42 99 92 52 26
48 80 65 9 89
87 68 47 24 78
74 2 85 43 56
21 7 44 36 3
0 72 66 28 74
99 68 83 53 30
96 81 4 69 23
15 20 31 41 42
87 78 21 6 62
68 95 30 5 20
81 54 42 50 70
18 39 93 35 72
22 97 73 74 75
25 24 62 5 54
85 31 88 40 76
48 44 15 3 4
61 47 56 72 9
11 49 50 78 39
54 30 52 70 16
63 49 91 3 51
71 36 46 79 67
80 66 31 57 43
45 9 84 17 35
68 41 32 23 24
95 62 87 97 5
16 2 48 17 1
59 9 35 96 22
50 70 44 89 31
21 35 47 36 2
33 49 77 39 60
70 91 97 18 66
25 95 22 87 20
69 27 76 52 11
42 40 26 61 98
33 88 79 72 73
7 57 71 0 82
2 21 74 63 41
58 96 50 13 4
80 84 96 31 38
58 75 19 91 6
10 54 52 66 81
55 35 47 23 69
95 40 21 17 79
58 68 27 0 81
49 51 93 83 23
5 90 8 76 57
53 33 45 75 84
72 28 38 43 40
73 62 93 77 17
4 89 82 85 18
33 57 58 55 49
48 11 94 14 72
7 53 34 69 21
19 37 69 48 13
5 14 46 2 86
40 87 95 79 36
81 62 70 23 82
51 43 91 29 9
70 16 3 5 38
62 85 19 15 41
36 42 13 31 87
58 66 27 49 6
82 54 91 23 10
67 63 60 80 66
16 30 2 85 97
45 52 62 21 49
83 75 76 10 82
47 90 72 5 9
3 11 31 61 99
42 62 15 64 40
30 95 7 81 28
63 50 74 77 34
38 89 73 2 92
34 73 60 32 56
49 35 44 79 83
64 61 57 5 24
72 58 8 66 77
94 31 55 67 74
49 17 22 97 88
0 16 14 93 31
28 73 6 82 90
80 45 92 55 78
3 42 65 37 29
23 61 97 1 69
53 98 28 52 19
66 51 46 77 15
34 36 47 80 14
7 89 62 9 49

100
2021/day4/day4.erl Normal file
View File

@@ -0,0 +1,100 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day3 solve
%%
-module(day4).
-export ([solve/0, solve/1, solve/2]).
-export ([read_boards/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_boards()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_boards() ->
{ok, IO} = file:open("boards.txt", 'read'),
Data = read_boards(IO),
file:close(IO),
Data.
read_boards(IO) ->
read_boards(IO, []).
read_boards('eof', Boards) ->
Boards;
read_boards(IO, Boards) ->
case read_board(IO) of
'eof' -> Boards;
Board -> read_boards(IO, Boards ++ [Board])
end.
read_board(IO) ->
read_line(IO, []).
read_line(IO, Board) ->
case file:read_line(IO) of
{ok, "\n"} -> make_board(Board);
'eof' -> 'eof';
{ok, Line} -> read_line(IO, [[ list_to_integer(X) || X <- string:tokens(Line, " \n")]|Board])
end.
make_board(B) ->
B ++ [lists:map(fun(X) -> lists:nth(Y,X) end, B) || Y <- lists:seq(1,5)].
solution1(Boards) ->
Input = get_input(),
Bingo = process(Boards, 1, Input),
score(Bingo).
solution2(Boards) ->
Input = get_input(),
Bingo = process(Boards, length(Boards), Input),
score(Bingo).
score({'bingo', Num, Board}) ->
Fun = fun(L) -> lists:foldl(fun(X, Acc) -> X + Acc end, 0, L) end,
Num * (Fun([Fun(X) || X <- Board]) / 2).
process(Boards, _Count, []) -> Boards;
process(Boards, Count, [H|T]) ->
New_boards = process_boards(Boards, H, []),
case is_bingo(New_boards) of
{'bingo', Board} when Count == 1 -> {'bingo', H, Board};
{'bingo', _Board} ->
New_boards2 = delete_bingo_boards(New_boards),
process(New_boards2, Count - length(New_boards) + length(New_boards2), T);
_Else -> process(New_boards, Count, T)
end.
process_boards([], _Num, Acc) ->
Acc;
process_boards([B|T], Num, Acc) ->
process_boards(T, Num, [[lists:delete(Num, X) || X <- B]|Acc]).
is_bingo([]) -> 'false';
is_bingo([Board|Rest]) ->
case is_line(Board) of
'true' -> {'bingo', Board};
'false' -> is_bingo(Rest)
end.
is_line(Board) ->
lists:any(fun(X) -> X == [] end, Board).
delete_bingo_boards(Boards) ->
[Board || Board <- Boards, not is_line(Board)].
get_input() ->
[83,69,34,46,30,23,19,75,22,37,89,78,32,39,11,44,95,43,26,48,84,53,94,88,18,40,62,35,27,42,15,2,91,20,4,64,99,71,54,97,52,36,28,7,74,45,70,86,98,1,61,50,68,6,77,8,57,47,51,72,65,3,49,24,79,13,17,92,41,80,63,67,82,90,55,0,10,93,38,21,59,73,33,31,9,76,5,66,16,58,85,87,12,29,25,14,96,56,60,81].

110
2021/day5/day5.erl Normal file
View File

@@ -0,0 +1,110 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day5).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' ->
lists:map(fun([{X1,Y1},{X2,Y2}]) ->
case X1 > X2 orelse (X1 == X2 andalso Y1 > Y2) of
'true' -> [{X2,Y2},{X1,Y1}];
'false' -> [{X1,Y1},{X2,Y2}]
end
end, Input);
Line -> read_input(IO, Input ++ [Line])
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> parse_line(Line)
end.
parse_line(Line) ->
Points = string:tokens(Line, " ,->\n"),
[X1, Y1, X2, Y2] = [list_to_integer(X) || X <- Points],
[{X1, Y1},{X2,Y2}].
solution1(Input) ->
HV = get_hor_vert(Input),
Points = lists:sort(
lists:flatten(
[[{X,Y} || X <- lists:seq(X1, X2, case X1 =< X2 of 'true' -> 1; _ -> -1 end),
Y <- lists:seq(Y1, Y2, case Y1 =< Y2 of 'true' -> 1; _ -> -1 end)] || [{X1,Y1},{X2,Y2}] <- HV]
)
),
Count = count_duplicates(Points),
Count.
solution2(Input) ->
Points = get_all_points(Input),
Count = count_duplicates(lists:sort(Points)),
Count.
get_hor_vert(Input) ->
lists:filter(fun([{X1, Y1},{X2,Y2}]) -> X1 == X2 orelse Y1 == Y2 end, Input).
get_all_points(Input) ->
get_all_points(Input, []).
get_all_points([], Acc) ->
Acc;
get_all_points([[{X1,Y1},{X2,Y2}]|Rest], Acc) when X1 == X2 ->
get_all_points(Rest, Acc ++ [{X1, Y} || Y <- lists:seq(Y1,Y2)]);
get_all_points([[{X1,Y1},{X2,Y2}]|Rest], Acc) when Y1 == Y2 ->
get_all_points(Rest, Acc ++ [{X, Y1} || X <- lists:seq(X1,X2)]);
get_all_points([[{X1,Y1},{X2,Y2}]|Rest], Acc) when Y1 < Y2 ->
get_all_points(Rest, Acc ++ [{X, Y1-X1+X} || X <- lists:seq(X1,X2)]);
get_all_points([[{X1,Y1},{X2,Y2}]|Rest], Acc) when Y1 > Y2 ->
get_all_points(Rest, Acc ++ [{X, (-1 * X) + X1 + Y1 } || X <- lists:seq(X1,X2)]).
count_duplicates(Points) ->
count_duplicates(Points, []).
count_duplicates([], Count) ->
lists:foldl(fun({X, _P}, Acc) ->
case X > 1 of 'true' -> Acc + 1; _ -> Acc end end, 0, Count);
count_duplicates([H|T], Count) ->
case T /= [] andalso hd(T) == H of
'true' -> count_duplicate(H, T, Count, 1);
_Else -> count_duplicates(T, Count ++ [{1, H}])
end.
count_duplicate(H, [H|T], Count, X) ->
count_duplicate(H, T, Count, X + 1);
count_duplicate(H, List, Count, X) ->
count_duplicates(List, Count ++ [{X, H}]).

4
2021/day5/day5.hs Normal file
View File

@@ -0,0 +1,4 @@
import System.IO
main = do
contents <- readFile "input.txt"
putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!"

500
2021/day5/input.txt Normal file
View File

@@ -0,0 +1,500 @@
941,230 -> 322,849
762,196 -> 701,257
656,197 -> 595,136
687,692 -> 57,692
37,953 -> 903,87
674,102 -> 84,102
952,323 -> 786,157
807,948 -> 430,948
280,66 -> 514,66
810,381 -> 928,263
41,278 -> 112,207
754,11 -> 754,574
499,830 -> 725,604
713,172 -> 658,172
805,54 -> 594,54
442,910 -> 40,508
160,170 -> 925,935
265,899 -> 265,313
960,976 -> 77,93
820,244 -> 877,187
883,501 -> 345,501
12,978 -> 941,49
988,46 -> 988,572
285,775 -> 285,298
718,69 -> 121,69
218,641 -> 146,641
857,277 -> 124,277
32,36 -> 657,36
964,280 -> 609,280
739,981 -> 910,981
960,794 -> 243,794
447,682 -> 751,378
813,103 -> 813,240
568,705 -> 497,705
888,47 -> 888,231
936,95 -> 336,695
305,349 -> 18,636
54,240 -> 54,222
28,704 -> 625,107
680,325 -> 680,623
209,405 -> 209,123
947,906 -> 947,721
149,810 -> 834,125
897,875 -> 146,124
928,267 -> 928,484
871,516 -> 871,136
954,725 -> 706,725
680,645 -> 958,645
680,326 -> 908,326
173,157 -> 890,874
842,802 -> 166,126
750,442 -> 270,922
567,891 -> 567,784
374,623 -> 374,174
979,725 -> 765,511
336,440 -> 82,440
214,213 -> 939,938
652,815 -> 763,815
220,48 -> 331,159
580,522 -> 141,522
286,685 -> 286,779
865,343 -> 865,257
738,898 -> 405,565
703,571 -> 420,571
792,368 -> 792,955
738,905 -> 738,79
646,95 -> 737,95
930,908 -> 72,50
310,933 -> 310,243
192,22 -> 918,748
245,803 -> 81,639
567,218 -> 901,218
148,950 -> 965,133
147,772 -> 159,772
774,84 -> 774,960
860,798 -> 372,798
856,131 -> 856,703
368,603 -> 247,603
587,533 -> 301,533
832,461 -> 832,506
164,709 -> 960,709
874,471 -> 327,471
346,237 -> 346,921
683,300 -> 910,527
353,717 -> 353,575
586,578 -> 798,366
27,813 -> 27,434
311,391 -> 418,391
369,304 -> 33,304
591,226 -> 591,558
634,545 -> 513,545
439,257 -> 207,257
42,791 -> 581,252
155,801 -> 155,294
599,603 -> 599,182
48,607 -> 337,896
199,828 -> 506,828
28,147 -> 733,852
799,563 -> 799,22
206,625 -> 455,874
185,330 -> 335,480
161,746 -> 590,746
932,13 -> 269,13
649,746 -> 649,309
463,169 -> 930,636
568,251 -> 386,251
739,692 -> 233,692
941,989 -> 84,132
513,356 -> 513,628
534,168 -> 285,168
447,563 -> 447,698
898,915 -> 791,808
339,405 -> 432,405
414,940 -> 335,940
591,741 -> 59,741
347,330 -> 347,341
186,40 -> 438,292
849,872 -> 295,318
406,620 -> 938,620
346,226 -> 864,226
609,40 -> 478,171
820,900 -> 947,900
201,63 -> 201,107
984,652 -> 47,652
193,204 -> 776,204
173,892 -> 740,892
389,675 -> 709,355
489,954 -> 546,954
18,82 -> 587,651
646,150 -> 675,150
618,805 -> 618,592
178,617 -> 178,606
179,30 -> 505,30
984,21 -> 21,984
172,167 -> 15,167
17,209 -> 192,209
814,945 -> 814,18
385,632 -> 161,632
126,41 -> 474,389
575,778 -> 737,778
74,270 -> 147,270
891,248 -> 467,672
95,426 -> 95,728
235,73 -> 235,583
730,302 -> 730,466
388,587 -> 377,598
525,155 -> 184,155
370,278 -> 966,874
950,150 -> 444,656
644,935 -> 401,935
798,515 -> 506,807
976,562 -> 253,562
674,350 -> 603,421
686,653 -> 576,653
691,278 -> 593,180
964,961 -> 76,73
735,582 -> 735,389
786,885 -> 76,885
402,732 -> 231,732
660,881 -> 660,525
683,383 -> 683,364
174,20 -> 174,75
692,819 -> 107,819
344,669 -> 577,902
562,126 -> 697,261
621,344 -> 621,707
731,892 -> 213,374
216,828 -> 663,828
990,534 -> 990,356
973,714 -> 519,714
25,981 -> 983,23
659,399 -> 535,275
967,885 -> 183,101
612,684 -> 732,684
955,485 -> 955,806
582,714 -> 582,719
342,203 -> 905,203
188,488 -> 272,488
659,65 -> 659,679
306,85 -> 605,384
975,847 -> 975,353
742,989 -> 742,652
917,524 -> 934,524
890,571 -> 662,799
901,791 -> 901,118
631,447 -> 114,447
850,28 -> 797,28
842,759 -> 91,759
659,538 -> 253,944
693,69 -> 693,452
161,515 -> 789,515
892,630 -> 892,785
78,947 -> 931,947
561,728 -> 11,178
138,842 -> 138,133
890,373 -> 628,373
509,370 -> 592,370
982,41 -> 185,838
184,210 -> 184,218
390,525 -> 390,558
387,151 -> 387,39
718,808 -> 833,808
206,234 -> 206,620
84,150 -> 84,959
336,468 -> 307,468
764,19 -> 739,44
752,607 -> 643,607
233,149 -> 112,149
368,612 -> 725,255
929,497 -> 909,477
829,274 -> 829,190
312,268 -> 312,128
519,18 -> 519,552
896,19 -> 140,19
368,727 -> 368,114
233,813 -> 750,813
477,758 -> 477,213
615,171 -> 615,530
38,461 -> 301,461
862,107 -> 154,815
271,52 -> 271,517
203,936 -> 365,936
96,700 -> 13,617
290,554 -> 389,455
377,923 -> 377,890
347,511 -> 147,511
889,412 -> 762,412
558,412 -> 424,412
45,838 -> 45,845
958,27 -> 958,454
154,244 -> 20,244
315,154 -> 315,173
135,618 -> 135,71
380,422 -> 131,671
314,500 -> 314,873
915,320 -> 915,159
213,772 -> 977,772
14,22 -> 978,986
444,759 -> 444,385
730,650 -> 730,210
532,551 -> 633,652
547,426 -> 335,426
868,191 -> 156,903
462,599 -> 611,748
729,709 -> 729,714
665,229 -> 849,413
880,947 -> 880,159
249,837 -> 249,604
575,205 -> 196,584
960,665 -> 320,25
617,853 -> 412,853
224,60 -> 224,467
226,741 -> 226,47
371,595 -> 118,342
371,708 -> 371,561
236,141 -> 955,860
55,509 -> 55,938
684,885 -> 684,670
93,509 -> 497,105
284,61 -> 812,61
438,353 -> 242,353
77,716 -> 363,430
283,769 -> 905,147
56,799 -> 551,799
804,637 -> 804,526
476,54 -> 154,54
686,400 -> 686,145
740,905 -> 417,905
21,113 -> 823,915
286,132 -> 880,726
923,378 -> 771,378
924,922 -> 36,34
801,609 -> 801,407
465,671 -> 550,756
628,235 -> 628,842
684,840 -> 716,808
841,366 -> 495,712
740,208 -> 740,174
657,370 -> 657,731
817,781 -> 466,781
308,894 -> 308,370
497,233 -> 755,233
35,145 -> 35,398
383,163 -> 578,163
620,985 -> 620,849
178,253 -> 178,724
556,51 -> 556,525
650,187 -> 706,243
161,988 -> 599,550
861,256 -> 501,616
46,555 -> 181,555
980,975 -> 980,916
345,751 -> 479,617
534,642 -> 534,202
901,240 -> 901,490
984,280 -> 337,927
578,663 -> 578,298
377,943 -> 259,943
975,38 -> 39,974
697,870 -> 387,560
147,520 -> 218,520
683,711 -> 486,711
825,26 -> 122,729
855,84 -> 751,84
558,945 -> 989,945
660,195 -> 597,195
889,696 -> 317,696
969,248 -> 240,977
598,625 -> 598,148
176,151 -> 256,151
939,70 -> 648,70
645,431 -> 411,431
502,518 -> 221,518
821,988 -> 213,988
361,850 -> 684,850
506,173 -> 506,405
323,151 -> 726,151
131,519 -> 35,519
164,445 -> 798,445
425,989 -> 425,133
18,739 -> 684,73
138,545 -> 138,155
401,104 -> 766,104
864,855 -> 203,855
636,361 -> 604,361
820,970 -> 820,882
866,859 -> 835,859
112,507 -> 112,715
529,494 -> 529,928
104,469 -> 193,469
82,841 -> 831,92
258,518 -> 258,778
34,917 -> 135,917
777,553 -> 985,345
64,952 -> 719,297
341,224 -> 902,224
87,128 -> 525,566
951,400 -> 448,903
344,963 -> 21,963
983,244 -> 983,503
938,771 -> 635,771
560,262 -> 560,974
46,386 -> 75,386
898,747 -> 898,17
239,929 -> 149,929
849,881 -> 849,251
204,204 -> 204,753
830,33 -> 830,130
304,339 -> 42,339
565,312 -> 773,312
387,523 -> 234,523
239,421 -> 543,725
197,433 -> 197,723
595,21 -> 370,21
547,171 -> 480,104
639,910 -> 639,241
908,185 -> 560,185
947,565 -> 947,411
211,670 -> 588,293
753,708 -> 753,624
36,147 -> 859,970
423,94 -> 930,94
613,680 -> 607,680
277,263 -> 836,822
186,413 -> 827,413
483,173 -> 142,173
25,771 -> 409,387
328,916 -> 613,631
267,604 -> 724,147
430,616 -> 150,896
692,463 -> 50,463
306,360 -> 306,653
736,948 -> 736,174
797,529 -> 774,529
492,486 -> 492,812
659,429 -> 102,429
582,503 -> 695,616
780,62 -> 780,164
58,318 -> 387,318
286,694 -> 286,396
248,241 -> 248,361
112,963 -> 707,963
771,722 -> 636,722
508,76 -> 389,76
435,307 -> 201,541
167,312 -> 618,763
721,407 -> 305,823
57,203 -> 516,203
83,239 -> 83,607
810,686 -> 137,13
817,268 -> 101,984
379,975 -> 379,631
597,38 -> 611,38
56,504 -> 56,900
108,587 -> 261,740
625,426 -> 476,426
248,486 -> 643,881
932,25 -> 21,936
388,613 -> 388,296
644,188 -> 644,273
871,425 -> 871,791
722,866 -> 722,39
96,579 -> 96,97
876,64 -> 297,643
581,633 -> 59,633
11,10 -> 989,988
947,55 -> 266,736
532,553 -> 735,756
898,855 -> 83,40
533,289 -> 306,62
497,736 -> 332,571
871,201 -> 345,727
550,686 -> 256,686
858,585 -> 607,836
380,171 -> 15,171
864,112 -> 864,686
791,857 -> 305,857
898,579 -> 741,579
479,713 -> 113,713
19,143 -> 779,903
347,161 -> 140,368
479,395 -> 534,340
929,37 -> 77,889
128,958 -> 884,202
921,18 -> 921,650
263,550 -> 263,280
155,592 -> 235,592
565,34 -> 565,454
913,371 -> 173,371
199,158 -> 974,933
98,775 -> 98,234
649,576 -> 649,444
801,855 -> 548,855
859,913 -> 363,913
274,487 -> 274,654
729,982 -> 443,982
664,827 -> 77,240
656,885 -> 656,350
916,74 -> 284,706
439,31 -> 439,175
423,753 -> 280,753
424,914 -> 948,914
980,723 -> 980,674
656,437 -> 626,407
577,654 -> 423,654
19,224 -> 424,224
310,181 -> 704,575
828,296 -> 828,308
905,151 -> 955,151
319,178 -> 892,178
972,939 -> 65,32
497,98 -> 91,98
987,402 -> 943,446
904,19 -> 174,749
265,885 -> 265,835
475,414 -> 658,597
610,93 -> 938,93
961,892 -> 661,892
297,600 -> 378,600
405,637 -> 52,284
439,874 -> 439,612
275,185 -> 275,218
220,840 -> 220,735
372,153 -> 644,425
896,964 -> 896,461
916,484 -> 951,449
485,355 -> 456,355
198,793 -> 198,132
614,735 -> 561,735
181,591 -> 147,591
175,289 -> 159,289
899,758 -> 962,695
506,647 -> 506,858
443,828 -> 720,828
623,641 -> 623,631
202,409 -> 891,409
486,751 -> 80,345
781,73 -> 781,710
911,643 -> 911,571
799,151 -> 89,861
716,815 -> 810,815
947,517 -> 947,575
704,260 -> 704,727
113,581 -> 113,606
408,252 -> 408,761
601,753 -> 457,609
851,424 -> 501,774
670,941 -> 916,941
480,839 -> 205,564
912,949 -> 38,75
477,39 -> 925,487
139,898 -> 309,898
93,386 -> 93,194
184,132 -> 943,891
247,557 -> 247,182
832,22 -> 76,778
61,814 -> 806,69
816,640 -> 604,428
214,561 -> 623,152
698,858 -> 389,858

73
2021/day6/day6.erl Normal file
View File

@@ -0,0 +1,73 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day6).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_line(IO).
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> parse_line(Line)
end.
parse_line(Line) ->
Points = string:tokens(Line, " ,->\n"),
parse_line([list_to_integer(X) || X <- Points], [{0,0},{1,0},{2,0},{3,0},{4,0},{5,0},{6,0},{7,0},{8,0}]).
parse_line([], Acc) ->
io:format("input: ~p~n", [Acc]),
Acc;
parse_line([H|T], Acc) ->
{A, V} = lists:keyfind(H, 1, Acc),
parse_line(T, lists:keyreplace(H, 1, Acc, {A, V + 1})).
solution1(Input) ->
Fish = days(Input, 80),
io:format("OUT: ~p~n", [Fish]),
lists:foldl(fun({_A, V}, Acc) -> Acc + V end, 0, Fish).
solution2(Input) ->
Fish = days(Input, 256),
io:format("OUT: ~p~n", [Fish]),
lists:foldl(fun({_A, V}, Acc) -> Acc + V end, 0, Fish).
days(Input, 0) ->
Input;
days(Input, Count) ->
New =
lists:foldl(fun({A, V}, AccIn) ->
case A of
0 ->
{7, V1} = lists:keyfind(7, 1, Input),
AccIn ++ [{6, V + V1},{8, V}];
7 -> AccIn;
_ -> AccIn ++ [{A-1, V}]
end
end, [], Input),
days(New, Count -1).

1
2021/day6/input.txt Normal file
View File

@@ -0,0 +1 @@
1,1,1,2,1,1,2,1,1,1,5,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,4,1,1,1,1,3,1,1,3,1,1,1,4,1,5,1,3,1,1,1,1,1,5,1,1,1,1,1,5,5,2,5,1,1,2,1,1,1,1,3,4,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,4,1,1,1,1,1,5,1,2,4,1,1,1,1,1,3,3,2,1,1,4,1,1,5,5,1,1,1,1,1,2,5,1,4,1,1,1,1,1,1,2,1,1,5,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,3,1,1,3,1,3,1,4,1,5,4,1,1,2,1,1,5,1,1,1,1,1,5,1,1,1,1,1,1,1,1,1,4,1,1,4,1,1,1,1,1,1,1,5,4,1,2,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,4,1,1,1,2,1,4,1,1,1,1,1,1,1,1,1,4,2,1,2,1,1,4,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,3,2,1,4,1,5,1,1,1,4,5,1,1,1,1,1,1,5,1,1,5,1,2,1,1,2,4,1,1,2,1,5,5,3

79
2021/day7/day7.erl Normal file
View File

@@ -0,0 +1,79 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day7).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_line(IO).
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> parse_line(Line)
end.
parse_line(Line) ->
Points = string:tokens(Line, " ,->\n"),
[list_to_integer(X) || X <- Points].
solution1(Input) ->
Max = lists:max(Input),
Min = lists:min(Input),
io:format("Max: ~p Min: ~p~n", [Max, Min]),
Fuels = [ fuel_needed(X, Input, fun fuel1/2) || X <- lists:seq(Min, Max)],
lists:min(Fuels).
solution2(Input) ->
Max = lists:max(Input),
Min = lists:min(Input),
io:format("Soln2: Max: ~p Min: ~p~n", [Max, Min]),
Fuels = [ fuel_needed(X, Input, fun fuel2/2) || X <- lists:seq(Min, Max)],
lists:min(Fuels).
fuel_needed(Pos, Data, Fuel_fun) ->
fuel_needed(Pos, Data, Fuel_fun, 0).
fuel_needed(_Pos, [], _, Acc) -> Acc;
fuel_needed(Pos, [H|T], Fuel_fun, Acc) ->
fuel_needed(Pos, T, Fuel_fun, Acc + Fuel_fun(H, Pos)).
fuel1(X, X) -> 0;
fuel1(X, Y) when X > Y -> X - Y;
fuel1(X, Y) when Y > X -> Y - X.
fuel2(X, X) -> 0;
fuel2(X, Y) when X > Y -> fuel_steps(X - Y);
fuel2(X, Y) when Y > X -> fuel_steps(Y - X).
fuel_steps(X) ->
fuel_steps(X, 1, 0).
fuel_steps(0, _Cost, Count) -> Count;
fuel_steps(X, Cost, Count) ->
fuel_steps(X - 1, Cost + 1, Count + Cost).

1
2021/day7/input.txt Normal file
View File

@@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,76,50,481,307,723,1100,235,147,851,504,1068,780,490,126,771,831,358,711,68,281,645,644,862,705,372,217,679,1097,1005,728,1739,571,40,1293,782,378,802,245,1370,1512,9,372,448,64,36,1212,141,585,1468,278,286,476,239,285,664,277,43,665,1037,654,205,1238,36,41,276,945,624,47,91,1569,284,107,845,60,961,30,21,269,1091,304,364,339,169,430,176,156,1483,1510,111,146,447,590,1227,611,483,428,396,839,307,901,380,128,80,535,461,482,379,59,281,977,44,966,545,37,163,845,845,151,936,269,938,612,1284,863,516,927,511,825,125,599,101,394,1062,140,483,218,83,443,404,492,78,507,860,1741,43,128,60,566,181,5,554,937,163,280,655,184,480,408,127,935,613,766,46,312,770,697,275,13,84,600,168,83,281,599,607,1441,197,344,0,302,414,147,370,748,421,844,871,319,666,117,640,247,167,529,324,252,235,303,443,1305,796,109,20,364,532,1388,708,769,916,340,405,90,47,504,516,97,535,28,69,960,590,254,106,188,190,1388,698,246,264,98,229,1648,292,710,14,421,31,147,1493,552,1371,454,4,146,674,452,1267,1027,170,141,936,1341,884,558,276,631,68,39,2,464,1,839,318,881,413,2,452,352,34,89,323,884,1439,243,79,56,128,1273,1134,606,11,682,747,415,599,782,179,269,320,682,177,336,466,10,370,159,1636,367,888,573,171,682,60,9,59,332,10,1496,637,1029,413,186,1183,77,309,461,883,1079,699,233,69,259,108,1160,435,480,495,13,858,718,126,115,728,1008,133,442,7,598,1475,1156,226,162,415,3,151,72,527,792,494,763,144,64,490,273,1245,300,465,744,36,1465,251,8,494,1126,362,180,1263,175,141,1041,103,163,205,568,93,699,103,437,204,931,563,550,88,415,146,265,31,221,1123,835,375,1101,578,388,92,1417,845,308,343,499,158,293,242,4,509,574,254,1556,69,668,691,0,558,16,687,1210,166,748,400,863,66,600,771,1073,561,738,398,384,232,350,393,1113,1222,153,462,907,797,712,18,1463,1185,1055,994,57,130,265,131,52,463,902,453,38,132,783,1560,232,169,1162,173,311,5,1477,397,336,480,540,491,67,340,27,291,341,35,275,78,1525,387,218,63,79,533,4,569,1643,595,1508,851,39,1200,912,10,53,42,60,154,1174,155,275,137,677,367,1373,4,708,441,756,647,1054,872,1039,109,530,1179,939,429,567,866,1411,436,23,212,184,66,79,831,538,90,827,678,549,313,434,60,907,284,171,570,1091,603,447,122,1092,29,789,563,462,15,310,340,16,365,393,614,48,368,42,457,736,737,1008,513,61,764,366,400,525,1683,1177,909,908,112,734,16,79,917,541,127,107,79,1208,32,258,596,166,376,1313,735,1457,864,563,55,439,54,694,81,93,48,470,1028,689,1177,1331,155,412,847,250,405,387,8,456,18,619,533,729,1475,1182,935,210,55,355,958,15,32,598,85,175,471,1087,280,652,53,13,225,12,488,717,353,2,134,351,698,276,456,209,535,604,19,12,785,3,63,879,437,216,1,1275,811,786,417,33,51,733,1074,143,309,65,555,557,78,611,909,260,973,701,998,490,213,9,233,760,933,916,437,1369,1952,372,324,859,670,73,296,1391,127,407,230,52,16,547,803,883,258,308,710,343,1290,184,8,41,9,68,104,175,1034,1544,219,752,327,690,134,601,1574,214,385,1233,231,267,944,1533,349,431,97,632,278,1505,162,888,62,90,489,351,990,846,14,159,134,14,314,148,214,1153,513,114,6,49,10,14,957,219,16,204,954,863,50,482,90,696,99,253,252,433,57,385,54,343,106,154,78,1595,590,380,102,825,1933,191,1328,374,263,355,137,494,60,781,1113,391,274,325,326,14,965,269,15,32,742,81,393,730,892,982,103,890,499,58,816,292,29,480,173,831,132,1033,1511,1137,1511,22,1105,146,344,308,915,540,1371,1238,414,352,304,841,749,6,491,30,1322,415,293,1207,31,90,636,303,1551,354,23,275,18,32,623,1483,49,12,311,407,1551,296,252,647,778,1499,98,1220,264,1020,1440,377,1125,8,72,270,162,348,3,1023,965,719,62,1467,1176,663,439,557,654,85,1493,70,349,10,727,15,1173,387,529,608,1398,905,619,173,849,1493,49,88,4,708,1084,370,1007,285,4,530,770,561,26,669,1100,30,876,649,178,32,354,621,911,334,514,9,449,1019,107

116
2021/day8/day8.erl Normal file
View File

@@ -0,0 +1,116 @@
%% to compile: erlc day8.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day8).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' ->Input;
Line -> read_input(IO, lists:reverse([Line|Input]))
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> parse_line(Line)
end.
parse_line(Line) ->
[string:tokens(X, " \n") || X <- string:split(Line, "|")].
solution1(Input) ->
lists:foldl(fun([_,X], Acc) -> count_unique(X, Acc) end, 0, Input).
solution2([]) -> 0;
solution2([[I|[O]]|T]) ->
[One] = find_length(2,I),
[Seven] = find_length(3,I),
[Four] = find_length(4,I),
[Eight] = find_length(7,I),
Three = find_three(find_length(5, I), One),
Nine = find_nine(find_length(6, I), Four),
[Five, Two] = find_five_and_two(find_length(5, I), Three, Four),
[Six, Zero] = find_six_and_zero(find_length(6, I), Nine, One),
Key = [{lists:sort(One), 1},{lists:sort(Two), 2},{lists:sort(Three), 3},{lists:sort(Four), 4},{lists:sort(Five), 5},{lists:sort(Six), 6},{lists:sort(Seven), 7},{lists:sort(Eight), 8},{lists:sort(Nine), 9}, {lists:sort(Zero), 0}],
[{_, A},{_,B},{_,C},{_,D}] = [lists:keyfind(lists:sort(X), 1, Key) || X <- O],
(A * 1000 + B * 100 + C * 10 + D) + solution2(T).
count_unique([], V) -> V;
count_unique([H|T], V) ->
count_unique(T, V + count(length(H))).
count(2) -> 1;
count(4) -> 1;
count(3) -> 1;
count(7) -> 1;
count(_) -> 0.
find_length(_Len, []) -> [];
find_length(Len, [H|T]) when length(H) == Len -> [H|find_length(Len, T)];
find_length(Len, [_H|T]) -> find_length(Len, T).
find_three([H|T], One) ->
case length(minus(H, One)) of
3 -> H;
_ -> find_three(T, One)
end.
find_nine([H|T], Four) ->
case length(minus(H, Four)) of
2 -> H;
_ -> find_nine(T, Four)
end.
find_five_and_two(Fives, Three, Four) ->
N = lists:delete(Three, Fives),
find_five_and_two(N, Four).
find_five_and_two([H,T], Four) ->
case length(minus(H, Four)) of
3 -> [T, H];
2 -> [H, T]
end.
find_six_and_zero(Sixes, Nine, One) ->
N = lists:delete(Nine, Sixes),
find_six_and_zero(N, One).
find_six_and_zero([H,T], One) ->
case length(minus(H, One)) of
4 -> [T, H];
5 -> [H, T]
end.
minus(X, Y) ->
lists:foldl(fun(T, Acc) -> lists:delete(T, Acc) end, X, Y).
plus(X, Y) ->
lists:usort(X ++ Y).

200
2021/day8/input.txt Normal file
View File

@@ -0,0 +1,200 @@
dcagfb cbegda cabdf bagedf dcb cd dcbegaf aebcf bgdaf dgcf | dacebgf cbafd dbacf cdgf
dfgbc ceagbd egb cgdae eafcgd ecbgd eb aefbgcd bfaceg deba | baed ecabgd aebd cadfebg
acbd bagfec bedcf efbca dbfecag gadcfe dc dbcaef cde fbegd | deacfg bfagcde bgedf cde
dgec dfc fabgd bfcgae dfbcg baegdcf cd fbdace cdbefg becgf | cedg adfbecg badgf dgbfc
be bacedg dfacb fbge deagcf eba egacf gcebfa febac dbgacfe | befg be egbf aedgcbf
gafbd gbafce agced fdgae bcedgaf gdeacb efdc eaf fe cgeadf | cgdae fdec fcdeabg ef
cdaefgb cfadbe gc afgcbe afedcg faecb cabg egc bfgde bfgce | fbaedc gc abgc bfcea
egcafb adgecf caebf fdgbaec fbaed cebg facbgd cfe fgabc ec | fbgca deagfc ce ce
fcdab deb cdbfea fecb befcdga abecd gdcae efbgda fcdgba be | cfbad dacge gbdeaf bed
afdecg gfcadb ecadfbg facgd ed egda efbgc ecd cdegf bfadec | becgf fcbeg de cgefb
caedbg gcfdba bfdae ge beagfdc bedag gcabd egac gcefbd deg | fdbecg acge bgfacd abefd
befad ecgad adgefcb febg gb afdcbe dcbfag beafgd dbg eagdb | feabd aedfb gbd aebdcf
bcfde acbfed dcaf dbcae gacbfe fc dgbcae egbacfd fcb edbfg | gbdfe gbeafc bcf cebfga
cgbade adcbgfe fb efgbad gbcfde aecfd edgba afbg bfeda fbe | ecafbdg bfdea fgab edfbgc
cbfa gefbd bcfedga bagefc ab bga ecdgab gbaef gfacde fcage | acdegf gab cegadb gab
cafbeg dfaebc fdca dgcbe eacbf fde debgfca egabfd dfceb fd | df eabcf egbdc dfe
egcdfa ebdac fdaecbg ed fdacb agecb cbdgaf ecd efdb dbaecf | dcabf cbeag ebdf ecgba
gacdebf decfbg afbdg dcagbf cg agdbc agcf ebcad abgedf bcg | badec dcbga cgfa abcde
fgcad dfebga gdbef ec ebcd fbcgea ceg fgedc defgbac dcfgbe | ce gbdfe fbcgdea fcgbdea
cgbef afcbeg dgbec ebafcd cgbfeda adbge cd ecd gfcd fgbdec | edc gcebd fegacdb cd
bafdcg egfdc bcfge dgeabf cd cgbadfe agefd facegd eadc cgd | cgd dcg defbagc cfdbega
degfcb fdg edabgf cadge fg gfcb cdeafbg bcefd dgfec beadcf | dgcbfe cabfed fecbd cgdfbea
dbfaec bfcgae cdb cdaf defbcg abgde ceabf adceb decgafb cd | fcda bdc dc afgecb
cead efcdg adbcfeg fagdc def acegfd fcbagd cbgef ed fgdaeb | ecad fcaedg cgadf cfaged
egdbfa cbgefad dgacbf cb dfbgec gefbd cbf gefbc gcefa bdec | ebcd ecgdbf fgebc cfb
fagbe acbged bf eagcf dgaebcf gbf febadg deabg cdagbf fbed | fbcdag fgeca abgdcf cdbage
baefc febgadc fcag dbgec feg gacfbe gfecb afedgb fg edafcb | aegdfb feabc agbfed fabged
gfedcb eadgcf fed befdagc gaedf bgdea bdafcg fe acdfg feca | bcfdga cagfd afce efca
cbfed degfba fgdbac acdg fdcab ac acgebf agdfb bac gcbfdea | cbdegfa caefgbd ca gdbcafe
dbcgea dc dcg cadf abgcdf efgdb afgbc gefacdb fbcdg agebfc | dabgce gbceda dgc dc
ef dgfbc cefbg gabecfd agceb feg gbcadf dfbe fcdgeb dfagec | gfe cafegd dfeb fe
ebgdc egdacb dfbcega dfe dbfge bagef ecdbfg bdcf df fgecad | dbgce fd cfdb edf
bdage ab fbgaecd aefb bfadeg gefad cdgeb agcbdf gba fgcdae | cgdbfa befa dgefca acebfdg
aecb dbgecf eagfcb ecdbfag fdegba geacf gebfa cge cgdfa ec | acgfe gec cabe fgcda
acd cafg abedcgf cfebda efcbgd cegfd ca cdgafe agcde bgeda | egcfbda gcfdae gefacd dca
dcefga gdefa db fbaed abefdgc faebc fbgaed bdf gbed dcafgb | bfd db db gbdfae
befac abcd bdcfe ecd cd baegfc edfgca adfbce aebcdgf edbfg | cgedfab feabc cd dabc
eacdgf cebgdfa acgebd ecdba dbcfea fdgcb dag ga acgdb abge | ag ga adg ag
gca fagbe dfageb bagce ecgfab gfce gc dcfeabg fadbcg bedac | afbdceg gadfeb cga acbdfg
gbad febac dea gadbecf fdegbc edfacg da bdecg cdbea bdcgae | dbag cdeabg gcdaeb adbg
cdgfeb cbeaf adef cfa fa aecbg fedcb acdebf bdcfga cdefbga | fdea eadf efad caefb
aedgb df cgbaed gdef bfcgda bfd efbac dgfeab debfa aegcbdf | acbfe egfd afebgd gefd
badcfe fcbg bf dfecbga faged gfadbc dfb dfgab abdcg dbceag | bf fb dafbgec fbd
bdae decgbf afcbge bdecfa afcbd edgfcab ebcaf bd dfacg dbc | ebda db cbd cbd
acfgde ae eac agbdec gadcb ebgcafd baed gfcbe cgabe bfgadc | cea fecdga ebgdac eac
cbedga cfb adcbe bfae afcdgeb cgdaf fcedab bf fcbdeg dacbf | dfceba fcdga fbc fcebda
cdafg dfgeac fc dbgcef gadbc gacfebd dcf edfgab gefad caef | fadgc facged adgefb ecgbdaf
gdcbf eabdgf gaefd fbdge afcedg bacgfe fagbedc dbea efb be | ecagfb dgaef be eb
gbecad faced abgedf cd cbfd abedcf dacgbfe cde deabf agcfe | cd edbfac cgabed daebfg
cgebaf dacbge gafed eadbgf dgcef ad fadgbce dea bgafe dafb | fegad dgeaf ad gdfae
ecfadb eagdbc afbcdge abfcgd cf bgedf agfc cfd gbfdc bcagd | fcga fcd befgd gdbfc
afcbg gfeb gfcad ecgdba cbf cafbeg bacge fb ebdfac baedcfg | fbc gabce egabc fcadg
agd gadbc da agcfed gcebad dgfcb ecabg ebcagdf eadb becfag | fgbeadc bcega geacb gceab
gaebf gfdbce aefbgd fde aefcbg ed dcaebfg agde aefdb acfdb | efd agcdebf cbgefd ebgfa
cdgfe ecbafd ebgca ad gedbcfa cad adbg cedabg egcad fcebga | gbedac adgb ecdgab daceg
efcb fcdag begdfa egcdf edf fe gfcdeb cdbega geacdbf becdg | dagfc gbadef decbg fe
bgaed agfdc fdgea fae fabcgd gcfe fe decfga afgedcb facbed | gdafe ef eacfbgd fe
ebgfca def fd ecgfbd efbda eabdg edgcfba afdc ecbfa bfdaec | abcefd fd dafc debcagf
eacbg efbdgca fdbga gde abecdg beagd cdeafg de cebd ecagbf | gaedbc gdcbafe gaecdf bgdeac
bafce cedb edacgfb fbgac fbe aedfgb be cbedfa cdefa eacfgd | be acbfg gfbac edbc
dgba cbgfdea gbe fbaegc cbedga fegdc decafb cdbge bg daecb | cadfbe bgad beadgc bgedc
abd fdbcae aedfb daec ad bacfdg dgefcb agbef cbefd cbagdef | ad dab acde da
cdagbe dagbe fageb gdfbeca gdcba edac dgcfba egd egcfbd ed | fbgcad cafdbg de facbged
dbecf egadfb cfagde da fcebga cagef dae fadec dagc dfbcaeg | da dgac bfedc ad
efa adcf fgcde acgeb bgedfa efcga degfbc aegcfdb fa efdgac | cfgea fedgc ecagf efdacgb
fecga eafgd acgefb cf gceab fbcdag gecdafb cabedg fbec gcf | agcbfd gfc cgf ceagf
egcaf ecdafgb dfgea cbafe dfebgc bgac cbagef cg gfc ecdfab | edbfgca edafg cgf gfc
afdcgb decgb fbdegca fbgae fdbaeg dgfbe gbafce df fdb efda | gcbed dafe gbfdca aefgb
gaebd baefg cfadbg fb gaedfbc gadbef cfgea bfde baf gacdeb | abf cdbagf aecbdg fagec
cfdb dcg geadbf febdg edfabcg dc bgecd eabcg fcgead cgbfde | dgc dc bdfge dcfebga
dfbcaeg fdgcae gfdab bagc gabcdf fecbgd cadgf gb fbade bdg | bdg gacb bgac ecfbdg
eg befdg fceabd badfg dbecf egbcfa bedgcf gbacefd egb dceg | beg faecdb gfdbe dbacef
cdagef edfca dgfa fca fa agedc gfcabe bdfec bdcage fbcaedg | caedbfg ecafgbd fdcega decgfba
gfc cfeb cafgdb fc aedgbfc cgdfe degfb aedcg fbegcd dfgbea | acdge fedbg agedc ebgdfa
aegdcb gde dgbfca eg bdecfga fedbgc gcbfd fgecd bfge acefd | ge debcag bgedcf bfeg
gbd eagbc cbefagd gcde aefdb gcebad cgdafb dg aegbd facbge | cdeg cged ebdaf gbd
eagbcf cba aebd cdfga cdegbf afcbedg dcbfe ab adfcb acfbde | bcfeag fgdcbe daeb bdae
gc dabcfg dcfageb acg ecabf bgadef facge gfeda ecdg fagced | gcde efdga agc fgaed
bcgdea bacfdg aeg cdabg dfeagc befgd aebc dabfgec ae degba | ebfdg egadcf aceb gbdac
ceadf defagb fg agbed dfgaebc bfaecg eacdbg afgde efg fgbd | gf egbda fabegc badfgec
begcfd fdecb fbdga ca bace cfadb egfdca cda cfaedb efabdcg | gacefd fdgba fcdebg bcae
bcga agbefc fageb ca afc debfc dafegc fdgeab abfdgec fecab | gdafceb befac fdecga ac
fce adcfg cgdefa bgcfda fdae acegf fe cgbae adgfceb cdegfb | gedacbf fdebgac deaf afde
agcf dbegf bga cfeba gfdebca fgaeb cbdefa ag egadcb cbfega | fbdge gcabef cagf ebdafc
aedg ge decbga bafce ecg gcdab degfcab gcfdeb ceabg abdcfg | cbfea ecg bafec eacfb
gfdceb cgb bcdfe gebdc egcabdf egcf bafcgd bgdae edabfc cg | gbc bgdfca cgfe gc
bg bedcf dgfaec fdbgc gcbafed fbg bfaged gadcf cgab acbgfd | agfbde bg bgcdf fgb
gafcb cdfgab abfcge cbgde bdcfae ea faeg dbgcafe ace agebc | decbaf fbacde bcagf ace
bgfdac cgabdef efdca fagce febagc cd fcd dfeacg gdce aefdb | dc dgcfba ecdagf gfacbe
bde eb egfbdc ebgfd fbdgc daebgc cagfbd gfbceda efbc aedfg | cgebadf becf cfbdg cfbedg
ebcdfag fadec daceg gbecaf eabcfd fd fad aebfc fcgbad efbd | dcefbag fceagb fdecabg adf
efgd fd bfagcd acefb febad gdbaef edcgba badge dbgaefc daf | eafbd gbcdfa gdfe edgba
cefdag bfagec bedcg da acd bcfead bdgcefa gcaef acdeg agfd | geacbf faegdc fcdgbea eacgfdb
bcedgaf bgfae cab bdcg cb gcade egdacf abdegc agbec becfad | gfcadbe adgbec gebfcda cfaedg
gf edbcafg cfge gdafe gdf cdabfg eabdg dgefac acfed fbceda | geabd cbgdfa ceadbf agbfdc
fcbgde fbged cgfdb gdebcaf cfge ebg deabf baedcg ge dfabcg | beadf ge dcbgef cfeg
eafdcb gcdfbea eag dgcba gacdef agebd dbefag deabf ge fgbe | fcbdea bgcdeaf dbgac fagcebd
dc fgebad ecd aedcfb cdga ebcagd dfbaecg adebg debcg gbecf | bcegd gcdeb dacg eagdb
cad feabgd fadegc bgcdfea gbcfa bgcda bgaced decb dc ebadg | acd adfbge cd dc
af cfbge fadecg afbgc dbaf gbdfac bdgeacf dbcga cfa gebcda | fa dabcg dafb gbceda
fbgdea gcefa agfdb fecdba egbd fdgea de bagfcd gdcbafe dae | ed eda abdefc fadgeb
fdbcga fdbae bfa adfcge gcbfaed bfeg fdage eacbd fgbaed fb | bdcfage fbeg eadfbg gbef
cfdbga fdeab gefbad bcaefd egad fdaegbc ag gcefb afg agefb | abfgecd egda bgcfe adefb
fbeda ae dae bdgfe gcadbf beac fedabc badfc ceagfd dgcbafe | bfcad gfabdc aed fbdac
fgcaed bagcef bdfc edabg efacd ebcadf fb afb abefd agfbdce | cfdb fb abf abf
fed bfaedc fceab edabg bfdea ecdagf begfdac df fceabg bfcd | cebgfa df eagbd gafced
egfdc fcg cadg dfebc dfebcga cg dgabef ebgacf dfgea gcfade | fbedc gcfed dcag fgc
fcbde fbeacgd afedb da fcegda dabg ebgaf dgfaeb dea febagc | da bfdce egfacb ad
fcdeg cdbe fgcab dceagf febcg eb cebdgf geb edfgab dbgcaef | abgcfed bdec gdcfe cfgdbe
degfb ebdacf edbfcg agdfce dbf bd bgcd dgfce gfdcbae abegf | gebfcd fgeab gecdfb gdafcbe
egacd abedg decfgba dagceb ca cabe cag dagfcb badfeg cegfd | gca caeb cfgde ca
gfa ebafd fg gafdbec gaecfb gbdfa edfg gaedbf bdgca ecadfb | fbagec fecabd caegbf fg
dfgce cgfbe cefad bfdace fcaged cdg badceg fdceagb dg afdg | gcfbe dg gadf edfabc
dfgbc cbfeda bdgcfe fdb efbg bf gcedb fdaecgb gdecba fgadc | bgdafec bgcde fbgdc agdfc
fgecd dbegfac adbfgc fa bdcage acegb eacgf efab fca fceabg | gafce af gdcbea af
fgdacb fbdec adb gfda acbdegf edbcga cfgba da cbgefa cadfb | gbcfea bgcfda da dab
fbcdg abfcdg cfegbda ed dbcaef gedcfb aecgb dcegb ced egdf | cabfgde egcdfb fbcade dgcfab
dbec fcd cegbdf dc fdacgeb abdfg baefcg gbfcd dafgce gfbec | dcf cfd fcd agefcb
fcdeg gdefb cfgadbe fb bef bfcg edfacb fecdgb gcdfea gadeb | efb feb fbcg dabge
acebd dacfg bdf agdcbfe gcafdb fcegad afgdbe bcfg bfcad bf | dbf bdaec bf febacdg
acdegf bdfa afdebg febcga gaefb fd fed dgefcab bedgf edbcg | efgdab aecdgf cedgbfa def
ecdfabg dfge cedfba fcadgb fgabd ebgfad fea egafb bcgae ef | efgdbca eagcfdb defg bfgda
efacgd bfda bcadg gab gbceadf ab cfbdga gadfc bcgfae dgbec | fgecad dbaf ba abfgdec
bcgaed fbgc edcfg dgbecf fgdae dgc cg abdcef gfcbdea ebfdc | cg cdfeb cg edbcfag
cagb fgcdeab gdebaf fcegd ecabfd acfeg ag afceb fga acgbfe | ga agf bcadfe fag
fbgde efbagc ecgd efdgabc ebdcf fdcgbe abfdg fcbead egb eg | dcge cedbgf fbgad acegfbd
febadcg cfdbge bgcef gecd ed gbdaf bfdeg deb gfabce fcbead | efdbca ed fgcbe cebdaf
eagbc cgde cafbg abdge fbdgae gacedb aec ec acefbgd abcefd | bgacf cgbfeda agbed dgec
efgbdca gfdb cadbfg dag dg degfac bcdaef gdacb bfcad agebc | gda dfagecb gcfdea dag
gefab bfde acbfgde gfdac ebfgad bfgeca aegbcd eda gfade de | gafbce gcfeabd aefgd fgbcae
gaecfb cdebgf fec ebcafgd bgecad cf debgc bfead ebfdc cgdf | cdbfe gfcd egbdca edfcgb
cfdeba bfgd gcedfa gbdae dgefcab cbega dg febadg agd bdaef | dfgb adg gecdfa fcebda
dea bgdeaf da befdc gdcfaeb ecdbga abecd gacd bgafce eagcb | aedcb facebg abfecdg bagefdc
gcebf dfbae dbgcfe egdc dgebcaf febcd bcd cagfeb fdgcba dc | bcd efcbd daefb agbdfce
fa acged bdfec afeg gcfadb bfdagce ecgafd caf acdfe dgaecb | debfc af afge adegc
bacfdg fadbe egbdfc ba bage acefd baf bgdafe fgebd gdacbef | dbfage bgedf aedgbf acedf
acgb dgcbfa dba edgbfac acdefb begdf bdgfa dceafg dcagf ba | abd dfbge abd gacb
gcdea gc agfdce egdfa egcf fbeagd aecbd dgc fcebgad cagfbd | acebd egcf defag gfadceb
fab begadc ebcad dfbea gfcdab acdfgbe fa fedgb adcfbe feca | cbgead bfcdega fba gbadce
gb efcagd fgcad cfbg bedca abg fcgbda dgbcafe acbgd bdafeg | abcdgf bcgf cgfb bg
fbcagd fcdae beacgf gf agf baecdg fecag bgfe cagdbef gbcea | edbagfc ebfg gebf bgcae
cdgfea bdeacf aefcgb dbca beafc bcgdefa ebdgf ad defab ade | dbcgefa da ade bacd
bcgaedf badecg cfegb afceg dcbf egdbfa gcedb gbf bf fgcdbe | bagfed bdagfe fdbc gefcbd
cegba bgeda cgefa cbde fegadb dagcbe acfgdb cb cbg ecbdafg | cdegabf ebgda bfgade ebgac
afbdg gdabec gcafdeb fc cfdeba bcf fgebdc cefg gcdbf ecdbg | fecg cdbefg gdbfc gfce
bgdfa egbf bcdgfa fe dfcbea aef feadgb edcag dagef afdcegb | fea cbedfa acdfgeb dfgeab
eacgfd caegdb gbfecad bdfce edcfa dgaf fea fa egdac aegbcf | af fa edbcf gdeacbf
baefg fedabcg gbfac gacbfe cb dfageb dcfag cgb bcea cfdegb | cabe eabc gfacd fgdcabe
fgdeac cgd ebdfga agefdbc bfcegd cdgbf cd cfabg dceb befgd | cgd cfgab fdgeb efbgd
aedgbf cgebda dbcagf gbf gbefd gdecf gdfabec fb eabdg feab | bf fbea cefdagb adgbe
fcgea fbgedca gbecfa efbadc efgd fcagd df cdfaeg fdc acgdb | efgac fcd dfceba fegac
egdfac cab cadbf gbdaf bc eadcf dcebagf dbecag fbdace cebf | cedabfg cgbaed bc efacbdg
be abcged afcgb ebg bgefc gebfcd cgefd efdb gdeafc daecgbf | gabedc dbeafcg debf febcadg
dgebafc cefdbg db edfb fcgdb fdceg dgecfa bdc abcfg baecdg | fegdca baecdfg bcd bd
fbgead cfab cdbgefa agcbdf aedgc fgcbd fa fagcd ecgbdf gaf | adgcebf gaf gaf af
debagfc eb fcedba ebc cgdab efdgac befa eadcf edabc gdbefc | bcdagfe fbae ecadb eabf
gaedcb bfcgd bcegf bagdcf fd aedfgbc bfd cbdga cabefd adfg | ecfbda cbfgda cfbadg fbgcd
agdcfbe bcfega cbeag ge dacfeb facged bcaef gbdac gce efbg | feabc acfbdeg aefbc gce
febcdg egdacb bedcf debfacg abdfce ba bae fbeda gdfea acfb | eadbf ab ab eab
cdfbg dfbcae dcabe egba ag cgdfea abdcg aefcdgb cga decagb | defabc edacb cbfgd ag
fgac dfgec eag cfbdge bacde ag abdgef bagfdec dcgfae agcde | aecbdgf gcaf efcgd befcdg
de gecad fbcgae gcafd dge edfbcg eadb eacfdbg bcedag acbge | gabce fcegba bgfcead ed
gbacd dbefcg fdcgae edgca afdeg aecf egc gfaedb dcfagbe ce | feac acegd caefbdg aecf
egacbd aedbc dagbc fcdagb ec bacefdg ace dcge ecfagb febda | afecgb fgdbac bcade cea
ceadgb cgaedf bfdge cfg cf fdgbc dgabc cfdabg gdcebaf bacf | gebdf fdeacg dafbgec cgf
bgfda gdcfae afce bfecgd ac acdfg afebdgc dgefc bdcaeg cag | gdbfec gcedf gfdaceb cegdab
fa fgeabdc bfga faedb fad ebgdfa daceb fgbcde fgdeb dafcge | efdab gcdfeb fa gabf
ebfc fb aedbgc ebagdf fab cgfeab gcdfeba acbeg abcfg gfdca | gbfac bgfeac bfa bcfga
fagdec gadebc cg cabfegd agdfb cbge dbeac cdfabe adcbg cgd | acgbed dgfeabc fdeacb dgbacef
fgbdc gbedaf bdgaf dfc cbfadg gfceb degcfa dc bacd gbafcde | dcf egfdca cd cdegaf
fcb aebf bf cafgb efgabc cgadbe egbadfc egbca gcfebd acdgf | beacgfd gebfcd dfbgec bf
bfgcd deb bfacdg ed fcebgd baefg dbaecfg efdc dabecg fdgbe | de gebaf dbcgfe bde
facgdbe gda ebdfag gabdf da cagfeb gebaf deaf dbagec dcfbg | egfba dgebca aefd cgdbf
deabg eg gdfacb bdfea egd gebfdca ecdgab acge dacbg cefgbd | eg dgcbef agcdb fadbceg
cafbged feacb gbafd dc gfdc agdcbf fcabd deabgf cad acgedb | fdcba egbfdac cad dca
dfagce fbd eabdfc gdfeb ebgfa dfabcge fdgce cedfbg bcgd bd | cdbg agbcefd db db
fdagec fcg gf gbace efbgacd ecgbfd dfcea caefg dagf dbecaf | acdegfb fg gbdfaec fadg
edfag bgfced dga ag cfbgdea bfdcag bfged decfa afedbg bgae | abeg dgaef ag edfgb
dcaegb fageb cdga begda adbce gd dge gbecafd cabfed cgfebd | gdaebc ged egd dcga
cfb agebfc fegb fb adcef afbec ebgdca bafdgc gbcea fdbeagc | ebfg gabce fbc agfdceb
fdcega cbgfea febg agdebfc cfe afecb gbcae ef bcdfa acgedb | fbgdeac egbf acfgeb bfge
fbadge dfgebc fbgacde bg fdage afdcge cedba agbf bge dgbea | adgbe fgba gabf fceagd
cfbda bgedca fgdcba abcfe dc agbfd dbc fdgc facbged dagfeb | cd fbcda fdcab fdgc
cdefa fbgdce aebdg geacd ebcdgfa gc cdg baegdc febdag gbac | fdeca cg dgfbcae bcgfde
gcadeb ef ecbfga acbgfed fbdga bcade dafcbe dcfe deabf feb | fdegacb agdbf fgbedac fcdbaeg
adgc fac ca gdfcae cfdegb gcdef adcfbe dgefcba cagef abgef | gcadbfe feabdc cdefg bgdeafc
cfdeba dega fga gadfec cdfabeg aecbfg cbdfg ag faedc dfcag | ag ga acfed cebfad
efcgb feagdc gdfbea fadc ac cea gadbfce cfgae aedgbc geafd | fcgeabd cafeg cbadge ca
ebfda cfgbea cgdbef fbadgec dgcb cedfga dg fdbeg efcgb gfd | fgdeb bfgcde ebfdg dg
dcafg facgdb bgfa dbacf ba gdcefa ecfdb cfegdba bac acedbg | gcbdaf fbadc begdac dgcbaf
eagf agedbcf acdegb abg gcfba fcegb befgcd fbcda ag ebcfag | dbcegfa bfecg cafdebg faeg
fbgcd cba fgacdb gabcdef fbgdec gdabc gacf bafdec begad ac | eacbdf badge acb becfad
eacbd dabfec dbgfeac ag eagdb fcdbag bga agec dbfge bacged | agecdb bag gebcdaf cdbea
bfgcd bcdegfa fdabgc da efgca cfagd efbdca cda dgfecb bgda | acdebf badfcg abdg cafgd
efcbgd cadefg cfegd dcgba cafedgb cagfd gaf acef fa bedafg | agf fcedg gefdcb cfdge
afdceg dcaf fd dfg aefgc gdbecf cafbdeg dbage ecagfb deafg | adefgc bfdecga fdg cefag
bfdacg dagbf abdefcg acfg fbcda beafdc egdab cfdegb gfd gf | bcafgd efgcabd cfga dcaegfb
dbage dfagbe cadfeb gfeb bae fabgdc be bafdg cdgea bgdfaec | fedacbg eb dfcbea eab
gebfd cfedga begfa dagbfc fgeadcb ecdgbf bced edfgc dgb db | dbec gadcfb db aefbg
dbcg fbagc bcf bc cdaebgf aegbdf faecg fdcaeb cdafbg afbgd | adfbg fadebgc dgbafc febagd

93
2021/day9/day9.erl Normal file
View File

@@ -0,0 +1,93 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day3 solve
%%
-module(day9).
-export ([solve/0, solve/1, solve/2]).
-export ([read_input/0]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
read_input() ->
{ok, IO} = file:open("input.txt", 'read'),
Data = read_input(IO),
file:close(IO),
Data.
read_input(IO) ->
read_input(IO, []).
read_input(IO, Input) ->
case read_line(IO) of
'eof' -> Input;
Line -> read_input(IO, Input ++ [Line])
end.
read_line(IO) ->
case file:read_line(IO) of
'eof' -> 'eof';
{ok, Line} -> [ X - $0 || X <- Line, [X] /= "\n"]
end.
solution1(Table) ->
Low_points = low_points(Table),
lists:foldl(fun(C, Acc) -> Acc + 1 + get_value(C, Table) end, 0, Low_points).
solution2(Table) ->
Low_points = low_points(Table),
Basins = get_basins(Low_points, Table),
[B1,B2,B3|_] = lists:sort(fun(A, B) -> length(A) > length(B) end, Basins),
length(B1) * length(B2) * length(B3).
get_basins(Low_points, Table) ->
get_basins(Low_points, Table, []).
get_basins([], _Table, Basins) -> Basins;
get_basins([Low_point|Rest], Table, Basins) ->
Basin = [Low_point|get_up_slopes([Low_point], Table)],
get_basins(Rest, Table, [Basin|Basins]).
low_points([H|_] = Table) ->
X = length(H),
Y = length(Table),
Coords = lists:flatten([[{A,B} || A <- lists:seq(1,X)] || B <- lists:seq(1,Y)]),
lists:filter(fun(C) -> is_low_point(C, Table) end, Coords).
get_up_slopes(Coords, Table) ->
get_up_slopes(Coords, Table, []).
get_up_slopes([], _Table, Acc) -> Acc;
get_up_slopes([{X, Y}|Rest], Table, Acc) ->
V = get_value({X,Y}, Table),
Neighbours = [{X+1, Y},{X-1, Y},{X, Y+1},{X, Y-1}],
Up_slopes = lists:filter(fun({X1,Y1}) -> C = get_value({X1,Y1}, Table), C /= 9 andalso C > V end, Neighbours),
get_up_slopes(lists:usort(Rest ++ Up_slopes), Table, lists:usort(Acc ++ Up_slopes)).
is_low_point({X,Y}, Table) ->
V = get_value({X,Y}, Table),
V < get_value({X+1, Y}, Table) andalso
V < get_value({X-1, Y}, Table) andalso
V < get_value({X, Y+1}, Table) andalso
V < get_value({X, Y-1}, Table).
get_value({X,Y}, [H|_] = Table) when X == 0; Y == 0; Y > length(Table); X > length(H) -> 9;
get_value({X,Y}, Table) ->
lists:nth(X, lists:nth(Y, Table)).

100
2021/day9/input.txt Normal file
View File

@@ -0,0 +1,100 @@
7659991098999876579910129879999876432123459874345567890126678999876588975767899323456989767899432101
8998789987898765467891239868899876541012398765123456789234567897643467894656798912399878948678944212
9867678976789878598954398756789997632343459873234569898765679999856578943547987893989865434567894323
7654567895896989679767499847994398755456579987656778969876892198767989652129876889876976725678965734
8767678954345698799898987659943219876577694598787889545987931019879996543298965679965987438789876799
9898789921239799898989899798794399987688965679898993534598942123989987654987654567894596549899989987
4969999892398989987876789987689989998789898789979992123989653934598798965976543678943987678999999876
3459898789497878976545678986567878999895679898768989239879869896789659879865432469432198799998789765
2498765699976567995434389765434567899934989977655679356965998789896545989865321258921019999987678954
3987654398765456789321238979325456799329898766434798999876797698987432198754310347894329789876567893
4696543219876967998910147998214345678998789954323987689999977567898543479885541456789498678988678932
5987654423987878987521236987601234567891678893219876567898765479987654567976632367899987569899789321
6798767834698989997432345698524568698932456789398765466989876567898765689987545478959768456789893210
7899898945679499876545656997434578799843569892987654345678989679999876799798658569349654367878964322
8977999498789398987896769876545689998767678931999869656789698789999987987698767895498743212567895433
9656789329898987899929879987676790199898989949877998767896559899878998977569978999987654323458986654
8797995434987896989434989798989891987919499896765569888921434998767789865452989998998795434569997765
9979899549876785678945995639698999876329398764354456999990125899545678954321299987899987895678949878
9865678998765434567899894324567892985498999863212347898989436798434599876210389996789999998789434999
7654567899896647978989789212459921296987899954393478987678945987324879965341567895679891019998959865
8543878901987656789765678901268933459876999895989569658567959876412568897432467894599789923987898654
9212389919898767897654577892999545998765798789878978945456899954323456789576578923989679899876789543
9543457898769879986543456789889959876543987676567899432345789976436567897697989219878565678965678932
8754568987654989765432347898767899989432976543456964321012498987545679998989892109867434699764567891
9867689298543299986541034987656789998743989654677895632134987898696789989878789298754324789543488989
1978792129654569987732129874543567897654799965789976853239876799989999878765678999865455697601245678
0989893098765678998653298763212375789795679878994989966398754889879898765464569899976566789212346789
9898954239987789998784987654301234699989789989873398765459765678968789884323456789987677894323456898
8777895345698999899895698976214345789878999997762129876589896789345678965434567999898989976564568967
7656976457899019767987899765423456789569899876543299987678987891234569876545898998769394987875679456
6546899568992198657898939878534677893456789987654989898789398932347678999856789889843212398986989345
5435688979879932545679929989665788902569991298779878789899299543458789798767895678932101569997890123
4323567899767891434589898998776899543457899999898768655978987654569899679878934569643219878998921254
6764579987658910123456797879887899656568978789987653234567998785678998532989545678954399989999432765
7875689998767891234567896569998998967989767698798767145678999896789987643498756789765989992987543876
8976796899878932545678997998769467899899854599659898657789985959897898784569867899876978931098954987
9697895799989873467889989999652348999799965678943939768999664543956789895679878979989867892129895698
4598954569899964578999878987643499997689978789432129879998543212345678976989989459899756789298789789
3499543798798765699998767898984987843579899996583235989987632105468789987899992398789898999987678991
4985432987679876789999658989876986532498798889874346798798743236589893498989891987678989459986568990
9876521296567987899876545878989875421987656778965498987669654587679999999876789898547678998765456789
9876432987678998998765434569998767410996545567896569896556965689789987898865676789435589765432369999
3987543498799549769886325679987654329877434456789698789439879789899976987764545678923459879321287899
4599656789989432458997212568999769498764321298999987688956989897999899876743236789212398998932456998
9798767897678921367989323459998998999875210147899876567897891956789798765432145794301987897893569997
8999878996569432349876534569987687898654321236789767456789932345679659976545012689419876896789878986
7899989987458943499998765678976546789985434545678954345679543567789545987983234568998765645678989565
6789199654347896589989876789987434579876545758789543237789654579895434499874356789329874234567893434
5679298789756789678976987899874323459987859767899654345678965989965421298765667895499932125689932123
4568999898969898789765698998765212398498869878998765656789879898965432399876878976987891034567894034
3456789987898999899894329987654323987349978989999898767896999797896743987987899989896789123678985125
2346899876767892999989212398765499876567989999899959878934987676989659876798945698765695434679876789
1256998765458993498979903459876989987698999999789943989949898455678998765759899987654989545689989899
4349879876569989986567894967989878999789989897678892099898789334569987674545678998743478957897696999
5478968987678979765456999898998767879899876789546789298788699212989876543234567998932569768998565678
6568956998789569876567897789999857867998975695437995987657598909898995432123459886521678989895434567
7678939879892458987678956699896645456987764789567894696543467898767986321019598765432789496789323878
8789998765901456798789545598775435349876543999698943495432356789656997432198969876547892345689439989
9895987654312345789899434459654324234987875898789212989321234896549876545997656989856921234579998997
6954398767433566789998921398743210125698986789894309878540145789432997859876543398767890123467897686
5695999876544678998787892987654341234589987895999498765431236894320989767987432129898921294878987575
4989899987698789987656789398765492395678998934998989896549898965999878979876421012969939989999098464
2976789998789899876543789249876989989989239019887678987656789879878767898985432123457898679889198323
9895678999894968987654590123989879978990129198764569998987894998765459987899543434568987546778987634
8654547899923656798985891294598768969893298998765678999598912349821398795698976546699876435569876545
6543336789012349899876789989987656756789987899976789989499909496543989654567898687987665523456987676
7652125978923598987987899878996541234567896569899899978987898987859878965678998789876563212345698787
6543234567894987876798998769876532345978943456799998767896987598998969898799989898765432101234569898
7685346878999876765689987655989645567899212566778987658965398459987856789899978999876543212385789999
8876798989998765434569976743498756789956401234568998789876999349876546899988769899987654525678999999
9987899697989954323798765632359867899543212345679239899989899956997635789879656789999769434789898989
4599996546567893212987654321235978998764637566989139999998789899986523998967545698999898945699787678
3499989435457899433498765435348989689895547677891098988997655798765439897645634567899976896789676567
2989978921345678994569876546757894578987678788992987677893234569898698765430125698999865689896543456
9879868935458789989778987987868943989998989999789987566989123456989789876321234789997684578965432123
8965657899599999878989998999979659899879596545678976435778934569879894987432545679876543467896673294
7654545698989997569999999998989799789965432434789897324567895979964933498545789789987432456998784989
6543234987978965478999899987899987656974321023498788212379976798763212379676899899997544567899999878
5432129876568896567898789656789976549875432164589654323568987987654343456987895978999655778956798967
4321019987456789678987698943495987678987543765678965454569998998765454567898954567898776789345987954
5432998765345698989996587992976798789798765897789876875678999869876569878979943459979987893212986543
6549879876234767899985456789897899997659897899897987996789998754997878989767892598965398994323497632
7698765438123456789876323496789992198943998943956998987899987643298989997656999987890139989434598745
8899954321014567899865212345678989989894989012345899498989998759109499998747988976789239878965987656
9998765732125678999954345489789679878789876543456789349678939998912349876434567895678949867896798767
4349876653346789998769497679898998765699998754678991234569019887893498765325658934589998758659999878
4239999778659899899898989989967987854897899869789893965678998766799987654312349898699896549237899989
9398999889767998789987978995459876543786789878898789896799987655678998895401256789798765432126789997
8987899999879987678976567894398765432645699989997678789929876543767899986212868999899654321034599896
7856789432989876569875456894298764321237989899986565678910987432347678997343479756998789532123456789
6545678921098765454986587932129879854349876799867434568924596541234599987654569545689898743454567895
5436889932129854323697698943234988765698765987654523567895987762345789598968678934578999654765678954
4321968894298765634598789954445699878987654599763212348997898943579893459899789545678998765878789875
5872456789349878745679897895768789989877653459894103456789999874568932598789897676789129878989899986
8763878995467989856789976979879899998765432345989294567899899865679943987699998989899234989299999899
7654567896568995977891234568999978919876521239878989678956756978989894976568999195978976790199899788
8765698987878923988910123456789567923987432399765679789432347989998769876456789234567897892988698677
9878789498999219899321256587995456894898643989813478997643458994987656987567894345679998999876545556
2989893219879998765432347898932345995798759876524567899856969543499897897698987457989899298765434345
1099954523467899876545456999545476789899899976435788923987897652101998998789876567897654349854321237