Advent of Code Solutions
advent-of-code aoc

2015: Day 1

yemou.pink 5e753416 29581d10

verified
Changed files
+54
2015
+1
.gitignore
··· 1 + input-real
+53
2015/01/solution.erl
··· 1 + #!/usr/bin/env escript 2 + 3 + % --- Part 1 Code 4 + 5 + part1(Input) -> 6 + io:format("Part 1: ~b~n", [destination(0, Input)]). 7 + 8 + destination(Floor, Input) -> 9 + case {_, Rest} = next_char(Input) of 10 + {"(", _} -> destination(Floor + 1, Rest); 11 + {")", _} -> destination(Floor - 1, Rest); 12 + {"\n", _} -> Floor 13 + end. 14 + 15 + % --- Part 2 Code 16 + 17 + part2(Input) -> 18 + io:format("Part 2: ~b~n", [basement_enter(0, 0, Input)]). 19 + 20 + basement_enter(Position, Floor, Input) -> 21 + if 22 + Floor == -1 -> 23 + Position; 24 + true -> 25 + case {_, Rest} = next_char(Input) of 26 + {"(", _} -> basement_enter(Position + 1, Floor + 1, Rest); 27 + {")", _} -> basement_enter(Position + 1, Floor - 1, Rest); 28 + {"\n", _} -> halt(1) % If we get here, our puzzle input is impossible 29 + end 30 + end. 31 + 32 + % --- Generic Helper Functions 33 + 34 + next_char([Char | String]) -> {[Char], String}. 35 + 36 + read_input() -> read_input(""). 37 + read_input(PrevLine) -> 38 + case io:get_chars("", 8192) of 39 + eof -> PrevLine; 40 + Data -> read_input(PrevLine ++ Data) 41 + end. 42 + 43 + main(Args) -> 44 + Input = read_input(), 45 + case Args of 46 + ["1"] -> 47 + part1(Input); 48 + ["2"] -> 49 + part2(Input); 50 + _ -> 51 + part1(Input), 52 + part2(Input) 53 + end.