Advent of Code Solutions
advent-of-code aoc

2015: Day 6

yemou.pink bac7740b 3e6e013c

verified
Changed files
+119
2015
+5
2015/06/part1-tests/expected
··· 1 + 1000000 2 + 1000 3 + 999000 4 + 999996 5 + 0
+1
2015/06/part1-tests/input1
··· 1 + turn on 0,0 through 999,999
+1
2015/06/part1-tests/input2
··· 1 + toggle 0,0 through 999,0
+2
2015/06/part1-tests/input3
··· 1 + turn on 0,0 through 999,999 2 + toggle 0,0 through 999,0
+2
2015/06/part1-tests/input4
··· 1 + turn on 0,0 through 999,999 2 + turn off 499,499 through 500,500
+1
2015/06/part1-tests/input5
··· 1 + turn off 499,499 through 500,500
+2
2015/06/part2-tests/expected
··· 1 + 1 2 + 2000000
+1
2015/06/part2-tests/input1
··· 1 + turn on 0,0 through 0,0
+1
2015/06/part2-tests/input2
··· 1 + toggle 0,0 through 999,999
+103
2015/06/solution.erl
··· 1 + #!/usr/bin/env escript 2 + % --- Part 1 Code 3 + part1(Input) -> io:format("Part 1: ~b~n", [count_lit_lights(Input, #{})]). 4 + 5 + generate_coordinates({XStart, YStart}, {XEnd, YEnd}) -> 6 + [{X, Y} || X <- lists:seq(XStart, XEnd), Y <- lists:seq(YStart, YEnd)]. 7 + 8 + generate_map(Coords, Value) -> maps:from_list([{Coord, Value} || Coord <- Coords]). 9 + 10 + perform_instruction(toggle, Coords, Map) -> 11 + maps:merge_with( 12 + fun(_, Current, _) -> 13 + case Current of 14 + 0 -> 1; 15 + 1 -> 0 16 + end 17 + end, 18 + Map, 19 + generate_map(Coords, 1) 20 + ); 21 + perform_instruction(Action, Coords, Map) -> 22 + maps:merge( 23 + Map, 24 + case Action of 25 + on -> generate_map(Coords, 1); 26 + off -> generate_map(Coords, 0) 27 + end 28 + ). 29 + 30 + count_lit_lights([], Map) -> 31 + maps:fold(fun(_, Value, Accumulator) -> Accumulator + Value end, 0, Map); 32 + count_lit_lights([{Action, Start, End} | Instructions], Map) -> 33 + count_lit_lights( 34 + Instructions, 35 + perform_instruction(Action, generate_coordinates(Start, End), Map) 36 + ). 37 + 38 + % --- Part 2 Code 39 + part2(Input) -> io:format("Part 2: ~b~n", [calculate_brightness(Input, #{})]). 40 + 41 + perform_instruction2(off, Coords, Map) -> 42 + maps:merge_with( 43 + fun(_, Current, _) -> 44 + case Current of 45 + Current when Current > 0 -> Current - 1; 46 + Current -> 0 47 + end 48 + end, 49 + Map, 50 + generate_map(Coords, 0) 51 + ); 52 + perform_instruction2(Action, Coords, Map) -> 53 + maps:merge_with( 54 + fun(_, Current, New) -> Current + New end, 55 + Map, 56 + case Action of 57 + toggle -> generate_map(Coords, 2); 58 + on -> generate_map(Coords, 1) 59 + end 60 + ). 61 + 62 + calculate_brightness([], Map) -> 63 + maps:fold(fun(_, Value, Accumulator) -> Accumulator + Value end, 0, Map); 64 + calculate_brightness([{Action, Start, End} | Instructions], Map) -> 65 + calculate_brightness( 66 + Instructions, 67 + perform_instruction2(Action, generate_coordinates(Start, End), Map) 68 + ). 69 + 70 + % --- Read Input 71 + split_coordinate(Coordinate) -> 72 + [X, Y] = string:split(Coordinate, ","), 73 + {Xint, _} = string:to_integer(X), 74 + {Yint, _} = string:to_integer(Y), 75 + {Xint, Yint}. 76 + 77 + parse_line(Data) -> 78 + case string:split(string:trim(Data), " ", all) of 79 + ["toggle", Start, _, End] -> {toggle, split_coordinate(Start), split_coordinate(End)}; 80 + ["turn", "on", Start, _, End] -> {on, split_coordinate(Start), split_coordinate(End)}; 81 + ["turn", "off", Start, _, End] -> {off, split_coordinate(Start), split_coordinate(End)} 82 + end. 83 + 84 + read_input() -> read_input(""). 85 + read_input(Prev) -> 86 + case io:get_line("") of 87 + eof -> Prev; 88 + {error, _} -> halt(1); 89 + Data when Prev == "" -> read_input([parse_line(Data)]); 90 + Data -> read_input(Prev ++ [parse_line(Data)]) 91 + end. 92 + 93 + main(Args) -> 94 + Input = read_input(), 95 + case Args of 96 + ["1"] -> 97 + part1(Input); 98 + ["2"] -> 99 + part2(Input); 100 + _ -> 101 + part1(Input), 102 + part2(Input) 103 + end.