74 lines
1.8 KiB
Python
74 lines
1.8 KiB
Python
import json
|
|
from sys import argv, exit
|
|
from os import path
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Location:
|
|
row: int
|
|
col: int
|
|
|
|
|
|
def main():
|
|
input_file = argv[1]
|
|
output_file = argv[2]
|
|
alternate_mode = len(argv) == 4 and argv[3] == "alt"
|
|
|
|
if not test_file(input_file):
|
|
print("Input file does not exist")
|
|
exit(1)
|
|
if test_file(output_file):
|
|
print("Output file already exists")
|
|
exit(1)
|
|
|
|
locations = read_input_csv_to_wells(input_file)
|
|
json_obj = tubes_to_tubes_json(locations)
|
|
|
|
if alternate_mode:
|
|
with open(output_file, 'w') as file:
|
|
for location in locations:
|
|
index = (location.row - 1) * 12 + location.col
|
|
file.write(f"{index}\n")
|
|
exit(0)
|
|
|
|
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()
|