Skip to content

Commit

Permalink
[2022][day 20] add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
IAjimi committed Dec 20, 2022
1 parent f4d07c4 commit ba1a5ad
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
89 changes: 89 additions & 0 deletions 2022/AOC20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from typing import List

from _utils import read_input, timer, Solution

DECRYPTION_KEY = 811589153


class Node:
def __init__(self, val: int, prev: "Node" = None, next: "Node" = None):
self.val = val
self.prev = prev
self.next = next

def __repr__(self):
return f"Node({self.val}, prev={self.prev.val}, next={self.next.val})"


def process_input(filename: str, decrypt_key: bool) -> List[Node]:
_input = read_input(filename)
if decrypt_key:
_input = [Node(int(val) * DECRYPTION_KEY) for val in _input]
else:
_input = [Node(int(val)) for val in _input]

for i, node in enumerate(_input):
_input[i].prev = _input[(i - 1) % len(_input)]
_input[i].next = _input[(i + 1) % len(_input)]
return _input


def mix_file(_input: List[Node]) -> List[Node]:
n_nodes = len(_input)

for i in range(n_nodes):
node = _input[i]
if node.val == 0:
continue

node.prev.next, node.next.prev = node.next, node.prev # remove node
new_prev_node, new_next_node = node.prev, node.next

move = node.val % (n_nodes - 1)
for _ in range(move):
new_prev_node = new_prev_node.next
new_next_node = new_next_node.next

node.prev, node.next = new_prev_node, new_next_node # update node pointers
new_prev_node.next, new_next_node.prev = node, node
return _input


def grove_coordinates(nodes: List[Node]) -> int:
node = nodes[0]
while node.val != 0:
node = node.next

solution = 0
for i in range(3_000 + 1):
if i in (1_000, 2_000, 3_000):
solution += node.val
node = node.next

return solution


def part1(filename: str) -> int:
nodes = process_input(filename, decrypt_key=False)
nodes = mix_file(nodes)
return grove_coordinates(nodes)


def part2(filename: str) -> int:
nodes = process_input(filename, decrypt_key=True)
for i in range(10):
nodes = mix_file(nodes)
return grove_coordinates(nodes)


@timer
def main(filename: str) -> Solution:
part_1_solution = part1(filename)
part_2_solution = part2(filename)
return part_1_solution, part_2_solution


if __name__ == "__main__":
part_1_solution, part_2_solution = main("aoc20.txt")
print(f"PART 1: {part_1_solution}") # 19070
print(f"PART 2: {part_2_solution}") # 14773357352059
3 changes: 2 additions & 1 deletion 2022/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ This repository contains Python solutions to the 2022 edition of [Advent of Code
| [16](https://adventofcode.com/2022/day/16) | **Proboscidea Volcanium** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC16.py) |
| [17](https://adventofcode.com/2022/day/17) | **Pyroclastic Flow**: Tetris emulator | [:star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC17.py) |
| [18](https://adventofcode.com/2022/day/18) | **Boiling Boulders**: Exploring 3D grid | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC18.py) |
| [19](https://adventofcode.com/2022/day/19) | **Not Enough Minerals** | [:star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC19.py) |
| [19](https://adventofcode.com/2022/day/19) | **Not Enough Minerals** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC19.py) |
| [20](https://adventofcode.com/2022/day/20) | **Grove Positioning System** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC20.py) |

0 comments on commit ba1a5ad

Please sign in to comment.