// File: /workspace/github_cpp_makefile_star2_O2/Windfisch[P]production-flow/read_factory.cpp
// Function: read_factory(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
Factory read_factory(string file)
{
	ifstream f(file);
	if (!f.good())
		throw runtime_error("could not open file '"+file+"'");
	
	Factory factory;
	string line;

	// parse facilities
	while (getline(f, line))
	{
		if (line == "#") break;

		size_t p1 = line.find(' ');
		if (p1 == string::npos)
			throw runtime_error("invalid format");
	
		if (p1 == line.length()-1)
		{
			vector<Factory::FacilityConfiguration> upgrade_plan;
			Factory::FacilityConfiguration conf;
			upgrade_plan.push_back(conf); // push an empty configuration. this is a splitter node

			factory.facilities.emplace_back(upgrade_plan);
		}
		else
		{
			size_t p2 = line.find(' ', p1+1);
			if (p2 == string::npos)
				throw runtime_error("invalid format");

			size_t p3 = line.find('/', p2+1);
			if (p3 == string::npos)
				throw runtime_error("invalid format");

			string recipe = line.substr(p1+1, p2-p1-1);
			double current = stod(line.substr(p2+1, p3-p2-1));
			double maximum = stod(line.substr(p3+1));

			cout << "recipe '" << recipe << "' with " << current << "/" << maximum << endl;

			vector<Factory::FacilityConfiguration> upgrade_plan;
			
			for (int i=0; i<5; i++)
			{
				Factory::FacilityConfiguration conf;
				
				for (auto iter : recipes.at(recipe))
					conf.production_or_consumption[iter.first] = int(SCALING_FACTOR * iter.second * (current + (maximum-current)*i/5.));
				upgrade_plan.push_back(conf);
			}

			factory.facilities.emplace_back(upgrade_plan);
		}
	}

	if (line != "#")
		throw runtime_error("invalid format: could not find edge list");

	while (getline(f, line))
	{
		size_t p1 = line.find(' ');
		if (p1 == string::npos)
			throw runtime_error("invalid format");
		size_t p2 = line.find(' ',p1+1);
		if (p2 == string::npos)
			throw runtime_error("invalid format");
		size_t p3 = line.find(' ',p2+1);
		if (p3 == string::npos)
			throw runtime_error("invalid format");

		int from = stoi(line.substr(0, p1));
		int to = stoi(line.substr(p1+1,p2-p1-1));
		string item = line.substr(p2+1,p3-p2-1);
		string dist_x = line.substr(p3+1);

		bool exists = (dist_x.substr(dist_x.length()-1) != "x");
		double dist = stod(dist_x);

		cout << "transport of " << item << " " << from << "->" << to << ", distance " << dist << " (line exists=" << exists << ")" << endl;

		vector<Factory::TransportLineConfiguration> upgrade_plan;

		upgrade_plan.emplace_back(Factory::TransportLineConfiguration{int(SCALING_FACTOR * 1*13.3), dist}); // one yellow
		upgrade_plan.emplace_back(Factory::TransportLineConfiguration{int(SCALING_FACTOR * 2*13.3), dist}); // two yellow
		upgrade_plan.emplace_back(Factory::TransportLineConfiguration{int(SCALING_FACTOR * 3*13.3), 4*dist}); // red+yellow
		upgrade_plan.emplace_back(Factory::TransportLineConfiguration{int(SCALING_FACTOR * 4*13.3), 4*dist}); // red+red
		upgrade_plan.emplace_back(Factory::TransportLineConfiguration{int(SCALING_FACTOR * 5*13.3), 15*dist}); // blue+red
		upgrade_plan.emplace_back(Factory::TransportLineConfiguration{int(SCALING_FACTOR * 6*13.3), 15*dist}); // blue+blue

		factory.transport_lines.emplace_back(item_lookup.at(item), from-1, to-1, upgrade_plan);
	}

	return factory;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2e8, %rsp            # imm = 0x2E8
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xe0(%rsp), %r14
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x3350
movq (%r14), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x100(%rsp,%rax)
jne 0x9e30
movl $0x90, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x3140
leaq 0x90(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x50(%rsp), %r12
leaq 0x80(%rsp), %r14
pushq $0x20
popq %rax
leaq 0x48(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r15, 0x30(%rsp)
movl %eax, %r15d
leaq 0xe0(%rsp), %rdi
movq %r14, %rsi
callq 0x3070
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x9940
movq %r14, %rdi
leaq 0x4380(%rip), %rsi      # 0xd9c4
callq 0xa4b8
testb %al, %al
jne 0x9940
movq %r14, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x3290
cmpq $-0x1, %rax
je 0x9d91
movq %rax, %rbx
movq 0x88(%rsp), %rax
decq %rax
cmpq %rax, %rbx
jne 0x96e1
andq $0x0, 0xb0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xa0(%rsp)
andl $0x0, 0x50(%rsp)
andq $0x0, 0x58(%rsp)
movq %r12, 0x60(%rsp)
movq %r12, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xa4c6
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0xa4f2
movq %r13, %rdi
callq 0x4a70
movq %rbx, %rdi
callq 0x49aa
jmp 0x9618
leaq 0x1(%rbx), %r12
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x3290
cmpq $-0x1, %rax
je 0x9dc6
movq %rax, %r15
leaq 0x1(%rax), %rdx
movq %r14, %rdi
pushq $0x2f
popq %rsi
movq %rdx, 0x28(%rsp)
callq 0x3290
movq %rax, (%rsp)
cmpq $-0x1, %rax
je 0x9dfb
notq %rbx
addq %r15, %rbx
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x3230
notq %r15
addq (%rsp), %r15
movq %r13, %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
movq %r15, %rcx
callq 0x3230
leaq 0x50(%rsp), %r12
movq %r13, %rdi
xorl %esi, %esi
callq 0xa52b
movsd %xmm0, 0x38(%rsp)
leaq 0xa0(%rsp), %rbx
movq %r13, %rdi
callq 0x3418
movq (%rsp), %rdx
incq %rdx
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
pushq $-0x1
popq %rcx
callq 0x3230
movq %r13, %rdi
xorl %esi, %esi
callq 0xa52b
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
callq 0x3418
movq 0xb812(%rip), %rdi      # 0x14fc8
leaq 0x4218(%rip), %rsi      # 0xd9d5
callq 0x3200
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0x4207(%rip), %rsi      # 0xd9de
callq 0x3200
movq %rax, %rdi
movsd 0x38(%rsp), %xmm0
callq 0x3340
movq %rax, %rdi
leaq 0x3bf2(%rip), %rsi      # 0xd3e6
callq 0x3200
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x3340
movq %rax, %rdi
callq 0x3110
andq $0x0, 0xd0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movsd 0x8(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r15d, %r15d
cmpl $0x5, %r15d
je 0x990a
andl $0x0, 0x50(%rsp)
andq $0x0, 0x58(%rsp)
movq %r12, 0x60(%rsp)
movq %r12, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0xba32(%rip), %rdi      # 0x15298
movq %rbx, %rsi
callq 0xa544
movq %rax, %r14
movq 0x18(%rax), %rbx
cvtsi2sd %r15d, %xmm0
addq $0x8, %r14
mulsd 0x8(%rsp), %xmm0
divsd 0x40cc(%rip), %xmm0     # 0xd958
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
cmpq %r14, %rbx
je 0x98e2
movups 0x20(%rbx), %xmm0
movaps %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa58a
movsd (%rsp), %xmm0
mulsd 0x4097(%rip), %xmm0     # 0xd960
mulsd 0x28(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, (%rax)
movq %rbx, %rdi
callq 0x3190
movq %rax, %rbx
jmp 0x9898
leaq 0xc0(%rsp), %rdi
movq %r13, %rsi
callq 0xa4c6
movq %r13, %rdi
callq 0x4a70
incl %r15d
leaq 0xa0(%rsp), %rbx
jmp 0x983a
movq 0x30(%rsp), %rdi
leaq 0xc0(%rsp), %r14
movq %r14, %rsi
callq 0xa4f2
movq %r14, %rdi
callq 0x49aa
movq %rbx, %rdi
callq 0x3418
leaq 0x80(%rsp), %r14
pushq $0x20
popq %r15
jmp 0x9618
leaq 0x407d(%rip), %rsi      # 0xd9c4
leaq 0x80(%rsp), %rdi
callq 0xa5d8
testb %al, %al
jne 0x9e95
movq 0x30(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x38(%rsp)
leaq 0x80(%rsp), %r13
leaq 0x48(%rsp), %r14
leaq 0xe0(%rsp), %rdi
movq %r13, %rsi
callq 0x3070
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x9cc1
movq %r13, %rdi
pushq $0x20
popq %rbp
movl %ebp, %esi
xorl %edx, %edx
callq 0x3290
cmpq $-0x1, %rax
je 0x9cf2
movq %rax, %r15
leaq 0x1(%rax), %rdx
movq %r13, %rdi
movl %ebp, %esi
movq %rdx, (%rsp)
callq 0x3290
cmpq $-0x1, %rax
je 0x9d27
movq %rax, %rbx
leaq 0x1(%rax), %rdx
movq %r13, %rdi
movl %ebp, %esi
movq %rdx, 0x8(%rsp)
callq 0x3290
cmpq $-0x1, %rax
je 0x9d5c
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rbp
movq %r13, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x3230
movq %r14, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0xa5e2
movl %eax, 0x28(%rsp)
movq %r14, %rdi
callq 0x3418
notq %r15
addq %rbx, %r15
movq %r14, %rdi
movq %rbp, %rsi
movq (%rsp), %rdx
movq %r15, %rcx
callq 0x3230
movq %r14, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0xa5e2
movl %eax, %r15d
movq %r14, %rdi
callq 0x3418
notq %rbx
addq %r12, %rbx
movq %r14, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rcx
callq 0x3230
incq %r12
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
pushq $-0x1
popq %r12
movq %r12, %rcx
callq 0x3230
movq %rbp, %r13
movq 0xa8(%rsp), %rdx
decq %rdx
leaq 0xc0(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x3230
movq %rbx, %r12
movq %rbp, %rdi
leaq 0x3f55(%rip), %rsi      # 0xda0f
callq 0xa5d8
movl %eax, %ebx
movq %rbp, %rdi
callq 0x3418
movq %r12, %rdi
xorl %esi, %esi
callq 0xa52b
movsd %xmm0, (%rsp)
movq 0xb4e9(%rip), %rdi      # 0x14fc8
leaq 0x3f2b(%rip), %rsi      # 0xda11
callq 0x3200
movq %rax, %rdi
movq %r14, %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0x3af2(%rip), %rsi      # 0xd5f2
callq 0x3200
movq %rax, %rdi
movl 0x28(%rsp), %esi
callq 0x33b0
movq %rax, %rdi
leaq 0x3648(%rip), %rsi      # 0xd163
callq 0x3200
movq %rax, %rdi
movl %r15d, %esi
callq 0x33b0
movq %rax, %rdi
leaq 0x3eea(%rip), %rsi      # 0xda1f
callq 0x3200
movq %rax, %rdi
movsd (%rsp), %xmm0
callq 0x3340
movq %rax, %rdi
leaq 0x3eda(%rip), %rsi      # 0xda2b
callq 0x3200
movzbl %bl, %esi
movq %rax, %rdi
callq 0x3390
movq %rax, %rdi
leaq 0x3bc6(%rip), %rsi      # 0xd731
callq 0x3200
movq %rax, %rdi
callq 0x3110
andq $0x0, 0xd0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movl $0x33f4, 0x10(%rsp)     # imm = 0x33F4
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x10(%rsp), %rbp
movq %rbp, %rsi
callq 0xa5fe
movl $0x67e8, 0x10(%rsp)     # imm = 0x67E8
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa5fe
movl $0x9bdc, 0x10(%rsp)     # imm = 0x9BDC
movsd (%rsp), %xmm0
mulsd 0x3d7c(%rip), %xmm0     # 0xd968
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa5fe
movl $0xcfd0, 0x10(%rsp)     # imm = 0xCFD0
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa5fe
movl $0x103c4, 0x10(%rsp)    # imm = 0x103C4
movsd (%rsp), %xmm0
mulsd 0x3d39(%rip), %xmm0     # 0xd970
movsd %xmm0, (%rsp)
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa5fe
movl $0x137b8, 0x10(%rsp)    # imm = 0x137B8
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa5fe
leaq 0xb656(%rip), %rdi      # 0x152c8
movq %r14, %rsi
callq 0xa678
movl 0x28(%rsp), %ecx
decl %ecx
movl %ecx, 0x10(%rsp)
decl %r15d
movl %r15d, 0x44(%rsp)
movq 0x38(%rsp), %rdi
movq %rax, %rsi
movq %rbp, %rdx
leaq 0x44(%rsp), %rcx
movq %rbx, %r8
callq 0xa636
movq %rbx, %rdi
callq 0x48fe
movq %r12, %rdi
callq 0x3418
movq %r14, %rdi
callq 0x3418
jmp 0x9977
leaq 0x80(%rsp), %rdi
callq 0x3418
leaq 0xe0(%rsp), %rdi
callq 0x30a0
movq 0x30(%rsp), %rax
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %r14
leaq 0x3cc2(%rip), %rsi      # 0xd9c6
movq %rax, %rdi
callq 0x30e0
movq 0xb2dd(%rip), %rsi      # 0x14ff0
movq 0xb2a6(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33a0
jmp 0x9ec5
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %r14
leaq 0x3c8d(%rip), %rsi      # 0xd9c6
movq %rax, %rdi
callq 0x30e0
movq 0xb2a8(%rip), %rsi      # 0x14ff0
movq 0xb271(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33a0
jmp 0x9ec5
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %r14
leaq 0x3c58(%rip), %rsi      # 0xd9c6
movq %rax, %rdi
callq 0x30e0
movq 0xb273(%rip), %rsi      # 0x14ff0
movq 0xb23c(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33a0
jmp 0x9ec5
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %r14
leaq 0x3c23(%rip), %rsi      # 0xd9c6
movq %rax, %rdi
callq 0x30e0
movq 0xb23e(%rip), %rsi      # 0x14ff0
movq 0xb207(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33a0
jmp 0x9ec5
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %r14
leaq 0x3bee(%rip), %rsi      # 0xd9c6
movq %rax, %rdi
callq 0x30e0
movq 0xb209(%rip), %rsi      # 0x14ff0
movq 0xb1d2(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33a0
jmp 0x9ec5
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %r14
leaq 0x3bb9(%rip), %rsi      # 0xd9c6
movq %rax, %rdi
callq 0x30e0
movq 0xb1d4(%rip), %rsi      # 0x14ff0
movq 0xb19d(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33a0
jmp 0x9ec5
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %r14
leaq 0x3b6c(%rip), %rsi      # 0xd9ae
leaq 0x80(%rsp), %rdi
movq %rbx, %rdx
callq 0xa442
leaq 0x3b69(%rip), %rdx      # 0xd9c2
leaq 0x48(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xa423
movb $0x1, %bpl
leaq 0x48(%rsp), %rsi
movq %r14, %rdi
callq 0x3360
xorl %ebp, %ebp
movq 0xb16c(%rip), %rsi      # 0x14ff0
movq 0xb135(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33a0
jmp 0x9ec5
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %r14
leaq 0x3b3f(%rip), %rsi      # 0xd9e6
movq %rax, %rdi
callq 0x30e0
movq 0xb13a(%rip), %rsi      # 0x14ff0
movq 0xb103(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33a0
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3418
jmp 0x9eda
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x80(%rsp), %rdi
callq 0x3418
testb %bpl, %bpl
jne 0x9efb
jmp 0xa00b
jmp 0x9fa8
jmp 0x9f68
movq %rax, %rbx
movq %r14, %rdi
callq 0x31b0
jmp 0xa00b
jmp 0x9fa8
jmp 0x9f68
jmp 0x9fa8
jmp 0x9f68
movq %rax, %rbx
jmp 0x9fda
jmp 0x9f25
jmp 0x9fad
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3418
jmp 0x9fe7
jmp 0x9fa8
jmp 0x9f68
jmp 0x9fad
jmp 0x9fa8
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4a70
leaq 0xa0(%rsp), %rdi
callq 0x49aa
jmp 0x9ff4
jmp 0x9fa8
jmp 0x9f68
jmp 0x9fa8
jmp 0x9f68
jmp 0x9fa8
movq %rax, %rbx
movq %r14, %rdi
callq 0x31b0
jmp 0x9ff4
jmp 0x9fa8
jmp 0x9f85
jmp 0x9f85
jmp 0x9f85
jmp 0x9f85
jmp 0x9f85
jmp 0x9f85
jmp 0x9f85
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x48fe
jmp 0x9fb9
jmp 0x9fb6
jmp 0x9fa1
jmp 0x9fa8
jmp 0x9fa1
jmp 0x9fa8
movq %rax, %rbx
jmp 0x9fc6
jmp 0x9fa8
movq %rax, %rbx
jmp 0x9ff4
movq %rax, %rbx
jmp 0x9fe7
jmp 0x9fcd
jmp 0x9fcd
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x3418
leaq 0x48(%rsp), %rdi
jmp 0x9fef
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4a70
leaq 0xc0(%rsp), %rdi
callq 0x49aa
leaq 0xa0(%rsp), %rdi
callq 0x3418
leaq 0x80(%rsp), %rdi
callq 0x3418
movq 0x30(%rsp), %rdi
callq 0x46c2
leaq 0xe0(%rsp), %rdi
callq 0x30a0
movq %rbx, %rdi
callq 0x33c0