diff --git a/src/aoc.zig b/src/aoc.zig index 9109ab3..024b3ea 100644 --- a/src/aoc.zig +++ b/src/aoc.zig @@ -31,7 +31,7 @@ pub const Day = struct { fn expectAnswer(comptime T: type, solver: Solver, expected: T, lines: []const []const u8) !void { var input = Input{ .lines = lines, .allocator = std.testing.allocator }; var result: Result = try solver(&input); - var actual = result.value(T) orelse return error.ResultTypeMismatch; + const actual = result.value(T) orelse return error.ResultTypeMismatch; try std.testing.expectEqual(expected, actual); } diff --git a/src/day1.zig b/src/day1.zig index 9c8cefb..136a99c 100644 --- a/src/day1.zig +++ b/src/day1.zig @@ -23,7 +23,7 @@ pub fn part1(input: *aoc.Input) !aoc.Result { } } - var number = 10*first_digit + last_digit; + const number = 10*first_digit + last_digit; sum += number; } @@ -49,9 +49,9 @@ pub fn part2(input: *aoc.Input) !aoc.Result { } for (0.., digits) |i, digit| { - var occurence_opt = std.mem.indexOf(u8, line, digit); + const occurence_opt = std.mem.indexOf(u8, line, digit); if (occurence_opt == null) continue; - var occurence = occurence_opt.?; + const occurence = occurence_opt.?; if (found_first and occurence > first_idx) continue; @@ -74,9 +74,9 @@ pub fn part2(input: *aoc.Input) !aoc.Result { } for (0.., digits) |i, digit| { - var occurence_opt = std.mem.lastIndexOf(u8, line, digit); + const occurence_opt = std.mem.lastIndexOf(u8, line, digit); if (occurence_opt == null) continue; - var occurence = occurence_opt.?; + const occurence = occurence_opt.?; if (found_last and occurence < last_idx) continue; @@ -86,7 +86,7 @@ pub fn part2(input: *aoc.Input) !aoc.Result { } } - var number = 10*first_digit + last_digit; + const number = 10*first_digit + last_digit; sum += number; } diff --git a/src/day10.zig b/src/day10.zig index 1af0bf5..d1da91a 100644 --- a/src/day10.zig +++ b/src/day10.zig @@ -147,15 +147,15 @@ fn parse_tile(char: u8) !Tile { } fn parse_input(allocator: Allocator, lines: []const []const u8) !Grid { - var height: u32 = @intCast(lines.len); - var width: u32 = @intCast(lines[0].len); + const height: u32 = @intCast(lines.len); + const width: u32 = @intCast(lines[0].len); var tiles = try allocator.alloc(Tile, width * height); errdefer allocator.free(tiles); for (0.., lines) |y, line| { for (0.., line) |x, char| { - var idx = y * width + x; + const idx = y * width + x; tiles[idx] = try parse_tile(char); } } @@ -193,8 +193,8 @@ fn get_neighbours(grid: *const Grid, x: u32, y: u32) std.BoundedArray(Point, 4) const ox = offset[0]; const oy = offset[1]; - var nx: i32 = @intCast(@as(i32, @intCast(x)) + ox ); - var ny: i32 = @intCast(@as(i32, @intCast(y)) + oy ); + const nx: i32 = @intCast(@as(i32, @intCast(x)) + ox ); + const ny: i32 = @intCast(@as(i32, @intCast(y)) + oy ); if (grid.in_bounds(nx, ny)) { neighbours.append(Point{ .x = @intCast(nx), .y = @intCast(ny) }) catch unreachable; } @@ -253,8 +253,8 @@ fn get_connections(grid: *const Grid, x: u32, y: u32) std.BoundedArray(Direction const ox = direction_tuple[1]; const oy = direction_tuple[2]; - var nx: i32 = @intCast(@as(i32, @intCast(x)) + ox ); - var ny: i32 = @intCast(@as(i32, @intCast(y)) + oy ); + const nx: i32 = @intCast(@as(i32, @intCast(x)) + ox ); + const ny: i32 = @intCast(@as(i32, @intCast(y)) + oy ); if (grid.in_bounds(nx, ny)) { const tile = grid.get(@intCast(nx), @intCast(ny)); if (@as(TileType, tile) == TileType.Pipe and does_pipe_have_direction(tile.Pipe, opposite_direction)) { @@ -350,7 +350,7 @@ pub fn part2(input: *aoc.Input) !aoc.Result { twice_area += (x_0 * y_1) - (y_0 * x_1); } - var result = std.math.absCast(twice_area) / 2 - (loop_path.items.len / 2 - 1); + const result = @abs(twice_area) / 2 - (loop_path.items.len / 2 - 1); return .{ .uint = result }; } diff --git a/src/day11.zig b/src/day11.zig index 572e35d..8339bcf 100644 --- a/src/day11.zig +++ b/src/day11.zig @@ -46,8 +46,8 @@ const GalaxyMap = struct { }; fn parse_input(allocator: Allocator, lines: []const []const u8) !GalaxyMap { - var height: u32 = @intCast(lines.len); - var width: u32 = @intCast(lines[0].len); + const height: u32 = @intCast(lines.len); + const width: u32 = @intCast(lines[0].len); var tile_map = try allocator.alloc(Tile, width * height); errdefer allocator.free(tile_map); @@ -163,7 +163,7 @@ fn sum_min_distances(points: []Point) u64 { const p2 = points[j]; const dx = @as(i64, @intCast(p1.x)) - @as(i64, @intCast(p2.x)); const dy = @as(i64, @intCast(p1.y)) - @as(i64, @intCast(p2.y)); - result += std.math.absCast(dx) + std.math.absCast(dy); + result += @abs(dx) + @abs(dy); } } return result; diff --git a/src/day12.zig b/src/day12.zig index 9b5a586..1b6187e 100644 --- a/src/day12.zig +++ b/src/day12.zig @@ -297,7 +297,7 @@ fn count_valid_arrangements_dp(cache: *Cache, states: []const SpringState, group } } - var cache_key = CacheKey{ .states = states, .groups = groups }; + const cache_key = CacheKey{ .states = states, .groups = groups }; if (cache.get(cache_key)) |cached_result| { return cached_result; } diff --git a/src/day13.zig b/src/day13.zig index 8e1657e..c94b2bc 100644 --- a/src/day13.zig +++ b/src/day13.zig @@ -51,8 +51,8 @@ const Input = struct { }; fn parse_map(allocator: Allocator, lines: []const []const u8) !Map { - var width: u32 = @intCast(lines[0].len); - var height: u32 = @intCast(lines.len); + const width: u32 = @intCast(lines[0].len); + const height: u32 = @intCast(lines.len); var tiles = try std.ArrayList(bool).initCapacity(allocator, width*height); errdefer tiles.deinit(); diff --git a/src/day16.zig b/src/day16.zig index f55250a..5f40fc9 100644 --- a/src/day16.zig +++ b/src/day16.zig @@ -238,7 +238,7 @@ pub fn part1(input: *aoc.Input) !aoc.Result { const map = try parse_input(input.allocator, input.lines); defer map.deinit(); - var energy_map = try simulate_beam(allocator, map, PointU32.zero(), RIGHT); + const energy_map = try simulate_beam(allocator, map, PointU32.zero(), RIGHT); defer allocator.free(energy_map); // print_energy_map(map, energy_map); diff --git a/src/day2.zig b/src/day2.zig index 2c3fb4e..cf96558 100644 --- a/src/day2.zig +++ b/src/day2.zig @@ -33,10 +33,10 @@ fn parse_game(game: *Game, line: []const u8) void { var cubes = std.mem.splitSequence(u8, game_set_str, ", "); while (cubes.next()) |cube_count| { - var space = std.mem.indexOf(u8, cube_count, " ") orelse @panic("Invalid format, expected space"); + const space = std.mem.indexOf(u8, cube_count, " ") orelse @panic("Invalid format, expected space"); - var cube = parse_cube(cube_count[(space+1)..]) orelse @panic("Invalid cube name"); - var count = std.fmt.parseInt(u32, cube_count[0..space], 10) catch @panic("Invalid format"); + const cube = parse_cube(cube_count[(space+1)..]) orelse @panic("Invalid cube name"); + const count = std.fmt.parseInt(u32, cube_count[0..space], 10) catch @panic("Invalid format"); cube_set.set(cube, count); } } @@ -54,7 +54,7 @@ pub fn part1(input: *aoc.Input) !aoc.Result { const lines = input.lines; const allocator = input.allocator; - var games = try parse_games(allocator, lines); + const games = try parse_games(allocator, lines); defer allocator.free(games); const max_red = 12; @@ -81,7 +81,7 @@ pub fn part1(input: *aoc.Input) !aoc.Result { pub fn part2(input: *aoc.Input) !aoc.Result { const lines = input.lines; const allocator = input.allocator; - var games = try parse_games(allocator, lines); + const games = try parse_games(allocator, lines); defer allocator.free(games); var sum: u32 = 0; diff --git a/src/day5.zig b/src/day5.zig index 7d27d70..001d6bc 100644 --- a/src/day5.zig +++ b/src/day5.zig @@ -115,7 +115,7 @@ pub fn part1(input: *aoc.Input) !aoc.Result { var lowest_location: ?u64 = null; for (parsed.seeds.slice()) |seed| { - var value = apply_seed_to_location(seed, &parsed); + const value = apply_seed_to_location(seed, &parsed); if (lowest_location) |location| { lowest_location = @min(location, value); diff --git a/src/day6.zig b/src/day6.zig index bc5cc89..e4e3272 100644 --- a/src/day6.zig +++ b/src/day6.zig @@ -91,7 +91,7 @@ pub fn part2(input: *aoc.Input) !aoc.Result { } } - var answer = max_time - min_time + 1; + const answer = max_time - min_time + 1; return .{ .uint = @intCast(answer) }; } diff --git a/src/day7.zig b/src/day7.zig index ce84a1c..d003f20 100644 --- a/src/day7.zig +++ b/src/day7.zig @@ -100,7 +100,7 @@ fn count_cards(hand: Hand) CardCounts { } fn determine_card_strength(hand: Hand) HandStrength { - var counts = count_cards(hand); + const counts = count_cards(hand); var ones_count: u4 = 0; var two_count: u4 = 0; diff --git a/src/day8.zig b/src/day8.zig index 5fba317..29c0654 100644 --- a/src/day8.zig +++ b/src/day8.zig @@ -248,7 +248,7 @@ pub fn part2(input: *aoc.Input) !aoc.Result { for (parsed.nodes.items) |node_junction| { if (node_junction.from[2] == comptime parse_char_to_node_part('A')) { - var steps = find_steps_needed(&node_map, parsed.instructions.items, node_junction.from); + const steps = find_steps_needed(&node_map, parsed.instructions.items, node_junction.from); try all_steps.append(steps); } } diff --git a/src/day9.zig b/src/day9.zig index 049a503..48c677e 100644 --- a/src/day9.zig +++ b/src/day9.zig @@ -90,7 +90,7 @@ pub fn part1(input: *aoc.Input) !aoc.Result { for (parsed.histories.items) |history| { const numbers = history.items; - var changes = try all_changes_of_history(allocator, numbers); + const changes = try all_changes_of_history(allocator, numbers); defer free_history_changes(changes); var prediction: i32 = numbers[numbers.len-1]; @@ -113,7 +113,7 @@ pub fn part2(input: *aoc.Input) !aoc.Result { for (parsed.histories.items) |history| { const numbers = history.items; - var changes = try all_changes_of_history(allocator, numbers); + const changes = try all_changes_of_history(allocator, numbers); defer free_history_changes(changes); var prediction: i32 = 0; diff --git a/src/main.zig b/src/main.zig index 1a3a502..eaf3f3d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -37,6 +37,7 @@ const Days = [_]aoc.Day{ create_day(@import("./day14.zig")), create_day(@import("./day15.zig")), create_day(@import("./day16.zig")), + create_day(@import("./day17.zig")), }; fn kilobytes(count: u32) u32 { @@ -49,7 +50,7 @@ fn run(allocator: Allocator, args: *cli) !u8 { return 255; } - var day = Days[args.day-1]; + const day = Days[args.day-1]; if ((args.part == 1 and day.part1 == null) or (args.part == 2 and day.part2 == null)) { std.debug.print("ERROR: Part {} for day {} does not exist\n", .{args.part, args.day}); return 255; @@ -81,7 +82,7 @@ fn run(allocator: Allocator, args: *cli) !u8 { var input = aoc.Input{ .allocator = allocator, .lines = lines.items }; - var start_time = std.time.microTimestamp(); + const start_time = std.time.microTimestamp(); var result: aoc.Result = undefined; if (args.part == 1) { result = try day.part1.?(&input); @@ -107,32 +108,33 @@ fn get_input(allocator: Allocator, args: *cli) !u8 { var client = std.http.Client{ .allocator = allocator }; defer client.deinit(); - var headers = std.http.Headers{ .allocator = allocator }; - defer headers.deinit(); - if (!try std.process.hasEnvVar(allocator, "AOC_SESSION")) { std.debug.print("ERROR: Missing environment variable AOC_SESSION\n", .{}); return 255; } - var aoc_session = try std.process.getEnvVarOwned(allocator, "AOC_SESSION"); + const aoc_session = try std.process.getEnvVarOwned(allocator, "AOC_SESSION"); defer allocator.free(aoc_session); - var cookie_header = try std.fmt.allocPrint(allocator, "session={s}", .{aoc_session}); + const cookie_header = try std.fmt.allocPrint(allocator, "session={s}", .{aoc_session}); defer allocator.free(cookie_header); - try headers.append("cookie", cookie_header); - try headers.append("accept", "*/*"); - - var url_str = try std.fmt.allocPrint(allocator, "https://adventofcode.com/2023/day/{}/input", .{args.day}); + const url_str = try std.fmt.allocPrint(allocator, "https://adventofcode.com/2023/day/{}/input", .{args.day}); defer allocator.free(url_str); const uri = std.Uri.parse(url_str) catch unreachable; - var request = try client.request(.GET, uri, headers, .{}); + var server_header_buffer: [1024]u8 = undefined; + var request = try client.open(.GET, uri, .{ + .server_header_buffer = &server_header_buffer, + .extra_headers = &.{ + .{ .name = "cookie", .value = cookie_header }, + .{ .name = "accept", .value = "*/*" }, + } + }); defer request.deinit(); - try request.start(); + try request.send(); try request.wait(); if (request.response.status != .ok) { @@ -195,4 +197,5 @@ test { _ = @import("./day14.zig"); _ = @import("./day15.zig"); _ = @import("./day16.zig"); + _ = @import("./day17.zig"); }