Day 7
This commit is contained in:
parent
1db33f5a9f
commit
8112d51004
|
@ -0,0 +1,27 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn build(b: *std.build.Builder) void {
|
||||||
|
// Standard target options allows the person running `zig build` to choose
|
||||||
|
// what target to build for. Here we do not override the defaults, which
|
||||||
|
// means any target is allowed, and the default is native. Other options
|
||||||
|
// for restricting supported target set are available.
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
|
||||||
|
// Standard release options allow the person running `zig build` to select
|
||||||
|
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||||
|
const mode = b.standardReleaseOptions();
|
||||||
|
|
||||||
|
const exe = b.addExecutable("day7", "src/main.zig");
|
||||||
|
exe.setTarget(target);
|
||||||
|
exe.setBuildMode(mode);
|
||||||
|
exe.install();
|
||||||
|
|
||||||
|
const run_cmd = exe.run();
|
||||||
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
if (b.args) |args| {
|
||||||
|
run_cmd.addArgs(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
const run_step = b.step("run", "Run the app");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,20,1091,861,228,628,186,980,996,710,541,354,1611,69,1331,91,1220,447,523,38,1286,244,643,1069,566,70,155,1710,1266,120,302,72,232,387,1086,278,1122,605,1559,98,111,1816,795,543,1217,304,356,129,839,704,49,523,370,74,13,232,179,101,664,892,266,622,197,404,147,882,435,504,48,766,684,1362,136,830,1393,1259,925,68,879,251,43,1339,61,98,403,51,1008,197,659,195,1823,233,121,731,82,141,580,18,427,774,13,685,496,752,63,132,39,237,18,167,51,299,22,19,1442,305,1283,253,159,731,302,76,115,185,136,447,821,307,207,30,1427,251,589,0,1096,1240,261,442,757,5,172,847,858,382,425,79,402,166,1058,186,35,21,324,183,1293,95,410,321,12,155,88,409,40,428,1180,199,1444,487,148,57,1187,15,100,983,77,0,1667,359,513,659,339,142,968,994,787,0,443,183,133,538,4,332,1459,204,1156,710,1654,20,36,407,890,1265,1090,743,36,78,1033,781,608,476,103,1401,24,4,875,414,799,305,1047,842,72,497,362,270,73,12,9,0,21,11,51,1357,455,505,483,552,199,1108,214,238,686,1496,116,154,1403,35,272,738,1024,50,50,934,564,19,395,324,447,794,1326,14,407,1694,452,439,455,442,86,1515,588,809,224,112,156,21,1405,610,187,23,370,112,397,995,777,75,1281,32,60,284,388,916,555,200,675,20,320,32,398,104,113,447,113,351,396,322,36,1674,1708,232,1004,514,95,859,382,116,277,239,343,3,433,11,55,699,513,1465,319,44,306,224,615,482,695,421,300,321,283,579,323,102,275,17,723,632,713,277,801,222,130,189,1549,49,784,690,136,444,1315,259,1334,472,711,1109,276,70,315,838,35,328,766,1100,460,4,178,630,571,5,106,429,368,547,1210,840,162,166,10,403,880,287,44,1001,316,402,1054,174,7,1194,105,58,268,667,86,588,166,547,238,1586,77,112,244,107,63,873,1152,16,407,198,26,587,20,1449,775,653,1369,732,262,566,222,10,102,22,573,233,297,1238,789,291,93,206,1274,177,58,841,672,37,16,262,201,241,938,133,774,978,631,511,0,263,498,799,51,330,11,206,325,173,676,15,457,364,46,373,34,1475,530,672,295,55,4,297,274,1519,15,688,555,96,160,185,583,646,41,378,1572,67,219,572,143,16,286,65,788,886,243,883,1012,109,90,742,464,1099,388,1855,731,62,6,415,66,232,542,98,1123,11,414,1262,4,440,8,691,130,164,773,992,423,115,1807,1618,153,168,1213,719,291,316,311,110,24,608,0,127,131,142,196,232,75,248,412,275,1295,239,86,967,133,422,415,894,280,807,345,446,250,979,231,713,201,1009,208,444,43,1400,434,8,221,141,235,909,1018,340,0,178,1144,353,662,491,294,6,440,446,824,1392,379,269,1427,911,671,231,424,102,718,86,54,130,206,514,137,1075,1573,248,472,602,249,974,8,372,59,2,940,165,1132,327,1424,63,6,97,140,1302,439,1237,59,324,733,397,477,426,278,274,1636,745,41,269,257,51,173,503,88,1223,754,228,584,72,632,645,323,156,7,337,192,375,583,613,370,172,528,1282,360,208,17,208,802,22,67,290,242,458,809,6,1388,49,14,327,911,121,475,101,617,284,91,1,35,421,293,1419,602,143,142,168,657,472,219,345,411,115,387,5,494,383,348,85,1070,154,42,63,586,953,563,12,1263,788,762,222,351,730,42,643,877,522,775,698,564,604,155,191,430,5,386,787,120,470,433,564,955,411,1090,210,1096,933,109,70,279,287,736,390,1075,30,194,30,1318,473,727,100,584,1227,89,432,120,393,1080,185,500,847,117,319,54,160,32,114,700,12,36,681,53,1063,113,82,1625,1426,53,658,80,253,36,16,810,560,602,177,275,147,335,1237,1447,176,55,366,721,471,501,217,67,899,914,168,981,1177,898,1,9,704,602,787,1280,305,57,786,1696,211,63,55,351,14,151,932,757,810,1748,169,1018,125,849,234,102,823,300,127,6,849,1163,229,726,397,656,135,466,137,1247,811,807,366,209,1703,24,1219,45,161,353,274,572,397,899,646,32,137,439,1048,2,391,8,214,736,518,409,414,567,1262,155,102,178,1247,526,10,94,759,781,18,14,1518,68,295,905,478,1581,89,429,937,438,915,1110,659,615,1128,8,125,89,289,1084,905,254,1227,184,883,983,110,1,24,748,1408,828,1187,63,264,481,214
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn main() anyerror!void {
|
||||||
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||||
|
defer arena.deinit();
|
||||||
|
const alloc = &arena.allocator;
|
||||||
|
|
||||||
|
// Read our input
|
||||||
|
var f = try std.fs.cwd().openFile("input", .{});
|
||||||
|
defer f.close();
|
||||||
|
var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32));
|
||||||
|
defer alloc.free(contents);
|
||||||
|
|
||||||
|
var values = std.ArrayList(u16).init(alloc);
|
||||||
|
defer values.deinit();
|
||||||
|
var it = std.mem.tokenize(contents, "\n");
|
||||||
|
while (it.next()) |line| {
|
||||||
|
var lit = std.mem.tokenize(line, ",");
|
||||||
|
while (lit.next()) |value| {
|
||||||
|
try values.append(try std.fmt.parseInt(u16, value, 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//std.log.debug("{} items", .{values.items.len});
|
||||||
|
std.sort.sort(u16, values.items, {}, comptime std.sort.asc(u16));
|
||||||
|
//std.log.debug("{any}", .{values.items});
|
||||||
|
|
||||||
|
// Try the median
|
||||||
|
var median = values.items[values.items.len/2];
|
||||||
|
std.log.debug("Median position {}, value: {}",
|
||||||
|
.{values.items.len/2, median});
|
||||||
|
|
||||||
|
var fuel_required: u64 = 0;
|
||||||
|
for (values.items) |v| {
|
||||||
|
if (v < median) {
|
||||||
|
fuel_required += @as(u64, median - v);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fuel_required += @as(u64, v - median);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std.log.info("[Part 1] {} fuel required to get to median {}",
|
||||||
|
.{fuel_required, median});
|
||||||
|
|
||||||
|
var i = values.items[0];
|
||||||
|
const max = values.items[values.items.len-1];
|
||||||
|
fuel_required = std.math.maxInt(u32);
|
||||||
|
var best_position: u16 = 0;
|
||||||
|
while (i <= max) : (i += 1) {
|
||||||
|
var fu = calculate_fuel_to_pos(i, values.items[0..]);
|
||||||
|
if (fu < fuel_required) {
|
||||||
|
fuel_required = fu;
|
||||||
|
best_position = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std.log.info("[Part 2] {} fuel required to get to best pos {}",
|
||||||
|
.{fuel_required, best_position});
|
||||||
|
}
|
||||||
|
|
||||||
|
// move 1: 1 = 1
|
||||||
|
// move 2: 1 + 2 = 3
|
||||||
|
// move 3: 1 + 2 + 3 = 6
|
||||||
|
// move 4: (1+2+3)+4 = 10
|
||||||
|
// move n: (1/2)n * (n+1)
|
||||||
|
fn calculate_fuel_to_pos(position: u16, crabs: []u16) u64 {
|
||||||
|
var fuel_required: u64 = 0;
|
||||||
|
for (crabs) |c| {
|
||||||
|
var delta: u64 = 0;
|
||||||
|
if (c < position) {
|
||||||
|
delta = @as(u64, position - c);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
delta = @as(u64, c - position);
|
||||||
|
}
|
||||||
|
fuel_required += (delta*(delta+1)) / 2;
|
||||||
|
}
|
||||||
|
return fuel_required;
|
||||||
|
}
|
Loading…
Reference in New Issue