Skip to content

Commit

Permalink
2024 day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Dec 14, 2024
1 parent 24304b5 commit 2e4a296
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
66 changes: 66 additions & 0 deletions 2024/11/solve.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const std = @import("std");

const Allocator = std.mem.Allocator;

fn number_of_digits(n: u64) u8 {
return 1 + std.math.log10_int(n);
}

fn split(n: u64) ?[2]u64 {
var exponent: u8 = 1;
var power_of_ten: u64 = 10;
var sep: u64 = 1;
while (power_of_ten <= n) {
exponent += 1;
power_of_ten *= 10;
if (exponent % 2 == 0)
sep *= 10;
}
if (exponent % 2 != 0)
return null;
return .{ @divTrunc(n, sep), n % sep };
}

fn _how_many(blinks: u7, stone: u64) u64 {
if (stone == 0) {
return switch (blinks) {
1, 2 => 1,
else => how_many(blinks - 2, 2024),
};
}
if (split(stone)) |pair| {
return if (blinks == 1) 2 else how_many(blinks - 1, pair[0]) + how_many(blinks - 1, pair[1]);
}
return if (blinks == 1) 1 else how_many(blinks - 1, stone * 2024);
}

var cache: std.AutoHashMapUnmanaged(struct { u7, u64 }, u64) = .empty;

fn how_many(blinks: u7, stone: u64) u64 {
if (cache.get(.{ blinks, stone })) |value| {
return value;
}
const value = _how_many(blinks, stone);
cache.putAssumeCapacity(.{ blinks, stone }, value);
return value;
}

pub fn main() !void {
const input = [_]u64{ 2, 77706, 5847, 9258441, 0, 741, 883933, 12 };
const blinks = 75;
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const alloc = arena.allocator();

try cache.ensureTotalCapacity(alloc, 140_000);

var result: u64 = 0;
var timer = try std.time.Timer.start();
for (input) |stone| {
result += how_many(blinks, stone);
}
const time = timer.read();
std.debug.print("Number of stones after {} blinks: {}\n", .{ blinks, result });
std.debug.print("{}\n", .{std.fmt.fmtDuration(time)});
std.debug.print("cache entries: {}\n", .{cache.count()});
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ My solutions for the [Advent of Code](https://adventofcode.com), a challenge sta
| 08 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 09 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 10 |||||:star::star:|:star::star:|:star::star:|:star::star:|
| 11 |||||:star::star:|:star::star:|:star::star:|:star::star:|
| 11 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 12 |||||:star::star:|:star::star:|:star::star:|:star::star:|
| 13 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 14 |||||:star::star:|:star::star:|:star::star:|:star::star:|
Expand All @@ -30,6 +30,6 @@ My solutions for the [Advent of Code](https://adventofcode.com), a challenge sta
| 23 |||||||:star::star:|:star:|
| 24 ||||||:star::star:|:star::star:|:star:|
| 25 |||||:star:|:star:|:star::star:||
| Total | 10 | 14 | 4 | 10 | 42 | 44 | 50 | 44 | 20
| Total | 10 | 14 | 4 | 10 | 42 | 44 | 50 | 44 | 22

Total stars: 238
Total stars: 240

0 comments on commit 2e4a296

Please sign in to comment.