1- #include < vector>
1+ #include < algorithm>
2+ #include < cassert>
3+ #include < cstdint>
4+ #include < cstring>
5+ #include < fstream>
26#include < iostream>
37#include < list>
4- #include < fstream>
58#include < sstream>
69#include < stdexcept>
7- #include < cstring>
8- #include < cassert>
9- #include < cstdint>
10- #include < algorithm>
10+ #include < vector>
1111
1212struct Operation
1313{
@@ -17,8 +17,7 @@ struct Operation
1717 uint64_t calc (uint64_t n) const
1818 {
1919 // WARNING no overflow checks in C++
20- switch (this ->opcode )
21- {
20+ switch (this ->opcode ) {
2221 case ' ^' :
2322 return n * n;
2423 case ' +' :
@@ -46,50 +45,39 @@ void solve(const std::vector<Monkey> &monkeys_orig, int rounds)
4645 std::vector<Monkey> monkeys = monkeys_orig;
4746
4847 uint64_t modulus = 1 ;
49- for (const auto &monkey : monkeys)
50- {
48+ for (const auto &monkey : monkeys) {
5149 modulus *= monkey.divisible_by ;
5250 }
5351
54- for (int round = 0 ; round < rounds; ++round)
55- {
56- for (auto &monkey : monkeys)
57- {
52+ for (int round = 0 ; round < rounds; ++round) {
53+ for (auto &monkey : monkeys) {
5854 // Nota: this loop can run forever if if_true/if_false refers the current monkey and C++ allows it.
5955 // In Rust it's not possible because of the borrowing that forces to copy the items vector
60- while (!monkey.items .empty ())
61- {
56+ while (!monkey.items .empty ()) {
6257 monkey.inspections += 1 ;
6358
6459 uint64_t item = monkey.items .front ();
6560 monkey.items .pop_front ();
6661
6762 uint64_t worry_level = monkey.operation .calc (item);
6863
69- if (rounds == 20 )
70- {
64+ if (rounds == 20 ) {
7165 worry_level = worry_level / 3 ;
72- }
73- else
74- {
66+ } else {
7567 worry_level = worry_level % modulus;
7668 }
7769
78- if (worry_level % monkey.divisible_by == 0 )
79- {
70+ if (worry_level % monkey.divisible_by == 0 ) {
8071 monkeys[monkey.if_true ].items .push_back (worry_level);
81- }
82- else
83- {
72+ } else {
8473 monkeys[monkey.if_false ].items .push_back (worry_level);
8574 }
8675 }
8776 }
8877 }
8978
9079 std::sort (monkeys.begin (), monkeys.end (),
91- [](const Monkey &a, const Monkey &b) -> bool
92- {
80+ [](const Monkey &a, const Monkey &b) -> bool {
9381 return a.inspections > b.inspections ;
9482 });
9583 uint64_t monkey_business_level = (uint64_t )monkeys[0 ].inspections * (uint64_t )monkeys[1 ].inspections ;
@@ -104,13 +92,11 @@ void read_data(const char *filename, std::vector<Monkey> &monkeys)
10492
10593 f.open (filename);
10694
107- if (!f.is_open ())
108- {
95+ if (!f.is_open ()) {
10996 throw std::logic_error (" bad filename" );
11097 }
11198
112- while (true )
113- {
99+ while (true ) {
114100 Monkey monkey{
115101 .items = std::list<uint64_t >(),
116102 .operation = Operation{.opcode = 0 , .param = 0 },
@@ -129,31 +115,23 @@ void read_data(const char *filename, std::vector<Monkey> &monkeys)
129115 line = line.substr (strlen (" Starting items: " ));
130116 std::string item;
131117 std::stringstream ss (line);
132- while (std::getline (ss, item, ' ,' ))
133- {
118+ while (std::getline (ss, item, ' ,' )) {
134119 monkey.items .push_back (std::stoul (item));
135120 }
136121
137122 // equally tedious parsing
138123 std::getline (f, line);
139124 assert (line.find (" Operation: new = " ) == 0 );
140125 line = line.substr (strlen (" Operation: new = " ));
141- if (line == " old * old" )
142- {
126+ if (line == " old * old" ) {
143127 monkey.operation .opcode = ' ^' ;
144- }
145- else if (line.find (" old + " ) == 0 )
146- {
128+ } else if (line.find (" old + " ) == 0 ) {
147129 monkey.operation .opcode = ' +' ;
148130 monkey.operation .param = std::stoul (line.substr (strlen (" old + " )));
149- }
150- else if (line.find (" old * " ) == 0 )
151- {
131+ } else if (line.find (" old * " ) == 0 ) {
152132 monkey.operation .opcode = ' *' ;
153133 monkey.operation .param = std::stoul (line.substr (strlen (" old * " )));
154- }
155- else
156- {
134+ } else {
157135 throw std::logic_error (" unknown operation" );
158136 }
159137
0 commit comments