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 3

yemou 0fd36abf 8768dc2b

+84
+3
2015/03/part1-tests/expected
··· 1 + 2 2 + 4 3 + 2
+1
2015/03/part1-tests/input1
··· 1 + >
+1
2015/03/part1-tests/input2
··· 1 + ^>v<
+1
2015/03/part1-tests/input3
··· 1 + ^v^v^v^v^v
+3
2015/03/part2-tests/expected
··· 1 + 3 2 + 3 3 + 11
+1
2015/03/part2-tests/input1
··· 1 + ^v
+1
2015/03/part2-tests/input2
··· 1 + ^>v<
+1
2015/03/part2-tests/input3
··· 1 + ^v^v^v^v^v
+72
2015/03/solution.erl
··· 1 + #!/usr/bin/env escript 2 + % --- Part 1 Code 3 + part1(Input) -> 4 + InitialPosition = {0, 0}, 5 + InitialMap = #{InitialPosition => 1}, 6 + io:format("Part 1: ~b~n", [length(maps:keys(process_instructions(InitialMap, InitialPosition, Input)))]). 7 + 8 + visit_house(HouseMap, Position) -> 9 + CurrentValue = maps:get(Position, HouseMap, 0), 10 + maps:put(Position, CurrentValue + 1, HouseMap). 11 + 12 + process_instructions(HouseMap, _, "\n") -> 13 + HouseMap; 14 + process_instructions(HouseMap, Position, Input) -> 15 + {CurrentX, CurrentY} = Position, 16 + NewPos = case {_, Rest} = next_char(Input) of 17 + {"^", _} -> {CurrentX, CurrentY + 1}; 18 + {"<", _} -> {CurrentX - 1, CurrentY}; 19 + {"v", _} -> {CurrentX, CurrentY - 1}; 20 + {">", _} -> {CurrentX + 1, CurrentY} 21 + end, 22 + process_instructions(visit_house(HouseMap, NewPos), NewPos, Rest). 23 + 24 + % --- Part 2 Code 25 + part2(Input) -> 26 + InitialPositionMap = #{santa => {0, 0}, robo_santa => {0, 0}}, 27 + InitialMap = #{{0, 0} => 2}, 28 + Mover = santa, 29 + io:format("Part 2: ~b~n", [length(maps:keys(process_instructions(InitialMap, InitialPositionMap, Mover, Input)))]). 30 + 31 + update_position(PositionMap, Mover, Movement) -> 32 + {CurrentX, CurrentY} = maps:get(Mover, PositionMap), 33 + case Movement of 34 + up -> maps:put(Mover, {CurrentX, CurrentY + 1}, PositionMap); 35 + left -> maps:put(Mover, {CurrentX - 1, CurrentY}, PositionMap); 36 + down -> maps:put(Mover, {CurrentX, CurrentY - 1}, PositionMap); 37 + right -> maps:put(Mover, {CurrentX + 1, CurrentY}, PositionMap) 38 + end. 39 + 40 + process_instructions(HouseMap, _, _, "\n") -> 41 + HouseMap; 42 + process_instructions(HouseMap, PositionMap, Mover, Input) -> 43 + NextMover = case Mover of santa -> robo_santa; robo_santa-> santa end, 44 + NewPosMap = case {_, Rest} = next_char(Input) of 45 + {"^", _} -> update_position(PositionMap, Mover, up); 46 + {"<", _} -> update_position(PositionMap, Mover, left); 47 + {"v", _} -> update_position(PositionMap, Mover, down); 48 + {">", _} -> update_position(PositionMap, Mover, right) 49 + end, 50 + process_instructions(visit_house(HouseMap, maps:get(Mover, NewPosMap)), NewPosMap, NextMover, Rest). 51 + 52 + % --- Generic Helper Functions 53 + next_char([Char | String]) -> {[Char], String}. 54 + 55 + read_input() -> read_input(""). 56 + read_input(PrevLine) -> 57 + case io:get_chars("", 8192) of 58 + eof -> PrevLine; 59 + Data -> read_input(PrevLine ++ Data) 60 + end. 61 + 62 + main(Args) -> 63 + Input = read_input(), 64 + case Args of 65 + ["1"] -> 66 + part1(Input); 67 + ["2"] -> 68 + part2(Input); 69 + _ -> 70 + part1(Input), 71 + part2(Input) 72 + end.