// File: Windfisch[P]production-flow/actiongraph.cpp
// Function: ActionGraph::dijkstra(Factory::FactoryConfiguration)
pair<Factory::FactoryConfiguration, double> ActionGraph::dijkstra(Factory::FactoryConfiguration initial_config)
{
	auto start_node = make_unique<ActionGraph::Node>();
	start_node->conf = initial_config;
	start_node->current_item_type = item_t(MAX_ITEM-1);
	start_node->total_cost = 0.;

	vector< unique_ptr< ActionGraph::Node> > openlist;
	vector< unique_ptr< ActionGraph::Node> > closedlist;
	openlist.push_back(move(start_node));

	while (!openlist.empty())
	{
		cout << "openlist has size " << openlist.size() << ", total expanded = " << openlist.size() + closedlist.size() << endl;

		// find and remove smallest element
		auto smallest = openlist.begin();
		for (auto it = openlist.begin(); it != openlist.end(); it++)
			if ((*it)->total_cost < (*smallest)->total_cost)
				smallest=it;
		
		closedlist.push_back(move(*smallest));
		auto& nodeptr = closedlist.back();
		*smallest = move(openlist.back());
		openlist.pop_back();

		cout << "inspecting item: " << nodeptr->current_item_type << ", ";
		cout << "nodes:";
		for (auto lvl : nodeptr->conf.facility_levels)
			cout << " " << lvl;
		cout << ", edges:";
		for (auto lvl : nodeptr->conf.transport_levels)
			cout << " " << lvl;
		cout << endl;


		// expand node
		auto successor_nodes = nodeptr->successors(factory);
		for (auto& successor : successor_nodes)
		{
			cout << "  -> successor item: " << successor->current_item_type << ", ";
			cout << "nodes:";
			for (auto lvl : successor->conf.facility_levels)
				cout << " " << lvl;
			cout << ", edges:";
			for (auto lvl : successor->conf.transport_levels)
				cout << " " << lvl;
			if (successor->current_item_type == DONE)
			{
				// we've found a goal state! :)
				cout << endl << "success, cost = " << successor->total_cost << ", expanded " << closedlist.size() + openlist.size() << " nodes" << endl;
				return pair<Factory::FactoryConfiguration, double>(successor->conf, successor->total_cost);
			}

			bool found = false;
			for (auto& openlist_node : openlist)
				if (openlist_node->equals(*successor, factory))
				{
					openlist_node = move(successor);
					cout << "; already in openlist" << endl;
					found = true;
					break;
				}
			if (!found)
				for (auto& closedlist_node : closedlist)
					if (closedlist_node->equals(*successor, factory))
					{
						found = true;
						cout << "; already in closedlist" << endl;
						break;
					}

			if (!found)
			{
				openlist.push_back(move(successor));
				cout << "; not seen yet, adding to openlist" << endl;
			}
		}
	}

	cout << "could not find a solution :(" << endl;
	return pair<Factory::FactoryConfiguration, double>(initial_config, -1.);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rdx, -0x198(%rbp)
movq %rdi, -0x1b8(%rbp)
movq %rdi, %rax
movq %rax, -0x1b0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1a8(%rbp)
leaq -0x20(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x17460
movq -0x1a0(%rbp), %rdi
callq 0x17280
movq -0x198(%rbp), %rsi
movq %rax, %rdi
callq 0x174c0
jmp 0x166a7
leaq -0x20(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x17280
movq -0x1c0(%rbp), %rdi
movl $0xa, 0x30(%rax)
callq 0x17280
movq $0x0, 0x38(%rax)
leaq -0x48(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x171e0
leaq -0x60(%rbp), %rdi
callq 0x171e0
movq -0x1c8(%rbp), %rdi
movq -0x1c0(%rbp), %rsi
callq 0x17500
jmp 0x16700
jmp 0x16702
leaq -0x48(%rbp), %rdi
callq 0x17530
xorb $-0x1, %al
testb $0x1, %al
jne 0x16716
jmp 0x17129
movq 0x228ab(%rip), %rdi     # 0x38fc8
leaq 0xf0f9(%rip), %rsi      # 0x2581d
callq 0x3290
movq %rax, -0x1d0(%rbp)
jmp 0x16732
leaq -0x48(%rbp), %rdi
callq 0x17570
movq -0x1d0(%rbp), %rdi
movq %rax, %rsi
callq 0x30a0
movq %rax, -0x1d8(%rbp)
jmp 0x16753
movq -0x1d8(%rbp), %rdi
leaq 0xf0cf(%rip), %rsi      # 0x25830
callq 0x3290
movq %rax, -0x1e0(%rbp)
jmp 0x1676f
leaq -0x48(%rbp), %rdi
callq 0x17570
movq %rax, -0x1f0(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x17570
movq -0x1f0(%rbp), %rsi
movq -0x1e0(%rbp), %rdi
addq %rax, %rsi
callq 0x30a0
movq %rax, -0x1e8(%rbp)
jmp 0x167a7
movq -0x1e8(%rbp), %rdi
movq 0x227eb(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x167bc
leaq -0x48(%rbp), %rdi
callq 0x17590
movq %rax, -0x68(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x17590
movq %rax, -0x70(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x17600
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x175c0
testb $0x1, %al
jne 0x167f9
jmp 0x1687d
leaq -0x70(%rbp), %rdi
callq 0x17630
movq %rax, %rdi
callq 0x17280
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0x1f8(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x17630
movq %rax, %rdi
callq 0x17280
movsd -0x1f8(%rbp), %xmm1
movsd 0x38(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x16867
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x16867
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x171ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x171bc
jmp 0x16869
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x17650
movq %rax, -0x80(%rbp)
jmp 0x167d6
leaq -0x68(%rbp), %rdi
callq 0x17630
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0x17500
jmp 0x16894
leaq -0x60(%rbp), %rdi
callq 0x17690
movq %rax, -0x88(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x17690
movq %rax, -0x210(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x17630
movq -0x210(%rbp), %rsi
movq %rax, %rdi
callq 0x176d0
movq -0x208(%rbp), %rdi
callq 0x17700
movq 0x226e2(%rip), %rdi     # 0x38fc8
leaq 0xef57(%rip), %rsi      # 0x25844
callq 0x3290
movq %rax, -0x200(%rbp)
jmp 0x168fb
movq -0x88(%rbp), %rdi
callq 0x17280
movq -0x200(%rbp), %rdi
movl 0x30(%rax), %esi
callq 0x34e0
movq %rax, -0x218(%rbp)
jmp 0x1691f
movq -0x218(%rbp), %rdi
leaq 0xecc4(%rip), %rsi      # 0x255f1
callq 0x3290
jmp 0x16934
movq 0x2268d(%rip), %rdi     # 0x38fc8
leaq 0xef14(%rip), %rsi      # 0x25856
callq 0x3290
jmp 0x16949
movq -0x88(%rbp), %rdi
callq 0x17280
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x7c60
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rdi
callq 0x7ab0
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xb140
testb $0x1, %al
jne 0x1699b
jmp 0x169f2
leaq -0x98(%rbp), %rdi
callq 0x7f80
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
movq 0x22610(%rip), %rdi     # 0x38fc8
leaq 0xec33(%rip), %rsi      # 0x255f2
callq 0x3290
movq %rax, -0x220(%rbp)
jmp 0x169cd
movq -0x220(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x30a0
jmp 0x169e2
jmp 0x169e4
leaq -0x98(%rbp), %rdi
callq 0xb1d0
jmp 0x16982
movq 0x225cf(%rip), %rdi     # 0x38fc8
leaq 0xee5d(%rip), %rsi      # 0x2585d
callq 0x3290
jmp 0x16a07
movq -0x88(%rbp), %rdi
callq 0x17280
addq $0x18, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x7c60
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x7ab0
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0xb140
testb $0x1, %al
jne 0x16a5d
jmp 0x16ab4
leaq -0xb8(%rbp), %rdi
callq 0x7f80
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movq 0x2254e(%rip), %rdi     # 0x38fc8
leaq 0xeb71(%rip), %rsi      # 0x255f2
callq 0x3290
movq %rax, -0x228(%rbp)
jmp 0x16a8f
movq -0x228(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x30a0
jmp 0x16aa4
jmp 0x16aa6
leaq -0xb8(%rbp), %rdi
callq 0xb1d0
jmp 0x16a44
movq 0x2250d(%rip), %rdi     # 0x38fc8
movq 0x224de(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x16ac9
movq -0x88(%rbp), %rdi
callq 0x17280
movq %rax, %rsi
movq -0x1a8(%rbp), %rax
movq (%rax), %rdx
leaq -0xe0(%rbp), %rdi
callq 0x15e40
jmp 0x16af0
leaq -0xe0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x17590
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x17600
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x175c0
testb $0x1, %al
jne 0x16b40
jmp 0x17102
leaq -0xf0(%rbp), %rdi
callq 0x17630
movq %rax, -0x100(%rbp)
movq 0x2246e(%rip), %rdi     # 0x38fc8
leaq 0xed05(%rip), %rsi      # 0x25866
callq 0x3290
movq %rax, -0x230(%rbp)
jmp 0x16b6f
movq -0x100(%rbp), %rdi
callq 0x17280
movq -0x230(%rbp), %rdi
movl 0x30(%rax), %esi
callq 0x34e0
movq %rax, -0x238(%rbp)
jmp 0x16b93
movq -0x238(%rbp), %rdi
leaq 0xea50(%rip), %rsi      # 0x255f1
callq 0x3290
jmp 0x16ba8
movq 0x22419(%rip), %rdi     # 0x38fc8
leaq 0xeca0(%rip), %rsi      # 0x25856
callq 0x3290
jmp 0x16bbd
movq -0x100(%rbp), %rdi
callq 0x17280
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rdi
callq 0x7c60
movq %rax, -0x110(%rbp)
movq -0x108(%rbp), %rdi
callq 0x7ab0
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0xb140
testb $0x1, %al
jne 0x16c0f
jmp 0x16c83
leaq -0x110(%rbp), %rdi
callq 0x7f80
movq (%rax), %rax
movq %rax, -0x120(%rbp)
movq 0x2239c(%rip), %rdi     # 0x38fc8
leaq 0xe9bf(%rip), %rsi      # 0x255f2
callq 0x3290
movq %rax, -0x240(%rbp)
jmp 0x16c41
movq -0x240(%rbp), %rdi
movq -0x120(%rbp), %rsi
callq 0x30a0
jmp 0x16c56
jmp 0x16c58
leaq -0x110(%rbp), %rdi
callq 0xb1d0
jmp 0x16bf6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x17400
jmp 0x171bc
movq 0x2233e(%rip), %rdi     # 0x38fc8
leaq 0xebcc(%rip), %rsi      # 0x2585d
callq 0x3290
jmp 0x16c98
movq -0x100(%rbp), %rdi
callq 0x17280
addq $0x18, %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rdi
callq 0x7c60
movq %rax, -0x130(%rbp)
movq -0x128(%rbp), %rdi
callq 0x7ab0
movq %rax, -0x138(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0xb140
testb $0x1, %al
jne 0x16cee
jmp 0x16d45
leaq -0x130(%rbp), %rdi
callq 0x7f80
movq (%rax), %rax
movq %rax, -0x140(%rbp)
movq 0x222bd(%rip), %rdi     # 0x38fc8
leaq 0xe8e0(%rip), %rsi      # 0x255f2
callq 0x3290
movq %rax, -0x248(%rbp)
jmp 0x16d20
movq -0x248(%rbp), %rdi
movq -0x140(%rbp), %rsi
callq 0x30a0
jmp 0x16d35
jmp 0x16d37
leaq -0x130(%rbp), %rdi
callq 0xb1d0
jmp 0x16cd5
movq -0x100(%rbp), %rdi
callq 0x17280
cmpl $-0x1, 0x30(%rax)
jne 0x16e88
movq 0x22266(%rip), %rdi     # 0x38fc8
movq 0x22237(%rip), %rsi     # 0x38fa0
callq 0x32f0
movq %rax, -0x250(%rbp)
jmp 0x16d77
movq -0x250(%rbp), %rdi
leaq 0xeaf7(%rip), %rsi      # 0x2587c
callq 0x3290
movq %rax, -0x258(%rbp)
jmp 0x16d93
movq -0x100(%rbp), %rdi
callq 0x17280
movq -0x258(%rbp), %rdi
movsd 0x38(%rax), %xmm0
callq 0x3530
movq %rax, -0x260(%rbp)
jmp 0x16db9
movq -0x260(%rbp), %rdi
leaq 0xeac6(%rip), %rsi      # 0x2588d
callq 0x3290
movq %rax, -0x268(%rbp)
jmp 0x16dd5
leaq -0x60(%rbp), %rdi
callq 0x17570
movq %rax, -0x278(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x17570
movq -0x278(%rbp), %rsi
movq -0x268(%rbp), %rdi
addq %rax, %rsi
callq 0x30a0
movq %rax, -0x270(%rbp)
jmp 0x16e0d
movq -0x270(%rbp), %rdi
leaq 0xea7e(%rip), %rsi      # 0x25899
callq 0x3290
movq %rax, -0x280(%rbp)
jmp 0x16e29
movq -0x280(%rbp), %rdi
movq 0x22169(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x16e3e
movq -0x100(%rbp), %rdi
callq 0x17280
movq %rax, -0x288(%rbp)
movq -0x100(%rbp), %rdi
callq 0x17280
movq -0x1b8(%rbp), %rdi
movq -0x288(%rbp), %rsi
movq %rax, %rdx
addq $0x38, %rdx
callq 0x17730
jmp 0x16e79
movl $0x1, -0x144(%rbp)
jmp 0x1710c
movb $0x0, -0x145(%rbp)
leaq -0x48(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rdi
callq 0x17590
movq %rax, -0x158(%rbp)
movq -0x150(%rbp), %rdi
callq 0x17600
movq %rax, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x175c0
testb $0x1, %al
jne 0x16edc
jmp 0x16f9a
leaq -0x158(%rbp), %rdi
callq 0x17630
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rdi
callq 0x17280
movq %rax, -0x298(%rbp)
movq -0x100(%rbp), %rdi
callq 0x17770
movq %rax, -0x290(%rbp)
jmp 0x16f17
movq -0x290(%rbp), %rsi
movq -0x298(%rbp), %rdi
movq -0x1a8(%rbp), %rax
movq (%rax), %rdx
callq 0x15bb0
testb $0x1, %al
jne 0x16f3a
jmp 0x16f87
movq -0x100(%rbp), %rsi
movq -0x168(%rbp), %rdi
callq 0x176d0
movq 0x22074(%rip), %rdi     # 0x38fc8
leaq 0xe945(%rip), %rsi      # 0x258a0
callq 0x3290
movq %rax, -0x2a0(%rbp)
jmp 0x16f69
movq -0x2a0(%rbp), %rdi
movq 0x22029(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x16f7e
movb $0x1, -0x145(%rbp)
jmp 0x16f9a
jmp 0x16f89
leaq -0x158(%rbp), %rdi
callq 0x177a0
jmp 0x16ec0
testb $0x1, -0x145(%rbp)
jne 0x170a1
leaq -0x60(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rdi
callq 0x17590
movq %rax, -0x178(%rbp)
movq -0x170(%rbp), %rdi
callq 0x17600
movq %rax, -0x180(%rbp)
leaq -0x178(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x175c0
testb $0x1, %al
jne 0x16ff4
jmp 0x1709f
leaq -0x178(%rbp), %rdi
callq 0x17630
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rdi
callq 0x17280
movq %rax, -0x2b0(%rbp)
movq -0x100(%rbp), %rdi
callq 0x17770
movq %rax, -0x2a8(%rbp)
jmp 0x1702f
movq -0x2a8(%rbp), %rsi
movq -0x2b0(%rbp), %rdi
movq -0x1a8(%rbp), %rax
movq (%rax), %rdx
callq 0x15bb0
testb $0x1, %al
jne 0x17052
jmp 0x1708c
movb $0x1, -0x145(%rbp)
movq 0x21f68(%rip), %rdi     # 0x38fc8
leaq 0xe84f(%rip), %rsi      # 0x258b6
callq 0x3290
movq %rax, -0x2b8(%rbp)
jmp 0x17075
movq -0x2b8(%rbp), %rdi
movq 0x21f1d(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x1708a
jmp 0x1709f
jmp 0x1708e
leaq -0x178(%rbp), %rdi
callq 0x177a0
jmp 0x16fd8
jmp 0x170a1
testb $0x1, -0x145(%rbp)
jne 0x170ef
movq -0x100(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x17500
jmp 0x170bc
movq 0x21f05(%rip), %rdi     # 0x38fc8
leaq 0xe804(%rip), %rsi      # 0x258ce
callq 0x3290
movq %rax, -0x2c0(%rbp)
jmp 0x170d8
movq -0x2c0(%rbp), %rdi
movq 0x21eba(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x170ed
jmp 0x170ef
jmp 0x170f1
leaq -0xf0(%rbp), %rdi
callq 0x177a0
jmp 0x16b24
movl $0x0, -0x144(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x17400
movl -0x144(%rbp), %eax
testl %eax, %eax
jne 0x17191
jmp 0x17124
jmp 0x16702
movq 0x21e98(%rip), %rdi     # 0x38fc8
leaq 0xe7ba(%rip), %rsi      # 0x258f1
callq 0x3290
movq %rax, -0x2c8(%rbp)
jmp 0x17145
movq -0x2c8(%rbp), %rdi
movq 0x21e4d(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x1715a
movq -0x198(%rbp), %rsi
movq -0x1b8(%rbp), %rdi
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0x190(%rbp)
leaq -0x190(%rbp), %rdx
callq 0x177c0
jmp 0x17187
movl $0x1, -0x144(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x17400
leaq -0x48(%rbp), %rdi
callq 0x17400
leaq -0x20(%rbp), %rdi
callq 0x17320
movq -0x1b0(%rbp), %rax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x17400
leaq -0x48(%rbp), %rdi
callq 0x17400
leaq -0x20(%rbp), %rdi
callq 0x17320
movq -0x28(%rbp), %rdi
callq 0x34f0