Skip to content

Commit c9e4761

Browse files
authored
Modernize some of our examples. (#5655)
Make some code simplifications using new toolchain functionality. Depends on #5653.
1 parent 519e633 commit c9e4761

13 files changed

+30
-50
lines changed

examples/advent2024/day10_part1.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn PopCount(n: u256) -> i32 {
1717
if (n & bit != 0) {
1818
++total;
1919
}
20-
bit = bit << 1;
20+
bit <<= 1;
2121
}
2222
return total;
2323
}
@@ -32,7 +32,7 @@ class Reachable {
3232
while (x < 43) {
3333
if (terrain.height[x][y] == 0) {
3434
me.trailheads[x][y] = next;
35-
next = next << 1;
35+
next <<= 1;
3636
}
3737
++x;
3838
}

examples/advent2024/day10_part2.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ class PathsToTop {
4242
if (adj_x >= 0 and adj_x < 43 and
4343
adj_y >= 0 and adj_y < 43 and
4444
terrain.height[adj_x][adj_y] == level + 1) {
45-
paths = paths + self->paths[adj_x][adj_y];
45+
paths += self->paths[adj_x][adj_y];
4646
}
4747
++i;
4848
}
4949
self->paths[x][y] = paths;
50-
total = total + paths;
50+
total += paths;
5151
}
5252
++x;
5353
}

