-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-12.py
216 lines (167 loc) · 6.21 KB
/
day-12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
"""Advent of Code Programming Puzzles
2019 Edition - Day 12
Puzzle Solution in Python
"""
import argparse
import logging
import math
import os
import sys
from itertools import permutations
from typing import Iterator
log = logging.getLogger(__name__)
# Common Methods ---------------------------------------------------------------
def load_contents(filename: str) -> Iterator[map]:
"""Load and convert contents from file
:param filename: input filename
:return: iterator yielding a list of integers
"""
lines = open(filename).read().strip().strip('<>').split(os.linesep)
for line in lines:
axis = [token.split('=') for token in line.strip('<>').split(',')]
axis = {name: int(value) for name, value in axis}
yield axis
def trace(step: int, positions: list[map], velocities: list[map]) -> None:
"""Trace values
:param step: step index
:param positions: bodies positions
:param velocities: bodies velocity
:return: nothing
"""
log.info(f'{step=}')
for i, pos in enumerate(positions):
vel = velocities[i]
log.info(f'{pos=}, {vel=}')
PERMUTATIONS = list(permutations(range(4), 2))
def compute_time_step(positions: list[map], velocities: list[map]) -> None:
"""Update positions and velocities
:param positions: bodies positions
:param velocities: bodies velocity
:return: nothing
"""
# bodies = range(len(positions))
# for ref, opp in permutations(bodies, 2):
# FIXME: iterate over each axis first
for ref, opp in PERMUTATIONS:
ref_pos = positions[ref].items()
opp_pos = positions[opp]
for axis, ref_val in ref_pos:
opp_val = opp_pos[axis]
if ref_val < opp_val:
velocities[ref][axis] += 1
elif ref_val > opp_val:
velocities[ref][axis] -= 1
for body, pos in enumerate(positions):
for axis in pos.keys():
pos[axis] += velocities[body][axis]
def step_by_axis(
positions: list[int], velocities: list[int]) -> None:
"""Update positions and velocities
:param positions: bodies positions
:param velocities: bodies velocity
:return: nothing
"""
for index, body in enumerate(positions):
velocities[index] += \
sum(1 if opp > body else -1 for opp in positions if opp != body)
for index, body in enumerate(positions):
positions[index] += velocities[index]
def compute_total_energy(positions: list[map], velocities: list[map]) -> int:
"""Compute total energy
:param positions: bodies positions
:param velocities: bodies velocity
:return: total energy
"""
total_energy = 0
for body, pos in enumerate(positions):
body_energy = sum(map(abs, pos.values()))
kin = velocities[body]
body_energy *= sum(map(abs, kin.values()))
total_energy += body_energy
return total_energy
# Solver Methods ---------------------------------------------------------------
def solve(contents: list[map], steps: int) -> int:
"""Part one solving method
:param contents: decoded contents
:param steps: number of steps to compute
:return: answer
"""
positions = contents
velocities = [{axis: 0 for axis in body.keys()} for body in positions]
for step in range(steps):
if not step % 10:
trace(step, positions, velocities)
compute_time_step(positions, velocities)
total_energy = compute_total_energy(positions, velocities)
return total_energy
def solve_part_two(contents: list[map]) -> int:
"""Part two solving method
:param contents: decoded contents
:return: answer
"""
def lcm(a: int, b: int) -> int:
return int((a * b) / math.gcd(a, b))
pos_per_axis = [[body[axis] for body in contents] for axis in contents[0].keys()]
cycles_per_axis = list()
for axis, positions in enumerate(pos_per_axis):
start_positions = positions.copy()
vel_per_axis = [0 for _ in range(4)]
step = 0
while not step or not all(body == 0 for body in vel_per_axis) \
or (positions != start_positions):
step += 1
step_by_axis(positions=positions, velocities=vel_per_axis)
cycles_per_axis.append(step)
answer = lcm(lcm(cycles_per_axis[0], cycles_per_axis[1]), cycles_per_axis[2])
return answer
# Support Methods --------------------------------------------------------------
EXIT_SUCCESS = 0
LOG_FORMAT = '# %(msecs)-3d - %(funcName)-16s - %(levelname)-8s - %(message)s'
def configure_logger(level: int):
"""Configure logging
:param level: verbosity level
:return: nothing
"""
logger = logging.getLogger()
logger.handlers = []
stdout = logging.StreamHandler(sys.stdout)
stdout.setLevel(level=logging.WARNING)
stdout.setFormatter(logging.Formatter(LOG_FORMAT))
logger.addHandler(stdout)
if level >= 2:
stdout.setLevel(level=logging.DEBUG)
logger.setLevel(level=logging.DEBUG)
elif level >= 1:
stdout.setLevel(level=logging.INFO)
logger.setLevel(level=logging.INFO)
def parse_arguments() -> argparse.Namespace:
"""Parse arguments provided by the command-line
:return: list of decoded arguments
"""
parser = argparse.ArgumentParser(description=__doc__)
pa = parser.add_argument
pa('filename', type=str, help='input contents filename')
pa('-p', '--part', type=int, help='solve only the given part')
pa('-v', '--verbose', action='count', default=0)
arguments = parser.parse_args()
return arguments
def main() -> int:
"""Script main method
:return: script exit code returned to the shell
"""
args = parse_arguments()
configure_logger(level=args.verbose)
log.info(f'Arguments: {args}')
compute_part_one = not args.part or 1 == args.part
compute_part_two = not args.part or 2 == args.part
contents = list(load_contents(filename=args.filename))
if compute_part_one:
answer = solve(contents=contents, steps=1000)
print(f'part one: {answer=}')
if compute_part_two:
answer = solve_part_two(contents=contents)
print(f'part two: {answer=}')
return EXIT_SUCCESS
if __name__ == "__main__":
sys.exit(main())