Day 1 part 2

This commit is contained in:
Adam Millerchip 2022-08-11 18:45:13 +09:00
parent 26f694c449
commit ba09492857
1 changed files with 18 additions and 37 deletions

View File

@ -1,13 +1,7 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const tokenize = std.mem.tokenize;
const List = std.ArrayList; const parseInt = std.fmt.parseInt;
const Map = std.AutoHashMap; const print = std.debug.print;
const StrMap = std.StringHashMap;
const BitSet = std.DynamicBitSet;
const Str = []const u8;
const util = @import("util.zig");
const gpa = util.gpa;
const data = @embedFile("../data/day01.txt"); const data = @embedFile("../data/day01.txt");
@ -25,8 +19,10 @@ pub fn main() !void {
} }
try part1(buffer[0..i]); try part1(buffer[0..i]);
try part2(buffer[0..i]);
} }
fn part1(input: []u16) !void { fn part1(input: []u16) !void {
for (input) |a, i| { for (input) |a, i| {
for (input[(i+1)..]) |b| { for (input[(i+1)..]) |b| {
@ -37,33 +33,18 @@ fn part1(input: []u16) !void {
} }
} }
} }
} }
// Useful stdlib functions fn part2(input: []u16) !void {
const tokenize = std.mem.tokenize; for (input) |a, i| {
const split = std.mem.split; for (input[(i+1)..]) |b, j| {
const indexOf = std.mem.indexOfScalar; for (input[(j+1)..]) |c| {
const indexOfAny = std.mem.indexOfAny; if (a + b + c == 2020) {
const indexOfStr = std.mem.indexOfPosLinear; const answer = @as(u32, a) * b * c;
const lastIndexOf = std.mem.lastIndexOfScalar; print("Part2: {d}\n", .{answer});
const lastIndexOfAny = std.mem.lastIndexOfAny; return;
const lastIndexOfStr = std.mem.lastIndexOfLinear; }
const trim = std.mem.trim; }
const sliceMin = std.mem.min; }
const sliceMax = std.mem.max; }
}
const parseInt = std.fmt.parseInt;
const parseFloat = std.fmt.parseFloat;
const min = std.math.min;
const min3 = std.math.min3;
const max = std.math.max;
const max3 = std.math.max3;
const print = std.debug.print;
const assert = std.debug.assert;
const sort = std.sort.sort;
const asc = std.sort.asc;
const desc = std.sort.desc;