examples/advent2024/day11_common.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ fn Next(n: i64) -> (i64, i64) {
1414
var pow10: i64 = 10;
1515
var pow100: i64 = 1;
1616
while (n / pow100 >= 100) {
17-
pow100 = pow100 * 100;
18-
pow10 = pow10 * 10;
17+
pow100 *= 100;
18+
pow10 *= 10;
1919
}
2020
if (n / pow100 >= 10) {
2121
return (n / pow10, n % pow10);

examples/advent2024/day11_part1.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn Run() {
1313
var n: i64;
1414
var total: i32 = 0;
1515
while (ReadInt64(&n)) {
16-
total = total + Count(n, 25);
16+
total += Count(n, 25);
1717
SkipSpaces();
1818
}
1919
Core.Print(total);

examples/advent2024/day11_part2.carbon

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,12 @@ class Digits {
4444
var count: array(array(i64, 75), 10);
4545
}
4646

47-
// TODO: Add a builtin to perform integer conversion / truncation.
48-
fn I64DigitToI32(a: i64) -> i32 {
49-
if (a == 0) { return 0; }
50-
if (a == 1) { return 1; }
51-
if (a == 2) { return 2; }
52-
if (a == 3) { return 3; }
53-
if (a == 4) { return 4; }
54-
if (a == 5) { return 5; }
55-
if (a == 6) { return 6; }
56-
if (a == 7) { return 7; }
57-
if (a == 8) { return 8; }
58-
return 9;
59-
}
60-
6147
fn ReduceToDigits(n: i64, depth: i32, multiplicity: i64, digits: Digits*) -> i64 {
6248
if (n == -1) { return 0; }
6349
if (depth == 0) { return multiplicity; }
6450
if (n < 10) {
65-
let count: i64* = &digits->count[I64DigitToI32(n)][depth - 1];
66-
*count = *count + multiplicity;
51+
let count: i64* = &digits->count[n as i32][depth - 1];
52+
*count += multiplicity;
6753
return 0;
6854
}
6955
let next: (i64, i64) = Next(n);
@@ -78,7 +64,7 @@ fn Run() {
7864

7965
var n: i64;
8066
while (ReadInt64(&n)) {
81-
total = total + ReduceToDigits(n, max_depth, 1, &digits);
67+
total += ReduceToDigits(n, max_depth, 1, &digits);
8268
PrintInt64(total);
8369
digits.Print(max_depth - 1);
8470
SkipSpaces();
@@ -90,14 +76,13 @@ fn Run() {
9076
digits.Print(depth);
9177
var digit: i64 = 0;
9278
while (digit < 10) {
93-
let m: i64 = digits.count[I64DigitToI32(digit)][depth];
79+
let m: i64 = digits.count[digit as i32][depth];
9480
if (m > 0) {
9581
let next: (i64, i64) = Next(digit);
96-
total = total +
97-
ReduceToDigits(next.0, depth, m, &digits) +
98-
ReduceToDigits(next.1, depth, m, &digits);
82+
total += ReduceToDigits(next.0, depth, m, &digits) +
83+
ReduceToDigits(next.1, depth, m, &digits);
9984
}
100-
digit = digit + 1;
85+
++digit;
10186
}
10287
--depth;
10388
}

examples/advent2024/day13_common.carbon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class BezoutSolutionSet {
6363

6464
// Find an initial solution. Note that m and n might be negative.
6565
let num_gcds: i64 = c / e.gcd;
66-
e.m = e.m * num_gcds;
67-
e.n = e.n * num_gcds;
66+
e.m *= num_gcds;
67+
e.n *= num_gcds;
6868

6969
// Pick the smallest positive m we can.
7070
let a_over_gcd: i64 = a / e.gcd;
@@ -77,8 +77,8 @@ class BezoutSolutionSet {
7777
} else {
7878
adj = e.m / b_over_gcd;
7979
}
80-
e.m = e.m - adj * b_over_gcd;
81-
e.n = e.n + adj * a_over_gcd;
80+
e.m -= adj * b_over_gcd;
81+
e.n += adj * a_over_gcd;
8282
return {.m0 = e.m, .n0 = e.n,
8383
.m_step = b_over_gcd, .n_step = -a_over_gcd};
8484
}

examples/advent2024/day13_part1.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn Run() {
1313
var total_cost: i64 = 0;
1414
while (true) {
1515
var m: Machine = Machine.Read();
16-
total_cost = total_cost + CostIfPossible(m);
16+
total_cost += CostIfPossible(m);
1717
if (not SkipNewline()) {
1818
break;
1919
}

examples/advent2024/day13_part2.carbon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ fn Run() {
1313
var total_cost: i64 = 0;
1414
while (true) {
1515
var m: Machine = Machine.Read();
16-
m.prize.0 = m.prize.0 + 10000000000000;
17-
m.prize.1 = m.prize.1 + 10000000000000;
18-
total_cost = total_cost + CostIfPossible(m);
16+
m.prize.0 += 10_000_000_000_000;
17+
m.prize.1 += 10_000_000_000_000;
18+
total_cost += CostIfPossible(m);
1919
if (not SkipNewline()) {
2020
break;
2121
}

examples/advent2024/day5_common.carbon

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class Rules {
2525
var a: i32;
2626
var b: i32;
2727
while (ReadInt(&a) and ConsumeChar(0x7C) and ReadInt(&b)) {
28-
// TODO: rules.disallowed_before[a] |= PageMask(b);
29-
rules.disallowed_before[a] = rules.disallowed_before[a] | PageMask(b);
28+
rules.disallowed_before[a] |= PageMask(b);
3029
SkipNewline();
3130
}
3231
return var;
@@ -75,8 +74,7 @@ class PageList {
7574
if (seen & rules.disallowed_before[page] != 0) {
7675
return false;
7776
}
78-
// TODO: seen |= PageMask(page);
79-
seen = seen | PageMask(page);
77+
seen |= PageMask(page);
8078
++i;
8179
}
8280
return true;

examples/advent2024/day6_part2.carbon

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ class LoopDetector {
1515

1616
fn Check[addr self: Self*](next: ((i32, i32), (i32, i32))) -> bool {
1717
// TODO: if (next == self->last) {
18-
// TODO: The lexer mishandles `next.0.0` as `next` `.` `0.0`.
19-
if (next.0 .0 == self->last.0 .0 and next.0 .1 == self->last.0 .1 and
20-
next.1 .0 == self->last.1 .0 and next.1 .1 == self->last.1 .1) {
18+
if (next.0.0 == self->last.0.0 and next.0.1 == self->last.0.1 and
19+
next.1.0 == self->last.1.0 and next.1.1 == self->last.1.1) {
2120
return true;
2221
}
2322
--self->steps;

0 commit comments

Comments
 (0)