ONQOGY2EUWGA5ATHOESEQHAQJLBSNPX7MAHY53SOC3R2IPXSVJ6AC
test {
_ = @import("day01.zig");
}
const std = @import("std");
const aoc2020 = struct {
const day01 = @import("day01.zig");
};
const days = 1;
pub fn main() anyerror!void {
var timer = try std.time.Timer.start();
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var buffer = std.io.bufferedWriter(std.io.getStdOut().writer());
const stdout = buffer.writer();
try stdout.print("{s:<8}{s:>20}{s:>20}\n", .{"Day", "Result", "Time"});
comptime var day: u5 = 0;
inline while (day < days) : (day += 1) {
comptime var buf: [5]u8 = undefined;
const day_str = comptime try std.fmt.bufPrint(&buf, "day{d:0>2}", .{day + 1});
timer.reset();
const first_res = @field(aoc2020, day_str).first(arena.allocator()) catch unreachable;
const first_time = timer.read() / std.time.ns_per_us;
try stdout.print("{s}a: {d:>20}{d:>20} us\n", .{ day_str, first_res, first_time });
if (day < 24) {
timer.reset();
const second_res = @field(aoc2020, day_str).second(arena.allocator()) catch unreachable;
const second_time = timer.read() / std.time.ns_per_us;
try stdout.print("{s}b: {d:>20}{d:>20} us\n", .{ day_str, second_res, second_time });
}
}
try buffer.flush();
}
const std = @import("std");
const PATH = "../input/day01.txt";
const MAX_LINES = 200;
pub fn first(allocator: ?std.mem.Allocator) anyerror!usize {
_ = allocator;
const input = try parseInput();
for (input) |item| {
for (input) |elem| {
if (item + elem == 2020) {
return item * elem;
}
}
}
unreachable;
}
pub fn second(allocator: ?std.mem.Allocator) anyerror!usize {
_ = allocator;
const input = try parseInput();
for (input) |item| {
for (input) |elem| {
for (input) |third| {
if (item + elem + third == 2020) {
return item * elem * third;
}
}
}
}
unreachable;
}
fn parseInput() ![]usize {
const file = @embedFile(PATH);
var lines = std.mem.tokenize(u8, file, "\n");
var ret: [MAX_LINES]usize = undefined;
var counter: usize = 0;
while (lines.next()) |line| : (counter += 1) {
ret[counter] = try std.fmt.parseUnsigned(usize, line, 0);
}
return &ret;
}
test "day01a" {
try std.testing.expectEqual(@as(usize, 482811), try first(std.testing.allocator));
}
test "day01b" {
try std.testing.expectEqual(@as(usize, 193171814), try second(std.testing.allocator));
}
1714
1960
1256
1597
1853
1609
1936
2003
1648
1903
1248
1525
1330
1281
1573
1892
1563
1500
1858
176
1802
1370
1708
1453
1342
1830
1580
1607
1848
1626
1602
1919
1640
1574
1414
766
1581
1924
1727
1949
1406
323
957
1862
1354
1427
1583
1067
1863
1553
1923
1990
691
1372
1357
1887
1485
1799
1270
1743
1601
1457
1723
1888
1272
1600
1880
1381
1413
1452
277
1866
1542
1693
1760
1637
1675
1975
1304
1327
1985
1842
1255
1915
1266
1944
1824
1770
1392
1259
1313
1547
1293
1393
1896
1828
1642
1979
1871
1502
1548
1508
710
1786
1845
1334
1362
1940
2009
1271
1448
1964
1676
1654
1804
1835
1910
1939
1298
1572
1704
1841
1399
1576
1164
1868
1035
262
1569
1639
1669
1543
1616
1658
1750
1765
1718
1861
1351
1531
1665
1771
1348
1289
875
1408
1486
1275
1625
1594
1816
704
1800
1564
1291
1234
1981
1843
1387
1938
1827
1883
1911
1755
1353
1808
1498
1416
2006
1916
1411
1539
1963
1874
1898
1951
1292
1366
1912
1369
1478
1359
1859
1421
1384
1534
1283
1913
1794
1494
1860
1312
1869
1730
1510
1319
1428
1706
1432
1532
const std = @import("std");
// number of days available
const days = 1;
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("aoc2020", "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 all solver");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main_test.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}
.git
.DS_Store
**/zig-cache
/zig-out