Advent of Code Solutions
advent-of-code aoc
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

2015: Day 2

yemou 37c9375f ff06e6f1

+66
+2
2015/02/part1-tests/expected
··· 1 + 58 2 + 43
+1
2015/02/part1-tests/input1
··· 1 + 2x3x4
+1
2015/02/part1-tests/input2
··· 1 + 1x1x10
+2
2015/02/part2-tests/expected
··· 1 + 34 2 + 14
+1
2015/02/part2-tests/input1
··· 1 + 2x3x4
+1
2015/02/part2-tests/input2
··· 1 + 1x1x10
+58
2015/02/solution.erl
··· 1 + #!/usr/bin/env escript 2 + % --- Part 1 Code 3 + part1(Input) -> 4 + io:format("Part 1: ~b~n", [calculate_wpaper(0, Input)]). 5 + 6 + calculate_wpaper(Total, [Dimension | Dimensions]) -> 7 + [Length, Width, Height] = lists:map( 8 + fun(Num) -> 9 + {Int, _} = string:to_integer(Num), 10 + Int 11 + end, 12 + string:tokens(Dimension, "x") 13 + ), 14 + SurfaceArea = (2 * Length * Width) + (2 * Width * Height) + (2 * Height * Length), 15 + [MinOne, MinTwo, _] = lists:sort([Length, Width, Height]), 16 + calculate_wpaper(Total + (SurfaceArea + (MinOne * MinTwo)), Dimensions); 17 + calculate_wpaper(Total, []) -> 18 + Total. 19 + 20 + % --- Part 2 Code 21 + part2(Input) -> 22 + io:format("Part 2: ~b~n", [calculate_ribbon(0, Input)]). 23 + 24 + calculate_ribbon(Total, [Dimension | Dimensions]) -> 25 + [Length, Width, Height] = lists:map( 26 + fun(Num) -> 27 + {Int, _} = string:to_integer(Num), 28 + Int 29 + end, 30 + string:tokens(Dimension, "x") 31 + ), 32 + [MinOne, MinTwo, _] = lists:sort([Length, Width, Height]), 33 + Perimeter = (2 * MinOne) + (2 * MinTwo), 34 + CubicVolume = Length * Width * Height, 35 + calculate_ribbon(Total + (Perimeter + CubicVolume), Dimensions); 36 + calculate_ribbon(Total, []) -> 37 + Total. 38 + 39 + % --- Generic Helper Functions 40 + read_input() -> read_input(""). 41 + read_input(PrevLine) -> 42 + case io:fread("", "~s") of 43 + eof -> PrevLine; 44 + {error, _} -> halt(1); 45 + {ok, Terms} -> read_input(PrevLine ++ Terms) 46 + end. 47 + 48 + main(Args) -> 49 + Input = read_input(), 50 + case Args of 51 + ["1"] -> 52 + part1(Input); 53 + ["2"] -> 54 + part2(Input); 55 + _ -> 56 + part1(Input), 57 + part2(Input) 58 + end.