Solved day 10, much easier than day 9!

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2024-12-12 00:27:51 +00:00
parent 62723e5129
commit f78e6ff427
4 changed files with 235 additions and 0 deletions

8
inputs/day10.example.txt Normal file
View file

@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732

59
inputs/day10.txt Normal file
View file

@ -0,0 +1,59 @@
43450121898101101123127821096542109876109701010178732100123
89543230767213210011018912987433234565018892121269823445674
76692145654300398322167903876323398934327743434454310538985
45781096890121497413456874565012367898456658945893239627876
34301587765432586504010567654101256787650167876761128716321
69212657654123677345623498903104345696543238945890029605430
78798748783014988932764567012254330165456783034321212567876
65671239692385976341873018898361221074325892103567103432987
54980178501096845320962129765470398983016783212458932121096
63056789432087730410434038654380147812705654426354345087125
12141010098121621562345549783491236503890123345243216190104
01632120127230541071076678892100345654387654216104507889213
34567836736549032987986323451215456789218943007897628974312
21456945845678123478967810560432365432100122106498710165403
10143210954321787561058927678101870122239231254327633258932
41014787665650196232347678879230989211078540760019824567541
32989690198754345145432589965447854302567659861278212345670
45676543282663232076501430012356967653438945876367109454981
12565654101078101089696321104321898301465438965454308763676
03443233210149230456787012765210147432378921087643214324565
12354104303230142367017633894323456343489012196543285614321
21067865496549651098198544565210769853458786787230198705210
30128956787678741287230133478901898762367698690124354306787
81434567874589230276541024321092432191056567547843265217895
98543210903270141125432011036786543087171256456950174307876
87690123210101432034545822345677604456980344323868789678921
76587434901219541002196953210578912347876501012879652541010
54106545890178654910087864321417823218985012508934541032781
67812656783267067823105675699101830105674327654325872305692
58923565654352158934234789788542987234501458965210987412543
45434565432543112985109898767653676543432569867105676543014
36585676501265003876501234650344567012569678458943489872101
23096987687652134561010105641256788656778323345432156789210
10187321098343458992876596030143299349865410236721005674321
43235497654210167783945987123232101238567892105875414565467
56986788323456434654834565696540342101056701458987323541058
67898129010987543210121078987661254322347892364379834432149
78107038895676695434010187545676765011056789875210778940430
69256546784987786543421296234989807652340676966901667651321
52347875273278987102210345101269218943401205457812567892343
41016934109109873201783210789878347892112312348903454987654
32105123258056724345694569650565756765089401232120123396323
65414014567145014566785478641214898706576587143089875431214
72343217654231123875073456732303743217434896054108566520301
81056108903210154965122109841012656198920125565211257015432
90087612112310568985431018956655654007611234474340348976301
87196456089423478976980167567743763014501765389478930985412
65280367176532032985873265498892892123349874210567321476323
74361298266541141034564972301281045891256103287656212389892
89450127347450230123455881214321236760961210196552101456701
30567255478978743789846790326760987654870300123467896565212
21498766967669652650176543439856101543065465410567887634343
12301457873456781043281012945347892032176978323436998325654
01232346122369897176895678876232103124987869835625213218745
78903498031078108985956589965109654435438932148714301009012
67612567948943267234898434453458789054323845089605687128321
56521019857654456110767021342167658167210756789598796437450
65430018767890301023456130256098943278130349823410145589869
78942123454321212154343210107897650129021231014543234676578

81
src/day10-2.zig Normal file
View file

