| @@ -0,0 +1,18 @@ | |||
| def main(): | |||
| """ For each depth in the list, determine whether it's deeper than the previous depth. | |||
| There is no previous depth for the first entry. | |||
| Output the total number of times the depth increased. | |||
| """ | |||
| with open("aoc1-1.txt", "r") as file: | |||
| depths = file.readlines() | |||
| depths = [int(el.strip()) for el in depths] | |||
| prev = 999999999 | |||
| increases = [] | |||
| for depth in depths: | |||
| increases.append(1 if depth > prev else 0) | |||
| prev = depth | |||
| print(sum(increases)) | |||
| if __name__ == "__main__": | |||
| main() | |||
| @@ -0,0 +1,30 @@ | |||
| def main(): | |||
| """ Consider three-depth windows instead of individual depths: | |||
| 199 A | |||
| 200 A B | |||
| 208 A B C | |||
| 210 B C D | |||
| 200 E C D | |||
| 207 E F D | |||
| 240 E F G | |||
| 269 F G H | |||
| 260 G H | |||
| 263 H | |||
| For each window in the list, determine whether it's deeper than the previous window. | |||
| There is no previous depth for the first entry. | |||
| Output the total number of times the depth increased. | |||
| """ | |||
| with open("aoc1-1.txt", "r") as file: | |||
| depths = file.readlines() | |||
| depths = [int(el.strip()) for el in depths] | |||
| prev = 999999999 | |||
| increases = [] | |||
| for i in range(len(depths)-2): | |||
| window = depths[i] + depths[i+1] + depths[i+2] | |||
| increases.append(1 if window > prev else 0) | |||
| prev = window | |||
| print(sum(increases)) | |||
| if __name__ == "__main__": | |||
| main() | |||
| @@ -0,0 +1,27 @@ | |||
| def main(): | |||
| """ | |||
| horizontal position and vertical position both start at 0 | |||
| forward X increases forward (horizontal) position by X units | |||
| down X increases depth (vertical position) by X units | |||
| up X decreases depth (vertical position) by X units | |||
| After following all instructions multiply final h_pos by final v_pos | |||
| """ | |||
| h_pos, v_pos = 0, 0 | |||
| instructions = [] | |||
| with open("aoc2-1.txt", "r") as file: | |||
| instructions = file.readlines() | |||
| for inst in instructions: | |||
| dir, amt = inst.strip().split(" ") | |||
| amt = int(amt) | |||
| if dir == "forward": | |||
| h_pos += amt | |||
| elif dir == "down": | |||
| v_pos += amt | |||
| elif dir == "up": | |||
| v_pos -= amt | |||
| else: | |||
| raise ValueError(f"Unrecognized direction: {h_pos}") | |||
| return (h_pos * v_pos) | |||
| if __name__ == "__main__": | |||
| print(main()) | |||
| @@ -0,0 +1,29 @@ | |||
| def main(): | |||
| """ | |||
| horizontal position, vertical position, and aim both start at 0 | |||
| forward X increases forward (horizontal) position by X units | |||
| AND changes depth by (X * aim) units | |||
| down X increases aim (vertical angle) by X units | |||
| up X decreases aim (vertical angle) by X units | |||
| After following all instructions multiply final h_pos by final v_pos | |||
| """ | |||
| h_pos, v_pos, aim = 0, 0, 0 | |||
| instructions = [] | |||
| with open("aoc2-1.txt", "r") as file: | |||
| instructions = file.readlines() | |||
| for inst in instructions: | |||
| dir, amt = inst.strip().split(" ") | |||
| amt = int(amt) | |||
| if dir == "forward": | |||
| h_pos += amt | |||
| v_pos += (amt * aim) | |||
| elif dir == "down": | |||
| aim += amt | |||
| elif dir == "up": | |||
| aim -= amt | |||
| else: | |||
| raise ValueError(f"Unrecognized direction: {h_pos}") | |||
| return (h_pos * v_pos) | |||
| if __name__ == "__main__": | |||
| print(main()) | |||
| @@ -0,0 +1,30 @@ | |||
| import sys | |||
| def main(): | |||
| with open("aoc3-1.txt", "r") as file: | |||
| inputs = [el.strip() for el in file.readlines()] | |||
| digits = [[] for _ in range(len(inputs[0]))] | |||
| for j, ip in enumerate(inputs): | |||
| # print(digits, ip) | |||
| for i, dg in enumerate(ip): | |||
| # print(i, dg) | |||
| dg = dg.strip() | |||
| if dg != "": | |||
| digits[i].append(int(dg)) | |||
| # if j >= 2: | |||
| # sys.exit(0) | |||
| gamma, epsilon = [], [] | |||
| for el in digits: | |||
| gamma.append(1 if el.count(1) >= el.count(0) else 0) | |||
| epsilon.append(0 if el.count(1) >= el.count(0) else 1) | |||
| gamma_str = "".join([str(el) for el in gamma]) | |||
| epsilon_str = "".join([str(el) for el in epsilon]) | |||
| gamma_int = int(gamma_str, 2) | |||
| epsilon_int = int(epsilon_str, 2) | |||
| print(f"Gamma: {gamma_str} -- {gamma_int}") | |||
| print(f"Epsilon: {epsilon_str} -- {epsilon_int}") | |||
| print(f"Consumption: {gamma_int*epsilon_int}") | |||
| if __name__ == "__main__": | |||
| main() | |||
| @@ -0,0 +1,52 @@ | |||
| def common_digit(list_of_lists: list, position: int, most=True) -> int: | |||
| digits = [] | |||
| for ip in list_of_lists: | |||
| digits.append(int(ip[position])) | |||
| # print(f"1: {digits.count(1)}, 0: {digits.count(0)}", end="") | |||
| if not most: | |||
| return 0 if digits.count(1) >= digits.count(0) else 1 | |||
| return 1 if digits.count(1) >= digits.count(0) else 0 | |||
| def main() -> None: | |||
| with open("aoc3-1.txt", "r") as file: | |||
| inputs = [el.strip() for el in file.readlines()] | |||
| digits = [[] for _ in range(len(inputs[0]))] | |||
| for j, ip in enumerate(inputs): | |||
| for i, dg in enumerate(ip): | |||
| dg = dg.strip() | |||
| if dg != "": | |||
| digits[i].append(int(dg)) | |||
| gamma, epsilon = [], [] | |||
| for el in digits: | |||
| gamma.append(1 if el.count(1) >= el.count(0) else 0) | |||
| epsilon.append(0 if el.count(1) >= el.count(0) else 1) | |||
| gamma_str = "".join([str(el) for el in gamma]) | |||
| epsilon_str = "".join([str(el) for el in epsilon]) | |||
| gamma_int = int(gamma_str, 2) | |||
| epsilon_int = int(epsilon_str, 2) | |||
| print(f"Gamma: {gamma_str} -- {gamma_int}") | |||
| print(f"Epsilon: {epsilon_str} -- {epsilon_int}") | |||
| print(f"Consumption: {gamma_int*epsilon_int}") | |||
| oxy_inputs = [el for el in inputs] | |||
| co2_inputs = [el for el in inputs] | |||
| i = 0 | |||
| while len(oxy_inputs) > 1: | |||
| mcd = common_digit(oxy_inputs, i) | |||
| oxy_inputs = [el for el in oxy_inputs if el[i] == str(mcd)] | |||
| # print(f", MCD: {mcd}, len: {len(oxy_inputs)}") | |||
| i += 1 | |||
| i = 0 | |||
| while len(co2_inputs) > 1: | |||
| lcd = common_digit(co2_inputs, i, False) | |||
| co2_inputs = [el for el in co2_inputs if el[i] == str(lcd)] | |||
| # print(f", LCD: {lcd}, len: {len(co2_inputs)}") | |||
| i += 1 | |||
| oxy_str, co2_str = oxy_inputs[0], co2_inputs[0] | |||
| oxy_int, co2_int = int(oxy_str, 2), int(co2_str, 2) | |||
| print(f"Oxygen: {oxy_str} -- {oxy_int}") | |||
| print(f"CO2: {co2_str} -- {co2_int}") | |||
| print(f"Life support: {oxy_int*co2_int}") | |||
| if __name__ == "__main__": | |||
| main() | |||