Преглед на файлове

Modernize some of our examples. (#5655)

Make some code simplifications using new toolchain functionality.

Depends on #5653.
Richard Smith преди 10 месеца
родител
ревизия
c9e4761f0c

+ 2 - 2
examples/advent2024/day10_part1.carbon

@@ -17,7 +17,7 @@ fn PopCount(n: u256) -> i32 {
     if (n & bit != 0) {
       ++total;
     }
-    bit = bit << 1;
+    bit <<= 1;
   }
   return total;
 }
@@ -32,7 +32,7 @@ class Reachable {
       while (x < 43) {
         if (terrain.height[x][y] == 0) {
           me.trailheads[x][y] = next;
-          next = next << 1;
+          next <<= 1;
         }
         ++x;
       }

+ 2 - 2
examples/advent2024/day10_part2.carbon

@@ -42,12 +42,12 @@ class PathsToTop {
             if (adj_x >= 0 and adj_x < 43 and
                 adj_y >= 0 and adj_y < 43 and
                 terrain.height[adj_x][adj_y] == level + 1) {
-              paths = paths + self->paths[adj_x][adj_y];
+              paths += self->paths[adj_x][adj_y];
             }
             ++i;
           }
           self->paths[x][y] = paths;
-          total = total + paths;
+          total += paths;
         }
         ++x;
       }

+ 2 - 2
examples/advent2024/day11_common.carbon

