66 lines
1.5 KiB
Python
66 lines
1.5 KiB
Python
|
import json
|
||
|
from sys import argv
|
||
|
from os import path, kill
|
||
|
from dataclasses import dataclass
|
||
|
|
||
|
|
||
|
@dataclass(frozen=True)
|
||
|
class Location:
|
||
|
row: int
|
||
|
col: int
|
||
|
|
||
|
|
||
|
def main():
|
||
|
input_file = argv[1]
|
||
|
output_file = argv[2]
|
||
|
|
||
|
if not test_file(input_file):
|
||
|
print("Input file does not exist")
|
||
|
kill(1)
|
||
|
if test_file(output_file):
|
||
|
print("Output file already exists")
|
||
|
kill(1)
|
||
|
|
||
|
locations = read_input_csv_to_wells(input_file)
|
||
|
json_obj = tubes_to_tubes_json(locations)
|
||
|
|
||
|
with open(output_file, 'w') as file:
|
||
|
json.dump(json_obj, file)
|
||
|
|
||
|
|
||
|
def read_input_csv_to_wells(p: str) -> [Location]:
|
||
|
with open(p, 'r') as file:
|
||
|
lines = file.readlines()
|
||
|
# Assume 96w format only
|
||
|
def get_first_col(line): return line.split(',')[0]
|
||
|
def get_row(x): return ord(x.upper()) - 64
|
||
|
|
||
|
def get_row_col(line: str) -> Location:
|
||
|
col = get_first_col(line)[1:]
|
||
|
row = get_row(get_first_col(line)[0])
|
||
|
return Location(row, int(col))
|
||
|
|
||
|
return list(set([get_row_col(line) for line in lines[1:]]))
|
||
|
|
||
|
|
||
|
def tubes_to_tubes_json(i: [Location]) -> dict:
|
||
|
target_positions_list = []
|
||
|
|
||
|
for tube in i:
|
||
|
target_positions_list.append((tube.row - 1) * 12
|
||
|
+ tube.col)
|
||
|
|
||
|
output_obj = {}
|
||
|
output_obj["TargetPositions"] = [{"PositionIndex": p}
|
||
|
for p in target_positions_list]
|
||
|
|
||
|
return output_obj
|
||
|
|
||
|
|
||
|
def test_file(p: str) -> bool:
|
||
|
return path.exists(p) and path.isfile(p)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|