@ -0,0 +1,81 @@
const std = @import("std");
const print = std.debug.print;
const args = @import("lib/args.zig");
const files = @import("lib/files.zig");
inline fn normalize(c: u8) u8 {
return switch (c) {
'0'...'9' => c - '0',
else => unreachable,
};
}
fn printMap(m: [60][60]u8, size: usize) void {
for (0..size) |y| {
for (0..size) |x| {
print("{c}", .{m[y][x]});
}
print("\n", .{});
}
}
// This approach does not take into account trails tha bifurcate.
//
// By the problem definition:
//
// 0
// 1
// 43234
// 5 5
// 6 6
// 78987
//
// The example above contains one trilhead with a score of one, but this
// implementation would considerit a a score of 2.
fn updateScores(m: [60][60]u8, s: *[60][60]u16, size: usize, y: usize, x: usize) void {
if (m[y][x] == '0') {
s[y][x] += 1;
return;
}
const c = m[y][x];
if (x > 0 and m[y][x - 1] == c - 1) {
updateScores(m, s, size, y, x - 1);
}
if (x < size - 1 and m[y][x + 1] == c - 1) {
updateScores(m, s, size, y, x + 1);
}
if (y > 0 and m[y - 1][x] == c - 1) {
updateScores(m, s, size, y - 1, x);
}
if (y < size - 1 and m[y + 1][x] == c - 1) {
updateScores(m, s, size, y + 1, x);
}
}
pub fn main() !void {
const input_path = args.getFirstArg();
const input = try files.openForReading(input_path);
var map: [60][60]u8 = undefined;
var scores: [60][60]u16 = .{.{0} ** 60} ** 60;
var map_size: usize = 0;
while (files.readLine(input, &map[map_size])) |_| : (map_size += 1) {} else |err| {
print("done reading input with error: {any}\n", .{err});
}
for (0..map_size) |y| {
for (0..map_size) |x| {
if (map[y][x] == '9') {
updateScores(map, &scores, map_size, y, x);
}
}
}
var score: usize = 0;
for (0..map_size) |y| {
for (0..map_size) |x| {
score += scores[y][x];
}
}
print("Scores: {d}\n", .{score});
}

87
src/day10.zig Normal file
View file

@ -0,0 +1,87 @@
const std = @import("std");
const print = std.debug.print;
const args = @import("lib/args.zig");
const files = @import("lib/files.zig");
inline fn normalize(c: u8) u8 {
return switch (c) {
'0'...'9' => c - '0',
else => unreachable,
};
}
fn printMap(m: [60][60]u8, size: usize) void {
for (0..size) |y| {
for (0..size) |x| {
print("{c}", .{m[y][x]});
}
print("\n", .{});
}
}
// This approach does not take into account trails tha bifurcate.
//
// By the problem definition:
//
// 0
// 1
// 43234
// 5 5
// 6 6
// 78987
//
// The example above contains one trilhead with a score of one, but this
// implementation would considerit a a score of 2.
fn updateScores(m: [60][60]u8, s: *[60][60]u16, v: *[60][60]bool, size: usize, y: usize, x: usize) void {
if (m[y][x] == '0') {
if (!v[y][x]) {
s[y][x] += 1;
v[y][x] = true;
}
return;
}
const c = m[y][x];
if (x > 0 and m[y][x - 1] == c - 1) {
updateScores(m, s, v, size, y, x - 1);
}
if (x < size - 1 and m[y][x + 1] == c - 1) {
updateScores(m, s, v, size, y, x + 1);
}
if (y > 0 and m[y - 1][x] == c - 1) {
updateScores(m, s, v, size, y - 1, x);
}
if (y < size - 1 and m[y + 1][x] == c - 1) {
updateScores(m, s, v, size, y + 1, x);
}
}
pub fn main() !void {
const input_path = args.getFirstArg();
const input = try files.openForReading(input_path);
var map: [60][60]u8 = undefined;
var scores: [60][60]u16 = .{.{0} ** 60} ** 60;
var visited: [60][60]bool = undefined;
var map_size: usize = 0;
while (files.readLine(input, &map[map_size])) |_| : (map_size += 1) {} else |err| {
print("done reading input with error: {any}\n", .{err});
}
for (0..map_size) |y| {
for (0..map_size) |x| {
if (map[y][x] == '9') {
// We only want to visit every 0 once per 9.
@memset(&visited, .{false} ** 60);
updateScores(map, &scores, &visited, map_size, y, x);
}
}
}
var score: usize = 0;
for (0..map_size) |y| {
for (0..map_size) |x| {
score += scores[y][x];
}
}
print("Scores: {d}\n", .{score});
}