@@ -14,8 +14,8 @@ fn Next(n: i64) -> (i64, i64) {
   var pow10: i64 = 10;
   var pow100: i64 = 1;
   while (n / pow100 >= 100) {
-    pow100 = pow100 * 100;
-    pow10 = pow10 * 10;
+    pow100 *= 100;
+    pow10 *= 10;
   }
   if (n / pow100 >= 10) {
     return (n / pow10, n % pow10);

+ 1 - 1
examples/advent2024/day11_part1.carbon

@@ -13,7 +13,7 @@ fn Run() {
   var n: i64;
   var total: i32 = 0;
   while (ReadInt64(&n)) {
-    total = total + Count(n, 25);
+    total += Count(n, 25);
     SkipSpaces();
   }
   Core.Print(total);

+ 7 - 22
examples/advent2024/day11_part2.carbon

@@ -44,26 +44,12 @@ class Digits {
   var count: array(array(i64, 75), 10);
 }
 
-// TODO: Add a builtin to perform integer conversion / truncation.
-fn I64DigitToI32(a: i64) -> i32 {
-  if (a == 0) { return 0; }
-  if (a == 1) { return 1; }
-  if (a == 2) { return 2; }
-  if (a == 3) { return 3; }
-  if (a == 4) { return 4; }
-  if (a == 5) { return 5; }
-  if (a == 6) { return 6; }
-  if (a == 7) { return 7; }
-  if (a == 8) { return 8; }
-  return 9;
-}
-
 fn ReduceToDigits(n: i64, depth: i32, multiplicity: i64, digits: Digits*) -> i64 {
   if (n == -1) { return 0; }
   if (depth == 0) { return multiplicity; }
   if (n < 10) {
-    let count: i64* = &digits->count[I64DigitToI32(n)][depth - 1];
-    *count = *count + multiplicity;
+    let count: i64* = &digits->count[n as i32][depth - 1];
+    *count += multiplicity;
     return 0;
   }
   let next: (i64, i64) = Next(n);
@@ -78,7 +64,7 @@ fn Run() {
 
   var n: i64;
   while (ReadInt64(&n)) {
-    total = total + ReduceToDigits(n, max_depth, 1, &digits);
+    total += ReduceToDigits(n, max_depth, 1, &digits);
     PrintInt64(total);
     digits.Print(max_depth - 1);
     SkipSpaces();
@@ -90,14 +76,13 @@ fn Run() {
     digits.Print(depth);
     var digit: i64 = 0;
     while (digit < 10) {
-      let m: i64 = digits.count[I64DigitToI32(digit)][depth];
+      let m: i64 = digits.count[digit as i32][depth];
       if (m > 0) {
         let next: (i64, i64) = Next(digit);
-        total = total +
-                ReduceToDigits(next.0, depth, m, &digits) +
-                ReduceToDigits(next.1, depth, m, &digits);
+        total += ReduceToDigits(next.0, depth, m, &digits) +
+                 ReduceToDigits(next.1, depth, m, &digits);
       }
-      digit = digit + 1;
+      ++digit;
     }
     --depth;
   }

+ 4 - 4
examples/advent2024/day13_common.carbon

@@ -63,8 +63,8 @@ class BezoutSolutionSet {
 
     // Find an initial solution. Note that m and n might be negative.
     let num_gcds: i64 = c / e.gcd;
-    e.m = e.m * num_gcds;
-    e.n = e.n * num_gcds;
+    e.m *= num_gcds;
+    e.n *= num_gcds;
 
     // Pick the smallest positive m we can.
     let a_over_gcd: i64 = a / e.gcd;
@@ -77,8 +77,8 @@ class BezoutSolutionSet {
     } else {
       adj = e.m / b_over_gcd;
     }
-    e.m = e.m - adj * b_over_gcd;
-    e.n = e.n + adj * a_over_gcd;
+    e.m -= adj * b_over_gcd;
+    e.n += adj * a_over_gcd;
     return {.m0 = e.m, .n0 = e.n,
             .m_step = b_over_gcd, .n_step = -a_over_gcd};
   }

+ 1 - 1
examples/advent2024/day13_part1.carbon

@@ -13,7 +13,7 @@ fn Run() {
   var total_cost: i64 = 0;
   while (true) {
     var m: Machine = Machine.Read();
-    total_cost = total_cost + CostIfPossible(m);
+    total_cost += CostIfPossible(m);
     if (not SkipNewline()) {
       break;
     }

+ 3 - 3
examples/advent2024/day13_part2.carbon

@@ -13,9 +13,9 @@ fn Run() {
   var total_cost: i64 = 0;
   while (true) {
     var m: Machine = Machine.Read();
-    m.prize.0 = m.prize.0 + 10000000000000;
-    m.prize.1 = m.prize.1 + 10000000000000;
-    total_cost = total_cost + CostIfPossible(m);
+    m.prize.0 += 10_000_000_000_000;
+    m.prize.1 += 10_000_000_000_000;
+    total_cost += CostIfPossible(m);
     if (not SkipNewline()) {
       break;
     }

+ 2 - 4
examples/advent2024/day5_common.carbon

@@ -25,8 +25,7 @@ class Rules {
     var a: i32;
     var b: i32;
     while (ReadInt(&a) and ConsumeChar(0x7C) and ReadInt(&b)) {
-      // TODO: rules.disallowed_before[a] |= PageMask(b);
-      rules.disallowed_before[a] = rules.disallowed_before[a] | PageMask(b);
+      rules.disallowed_before[a] |= PageMask(b);
       SkipNewline();
     }
     return var;
@@ -75,8 +74,7 @@ class PageList {
       if (seen & rules.disallowed_before[page] != 0) {
         return false;
       }
-      // TODO: seen |= PageMask(page);
-      seen = seen | PageMask(page);
+      seen |= PageMask(page);
       ++i;
     }
     return true;

+ 2 - 3
examples/advent2024/day6_part2.carbon

@@ -15,9 +15,8 @@ class LoopDetector {
 
   fn Check[addr self: Self*](next: ((i32, i32), (i32, i32))) -> bool {
     // TODO: if (next == self->last) {
-    // TODO: The lexer mishandles `next.0.0` as `next` `.` `0.0`.
-    if (next.0 .0 == self->last.0 .0 and next.0 .1 == self->last.0 .1 and
-        next.1 .0 == self->last.1 .0 and next.1 .1 == self->last.1 .1) {
+    if (next.0.0 == self->last.0.0 and next.0.1 == self->last.0.1 and
+        next.1.0 == self->last.1.0 and next.1.1 == self->last.1.1) {
       return true;
     }
     --self->steps;

+ 2 - 2
examples/advent2024/day7_common.carbon

@@ -16,8 +16,8 @@ fn Concat(a_val: i64, b_val: i64) -> i64 {
     return a * 10;
   }
   while (b != 0) {
-    a = a * 10;
-    b = b / 10;
+    a *= 10;
+    b /= 10;
   }
   return a + b_val;
 }

+ 1 - 2
examples/advent2024/day7_part1.carbon

@@ -14,8 +14,7 @@ fn Run() {
   while (PeekChar() != Core.EOF()) {
     var eq: Equation = Equation.Read();
     if (eq.Solve(false)) {
-      // TODO: total += eq.result;
-      total = total + eq.result;
+      total += eq.result;
     }
   }
   PrintInt64(total);

+ 1 - 2
examples/advent2024/day7_part2.carbon

@@ -14,8 +14,7 @@ fn Run() {
   while (PeekChar() != Core.EOF()) {
     var eq: Equation = Equation.Read();
     if (eq.Solve(true)) {
-      // TODO: total += eq.result;
-      total = total + eq.result;
+      total += eq.result;
     }
   }
   PrintInt64(total);