import sys
import csv
import re

def parse_file(filename):
    file_data = {}
    current_section = None
    with open(filename, 'r') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            if line.endswith(':'):
                current_section = line[:-1].strip()
                file_data[current_section] = {}
            elif line.startswith('['):
                if current_section is None:
                    continue
                match = re.match(r'^\[(\d+)\]:\s*(.*)$', line)
                if not match:
                    continue
                run_idx = int(match.group(1))
                tuples_str = match.group(2)
                tuples = []
                for tuple_str in tuples_str.split():
                    if tuple_str.startswith('(') and tuple_str.endswith(')'):
                        t_v_str = tuple_str[1:-1].split(',')
                        if len(t_v_str) != 2:
                            continue
                        try:
                            t = float(t_v_str[0].strip())
                            v = float(t_v_str[1].strip())
                            tuples.append((t, v))
                        except ValueError:
                            continue
                if tuples:
                    file_data[current_section][run_idx] = tuples
    return file_data

def process_file(file_data, writer):
    all_run_indices = []
    for section in file_data.values():
        all_run_indices.extend(section.keys())
    if not all_run_indices:
        return
    max_run = max(all_run_indices)
    for run_idx in range(max_run + 1):
        a_lf = None
        a_lf_section = file_data.get('A_lf', {})
        a_lf_tuples = a_lf_section.get(run_idx, [])
        if a_lf_tuples:
            a_lf = a_lf_tuples[-1][1]
        
        car_times = [None] * 4
        for i in range(4):
            section_name = f'crashed[{i}]'
            crash_section = file_data.get(section_name, {})
            crash_tuples = crash_section.get(run_idx, [])
            crash_time = None
            for t, v in crash_tuples:
                if v == 1:
                    crash_time = t
                    break
            car_times[i] = crash_time
        
        time_first_crash = None
        for ct in car_times:
            if ct is not None:
                if time_first_crash is None or ct < time_first_crash:
                    time_first_crash = ct
        
        row = [a_lf, time_first_crash] + car_times
        row = ['' if x is None else x for x in row]
        writer.writerow(row)

def main():
    if len(sys.argv) < 2:
        print("Usage: python script.py input_files...", file=sys.stderr)
        sys.exit(1)
    
    writer = csv.writer(sys.stdout)
    writer.writerow(['A_lf', 'time_first_crash', 'car0', 'car1', 'car2', 'car3'])
    
    for filename in sys.argv[1:]:
        file_data = parse_file(filename)
        process_file(file_data, writer)

if __name__ == "__main__":
    main()