-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.cpp
119 lines (109 loc) · 2.68 KB
/
day6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <chrono>
using namespace std;
const char FILENAME[] = "input.txt";
const int NUM_BANKS = 16;
const int MAX_DEPTH = 32000;
void getInitialConfig(short int banks[])
{
ifstream stream;
stream.open(FILENAME);
if (stream.fail())
{
cout << "Couldn't open file" << endl;
exit(1);
}
int nBanks = 0;
int blocks;
while (stream >> blocks)
{
if (nBanks > NUM_BANKS)
{
cout << "More banks than expected" << endl;
exit(1);
}
banks[nBanks] = blocks;
nBanks++;
}
stream.close();
if (nBanks < NUM_BANKS)
{
cout << "Fewer banks than expected" << endl;
exit(1);
}
}
void recordForPosterity(const short int banks[], short int history[][NUM_BANKS], const int cycles)
{
for (int i = 0; i < NUM_BANKS; i++)
history[cycles][i] = banks[i];
}
void redistributeBlocks(short int banks[], short int history[][NUM_BANKS])
{
int maxBlocks = banks[0];
for (int i = 1; i < NUM_BANKS; i++)
if (banks[i] > maxBlocks)
maxBlocks = banks[i];
int X;
for (int i = 0; i < NUM_BANKS; i++)
if (banks[i] == maxBlocks)
{
X = i;
banks[X] = 0;
break;
}
for (int blocks = maxBlocks; blocks > 0; blocks--)
{
X++;
if (X == NUM_BANKS)
X = 0;
banks[X]++;
}
}
bool duplicateStates(const short int history[][NUM_BANKS], const int cycles, int& diff)
{
bool duplicate = false;
for (int i = 0; i < cycles; i++)
{
duplicate = true;
for (int j = 0; j < NUM_BANKS; j++)
{
if (history[cycles][j] != history[i][j])
{
duplicate = false;
break;
}
}
if (duplicate)
{
diff = cycles - i;
break;
}
}
return duplicate;
}
int main()
{
short int banks[NUM_BANKS];
int cycles = 0;
short int history[MAX_DEPTH][NUM_BANKS];
int diff;
getInitialConfig(banks);
auto start = chrono::high_resolution_clock::now();
recordForPosterity(banks, history, cycles);
while (!duplicateStates(history, cycles, diff))
{
redistributeBlocks(banks, history);
cycles++;
recordForPosterity(banks, history, cycles);
}
auto finish = chrono::high_resolution_clock::now();
cout << cycles << endl;
cout << diff << endl;
chrono::duration<double> elapsed = finish - start;
cout << elapsed.count() << endl;
for (int j = 0; j < NUM_BANKS; j++)
cout << history[cycles][j] << ' ';
cout << endl;
}