-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const std = @import("std"); | ||
const utils = @import("utils.zig"); | ||
|
||
const HikeTrail = enum { | ||
Start, | ||
One, | ||
Two, | ||
Three, | ||
Four, | ||
Five, | ||
Six, | ||
Seven, | ||
Eight, | ||
End, | ||
|
||
Empty, | ||
}; | ||
|
||
fn parse_map(alloc: std.mem.Allocator, file: []const u8) !std.ArrayList([]HikeTrail) { | ||
const width = std.mem.indexOf(u8, file, "\n").?; | ||
|
||
var map = try std.ArrayList([]HikeTrail).initCapacity(alloc, width); | ||
errdefer map.deinit(); | ||
|
||
var it = std.mem.tokenizeAny(u8, file, "\n"); | ||
while (it.next()) |raw_line| { | ||
var line = try std.ArrayList(HikeTrail).initCapacity(alloc, width); | ||
for (raw_line) |tile| | ||
line.appendAssumeCapacity(if (tile >= '0' and tile <= '9') | ||
@enumFromInt(tile - '0') | ||
else if (tile == '.') .Empty else unreachable); | ||
map.appendAssumeCapacity(try line.toOwnedSlice()); | ||
} | ||
return map; | ||
} | ||
|
||
const Coordiates = struct { | ||
x: i64, | ||
y: i64, | ||
|
||
inline fn add(self: Coordiates, other: Coordiates) Coordiates { | ||
return .{ .x = self.x + other.x, .y = self.y + other.y }; | ||
} | ||
}; | ||
|
||
fn compute(comptime part_1: bool, map: [][]HikeTrail, pos: Coordiates) usize { | ||
var result: usize = 0; | ||
|
||
const expect: HikeTrail = | ||
@enumFromInt(@intFromEnum(map[@intCast(pos.y)][@intCast(pos.x)]) + 1); | ||
for (&[_]Coordiates{ | ||
pos.add(.{ .x = 0, .y = -1 }), | ||
pos.add(.{ .x = -1, .y = 0 }), | ||
pos.add(.{ .x = 1, .y = 0 }), | ||
pos.add(.{ .x = 0, .y = 1 }), | ||
}) |other| { | ||
if (other.y >= map.len or other.y < 0) | ||
continue; | ||
const line = &map[@intCast(other.y)]; | ||
if (other.x >= line.len or other.x < 0) | ||
continue; | ||
const tile = &line.*[@intCast(other.x)]; | ||
if (tile.* != expect) | ||
continue; | ||
if (tile.* == .End) { | ||
if (part_1) | ||
tile.* = .Empty; | ||
result += 1; | ||
} else { | ||
result += compute(part_1, map, other); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
fn deinit(array: *std.ArrayList([]HikeTrail)) void { | ||
for (array.items) |l| { | ||
array.allocator.free(l); | ||
} | ||
array.deinit(); | ||
} | ||
|
||
pub fn solution(alloc: std.mem.Allocator) !utils.AOCSolution { | ||
const file = @embedFile("input/day10.txt"); | ||
var map = try parse_map(alloc, file); | ||
defer deinit(&map); | ||
|
||
var sol = utils.AOCSolution{ .part1 = 0, .part2 = 0 }; | ||
for (0.., map.items) |y, line| { | ||
for (0.., line) |x, _| { | ||
if (map.items[y][x] == .Start) { | ||
var m = try map.clone(); | ||
defer deinit(&m); | ||
for (m.items) |*l| { | ||
l.* = try alloc.dupe(HikeTrail, l.*); | ||
} | ||
sol.part1 += compute(true, m.items, .{ .x = @intCast(x), .y = @intCast(y) }); | ||
sol.part2 += compute(false, map.items, .{ .x = @intCast(x), .y = @intCast(y) }); | ||
} | ||
} | ||
} | ||
|
||
return sol; | ||
} | ||
|
||
test { | ||
const sol = try solution(std.testing.allocator); | ||
|
||
try std.testing.expectEqual(593, sol.part1); | ||
try std.testing.expectEqual(1192, sol.part2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
210121012321234586543237650120901076540121001 | ||
101234985490147897890198701221812389430430012 | ||
567105676787458721209877654336765410321569123 | ||
498014785010969630118960165445898923210678234 | ||
309323494323874549823454278321080854100123945 | ||
211234515690123678989923329876571765987637876 | ||
110987601781010501677815410989432894543540345 | ||
021010532782323432596506901098126545652901276 | ||
012123445693437010487417892387087036761892987 | ||
323098530696598923300306103456592123890743210 | ||
014567621787127654211215296503431014125650309 | ||
329643452650034018987894387112321265034321478 | ||
498762167541005329876012379012450996543406567 | ||
567433098932116456705430498763567887456715458 | ||
016526187654327643212321567654654321019823309 | ||
327817656783298984103435433216789670321094215 | ||
438908943090125675678976124105894589452385434 | ||
567617812181034106789780045674583076581276123 | ||
106787801654323295610191230983012109690187012 | ||
245096998745016784327898101092103298781298432 | ||
332165432101565410678765432387654567654382321 | ||
456976541001274328769867211498565650103401450 | ||
697889034012389219654358300567656743212519567 | ||
787765121965476101011249401456549865435658498 | ||
657854320876210894320134562340134678321067019 | ||
548943013456367885012021076543221589801354323 | ||
038765412387478976523256789601100499832231234 | ||
129914302198565985430143898702341300740140145 | ||
878801243089478876943232169219654211655657656 | ||
965760651078769217854321078348778902598748912 | ||
054450782369854308765010127651267143456732103 | ||
123321895451043219678098763410356012349801654 | ||
103456986032112008769123452345441078959798723 | ||
212387872149800123452101011096732163478760810 | ||
301095433458945654343000125687823454307451923 | ||
454346721567238769289210234776916543210312367 | ||
965239810123189378176321349885407892321208498 | ||
870125676034003210065443456790398901765089567 | ||
345676787165612349898652105381210340894176506 | ||
210985696278973656776789965450678256783215410 | ||
101974320161087298785649874367589109891006321 | ||
007876013232196105694234701298432098782107890 | ||
012565434983435234523105650165601105673456921 | ||
323476325876524321012023498074320234504305430 | ||
434789016789012010123110567789210343212214321 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters