This commit is contained in:
parent
d848f3951b
commit
fbca1996a5
@ -14,54 +14,42 @@ const Solution = struct {
|
|||||||
b: usize,
|
b: usize,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Race = struct {
|
|
||||||
time: usize,
|
|
||||||
distance: usize,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
|
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
|
||||||
const races: []Race = r: {
|
var result: usize = 1;
|
||||||
|
{
|
||||||
var it = util.splitLines(input);
|
var it = util.splitLines(input);
|
||||||
const times = try util.parseIntsScalar(usize, alloc, it.next().?[10..], .{});
|
const times = try util.parseIntsScalar(usize, alloc, it.next().?[10..], .{});
|
||||||
const distances = try util.parseIntsScalar(usize, alloc, it.next().?[10..], .{});
|
const distances = try util.parseIntsScalar(usize, alloc, it.next().?[10..], .{});
|
||||||
defer alloc.free(times);
|
defer alloc.free(times);
|
||||||
defer alloc.free(distances);
|
defer alloc.free(distances);
|
||||||
var al = try std.ArrayList(Race).initCapacity(alloc, times.len);
|
|
||||||
for (times, distances) |t, d| {
|
|
||||||
try al.append(.{ .time = t, .distance = d });
|
|
||||||
}
|
|
||||||
break :r try al.toOwnedSlice();
|
|
||||||
};
|
|
||||||
defer alloc.free(races);
|
|
||||||
|
|
||||||
const bigRace: Race = r: {
|
for (times, distances) |time, minDistance| {
|
||||||
var it = util.splitLines(input);
|
var winning: usize = 0;
|
||||||
var vals: [2]usize = undefined;
|
for (0..time - 1) |speed| {
|
||||||
for (0..vals.len) |i| {
|
const distance = speed * (time - speed);
|
||||||
const line = try mem.replaceOwned(u8, alloc, it.next().?[10..], " ", "");
|
if (distance > minDistance) winning += 1;
|
||||||
defer alloc.free(line);
|
}
|
||||||
vals[i] = try std.fmt.parseInt(usize, line, 10);
|
result *= winning;
|
||||||
}
|
}
|
||||||
break :r .{ .time = vals[0], .distance = vals[1] };
|
|
||||||
};
|
|
||||||
|
|
||||||
var result: usize = 1;
|
|
||||||
for (races) |race| {
|
|
||||||
var winning: usize = 0;
|
|
||||||
for (0..race.time - 1) |speed| {
|
|
||||||
const distance = speed * (race.time - speed);
|
|
||||||
if (distance > race.distance) winning += 1;
|
|
||||||
}
|
|
||||||
result *= winning;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var result2: usize = 1;
|
var result2: usize = 1;
|
||||||
var winning2: usize = 0;
|
{
|
||||||
for (0..bigRace.time - 1) |speed| {
|
var it = util.splitLines(input);
|
||||||
const distance = speed * (bigRace.time - speed);
|
var td: [2]usize = undefined;
|
||||||
if (distance > bigRace.distance) winning2 += 1;
|
for (0..td.len) |i| {
|
||||||
|
const line = try mem.replaceOwned(u8, alloc, it.next().?[10..], " ", "");
|
||||||
|
defer alloc.free(line);
|
||||||
|
td[i] = try std.fmt.parseInt(usize, line, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
var winning2: usize = 0;
|
||||||
|
for (0..td[0] - 1) |speed| {
|
||||||
|
const distance = speed * (td[0] - speed);
|
||||||
|
if (distance > td[1]) winning2 += 1;
|
||||||
|
}
|
||||||
|
result2 *= winning2;
|
||||||
}
|
}
|
||||||
result2 *= winning2;
|
|
||||||
|
|
||||||
return .{ .a = result, .b = result2 };
|
return .{ .a = result, .b = result2 };
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user