adventofcode2022/day23/toivy.py

43 lines
833 B
Python
Executable File

#!/usr/bin/env python3
"""
Converts a text file containing a list of integers
into a format that can be loaded by ivy.
"""
import sys
if sys.argv[1:]:
filename = sys.argv[1]
else:
filename = "input"
varname = filename.split(".",1)[0]
with open(filename) as f:
lines = f.read().splitlines()
data = []
for line in lines:
line = line.replace("-", ",")
data.append(line.split(","))
def count(row):
n = 0
for x in row:
if x.isdigit():
n += 1
else:
n += len(x)
return n
cols = max(map(count, data))
print("{} = {} {} rho".format(varname, len(data), cols), end="")
for row in data:
print("", " ".join(
x if x.isdigit() else repr(x)
for x in row), end="")
n = count(row)
if n < cols:
print(" 0" * (cols - n), end="")