day 8 (Failed)

This commit is contained in:
Dusk 2022-12-09 13:29:40 +01:00
parent f00e9cf066
commit bd30807378
4 changed files with 237 additions and 0 deletions

Binary file not shown.

Binary file not shown.

99
day8/input Normal file
View File

@ -0,0 +1,99 @@
020110220332333020110144320304042020444223003535441353331002333431100300241023221210123003331021020
002120010112022233203323334422340102033151553341235324543343233301202102130210343113312320222102020
000000000301021002423224442341031145215244543444223314545524515335404222310244314423023331200102012
011200302010212334422213334343335452314532352111122533515334412241512412033340322004232020212310011
110111200102310120411101020333232255345143342245212323552344535253214434143032323213200301101020121
122001020113011314043121413025543341512335111452355331355351515421515411500421204113413212021112302
011210211003201420103123304513544114145311452334355144111521441452154223512022101102111221311323311
011030200312240341224434214241313322323544154242345656634321531531424124544551344224331443202111303
031023120224010220144244342511241334331166432443322363352643453525244443222142533014331212133113031
331033331100013013431114214253543323143222523423236646456363626622312534521454454123324132323233200
313320023430241044213221132131122135436564345625426422255326234363243521151144414344130023210131130
002203011410101343345413454143416333665335233566235623536644652644522345535525153521230411343303211
022030323311323124542521154345356626352343324652424464566645425223224521453553252234321033403323130
331222243010032043452454232565665256426536435254566436633323343652534544214412242551144312310221231
231133233004211422411531255443363333555345434532475762623642433466363266634413551244254134333243030
020231113200002214145443133533534653326664654474363645366343345466265644554325342415122332200024311
311032340032414323451542554464466352256347635357637447736577667463426532442564245132242343241314212
210342413212232542421452232234546643373633347344743377376553465333423522425436534352355150302110320
012302042023135125251364365232626547463576733637455663764376436734352545425434331543241124042312202
304042410233125532432523224245663443763753734647533535565636563665334542556665554245524241400402122
100324144335411111343624365526556753335435457754653754663647544353454443256623466532421524442220212
004310002315421423634565264237567445665436743333467647547754434765353667542365233361541321530411122
340100133133254351662534562377644656735477678648447854646765556636735646456562322345353223545130204
223422234555541234336653556365464346366386775667444744567758637737757667635354324626442355153142402
301202352352311332364654446567354376644665887886878854847858588356564773766244445222614543212440041
121010254322255546522625235356663453785775455846758588786788567566653645763532436352663334521520342
223123122512444365553626454767563488464886467858877655766685458675437556363673526236645213411512224
003314511142456453664565476333555654845565878476575468776548666885476777366354622652365442341232330
100434351354142623236353735657745766665557455465675844665578767675874476556574642555262344222545124
310035122251134332246455666547787878577677667587668567788744875844888844364453536232254344234442004
332355524421462334544344655646458867688886798865795569865997666688465755335745555656646243433122421
212252432545222565265377376368574784887787696558698987968675547855656555445333366624445325141553342
113535121253534442546646364347455675758878676575695576785998796454646647555637657565266546254424513
023531155256556443573534665476784478579889966685965558767695667786588646763565366352565623135242132
332235311465633455637577656876574774599858977857599877979855878694546488485435736564352255345534352
423312331346353345545646578646466549968669789766955768696578957665488458568376747735452426221223133
042131555255526335474354376567744577697576655686887997959968659596857774864465577464334245541411144
314335533542324633457667677854784879668896688676989899996875778858675674784535777333543532331142411
342431321343652634443346854886679789759856968779866777899677797959996774647753365556465426225312415
443122331352464374355445874586585857875568898788666678776787986756859447544446666763365552244452322
434332122456323647344556476668759666569568769987679766689798887678765548587477474536465533643451422
012515214225433447464677777487776597697976998888987789798887796967785955887765466437333665342324523
151212435544622335634548655777875966867886867777679699887698988956977857868655457465635544364542154
425332155643342656676678888575989888788767888899689899798688796666757955465858667777362363434424542
531532414223243445466348848575895777968779899997887798776667989655588765447757447347342252524115215
155245334655555376575664568856876978978787888977789987998877778796686666588544843473375333466131323
442223263655353573545578848875879796997899897789887987979686977788898887848487563664644353624212355
532342436423523433743374566649565796978689677778887988977986779775985585775684847364464225436344143
144131162643455446377457566657777998676869667898877798979888978868889578745676553337663625625354412
212531125562654464546354657756555595879978788779777999979988869895856856488587466676673534564554215
515521136425625643733788668547967896969879989899779977888986996776697776777887634766746536654352314
533115456346557773554457648785576555967877979777897997977979879979889786544548474557775226255314122
432554142445363766476368787846679896977899887779797978979969889696765986666844737665565256236231155
125253442625345476646468778468579567787976888987879797899978977695756855477657433457362446432521533
451335344244435635354545558745856999567779868887799878899867977959975677866558447464563253543641554
412221312464362643575387865576575778679997969899879799897898997777656968646646747476434353234412443
132123436534443354553586484485777568678779897878779989966877968659558598445764456573762542646254544
024143156436353736354736885476858987768978898698789976987668878886568974574565737457544455543452243
413135313643263775675578866878568599957868689889887898688888897699957964455656556464353643525222353
151155234625524455454576866886566769799886776697778986968676996687578554546778475347663665445441542
225335432336665664467554464786765996688599897886876679899878795577796866847588643664352344525425543
231314414363454365656445447648785668665699968677877867679686575989979554888874435546345632362455211
215225154624264466667536475884866766856799777898688869799678789658958655666655664763723652345235254
035354151466424636347656474544888889966575676978876877797665689589954588444647355553546242434541533
001253252266442433343367457848868577696576876956987767987878766766685545864636355333223636351531525
432114153244234344474473336455665467595685659677769775776598955788485844764764774633253355345153344
440231335123622442356777766866685464699576999777998779679677676798556674878735743562224245652132511
321533313113424533355576556587855784598797699998756887876998858868457756543476743664332364153212141
003415211213442534357575744467674686668987887956977567898997789587764784753377657636326255155213240
224445525333436434526743474634674544887785577959758998579798555487687877644644777665225554533252422
114025431113445462556535777346657775747764689775588768859877487468477864653336547436625221413353402
444431445545324552322733365535555774656466544687955795855665775645668663457547655566226341423452434
410445115125126462226236353755455888548674587788447578688475766447586446763443644432535242233435244
124340412535436432636663557555763744747854587474866557668648548584743443577534342355356535325312231
200313132541126432532542747437536486654767558545587875558487658465577773437664544545354413212211212
101333454545223643623655465756576356785656755457885765456674785763465474656725224222541315314114031
131233142323341366245232435554365765545574774767785475776848668453355554474332533326544433554514201
321411402434455156645353526473756763444567846554465464446465455445745336435566265254512531525210112
030131034355454454644464565464475577344753366858566476585577545346763733342656446563454444451042420
123233024345514531322525463344645673755773347575544437475656346736346777342352464432215452454013411
014322323421235432556233655455544546633674634474456664346664375533773475562242423541545331424443423
103044012005453352356343445634325645434337637364655565435774676736733553262634342435541343244030000
320224321434222311444445623462256346445463567453434633465443337646352226626622452524224345304403330
212420324020443535541356556336654222753455556557453353546474577342535554563532343321131244211234443
122201202000423112251512562424625443344753367744337643545467743645523543344535431154212330403302410
302200224224202341324112146243633653523426575646533673677763345633345645554351214352331400423031333
123001320330302415435532414224224226352235334353577574436324344226425624263133551342153034141032032
213131040401103044255424155325562642646633364323565455323646566426224245451451423431324433130343033
231211142003310021532152553311226325626362465244625533422332633634454451545253314415220400420413203
312320130422410131322514243524545532465435666446335525623552365342455442353243151122403024303312021
133332233112042420435241253424143243654366264342464546256332442465235435334432132240241304411322221
333302213333240222002452421142454145262355663455246362444253326353144344554554324413300122011222021
202310030210410440001115535553444131333333663342464256255352255253344353414423520402213424030101331
020120023231001430421410113431542415212332151336254445313532121145524443121220343020012020230032230
210023323121322121303302233233245321224455333332343322323215223451525532222223304043001030230132022
002222021003231011234340303045243412535411232132142332533521354111141433134004443142111110122121100
022122131333022231003311233440122445251415343142141523345254553214425533033042143220401300010213100
122110132221332212341431023220242412121544355542132225542445544553234342030402442141111321103231211
122021200131211021314010312001241211235253115315252213451111223452204220130001212341202130012001221

138
day8/puzzle1.zig Normal file
View File

@ -0,0 +1,138 @@
const std = @import("std");
const input = @embedFile("input");
const stdout = std.io.getStdOut().writer();
const Allocator = std.mem.Allocator;
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const grid = textToGrid(input, allocator);
// for (grid) |row| {
// stdout.print("{s}\n", .{row}) catch unreachable;
// }
const visible = visibleElemsInGrid(grid);
stdout.print("{}\n", .{visible}) catch unreachable;
}
fn textToGrid(text: []const u8, allocator: Allocator) [][]const u8 {
var iterator = std.mem.split(u8, text, "\n");
const n_col = iterator.first().len;
const n_row = iterator.buffer.len / (n_col + 1);
// Need to do this to start at the beggining
iterator.reset();
// Creating and allocating the grid
var grid: [][]const u8 = allocator.alloc([]const u8, n_row) catch unreachable;
for (grid) |*row| {
row.* = allocator.alloc(u8, n_col) catch unreachable;
}
//stdout.print("Width: {}\n", .{n_col}) catch unreachable;
//stdout.print("Height: {}\n", .{n_row}) catch unreachable;
// Filling the grid
var i: usize = 0;
while (iterator.next()) |row| : (i += 1) {
if (row.len == 0) continue;
grid[i] = row;
}
return grid;
}
fn visibleElemsInGrid(grid: [][]const u8) u32 {
var visible: u32 = 0;
const n_row = grid.len;
const n_col = grid[0].len;
var row: usize = 0;
var col: usize = 0;
// For each element of the grid
while (row < n_row) : (row += 1) {
elem: while (col < n_col) : (col += 1) {
// Edges are always visible
if (
col == 0 or
col == n_col - 1 or
row == 0 or
row == n_row - 1
) {
visible += 1;
continue :elem;
}
var row_aux: usize = row;
var col_aux: usize = col;
// Upper
{
while (col_aux > 0) : (col_aux -= 1) {
if (grid[row_aux][col_aux - 1] >= grid[row][col]) {
break;
}
} else {
visible += 1;
continue: elem;
}
}
// Lower
{
while (col_aux < n_col - 1) : (col_aux += 1) {
if (grid[row_aux][col_aux + 1] >= grid[row][col]) {
break;
}
} else {
visible += 1;
continue: elem;
}
}
// Left
{
while (row_aux > 0) : (row_aux -= 1) {
if (grid[row_aux - 1][col_aux] >= grid[row][col]) {
break;
}
if (row_aux == 0) break;
} else {
visible += 1;
continue: elem;
}
}
// Right
{
while (row_aux < n_row - 1) : (row_aux += 1) {
if (grid[row_aux + 1][col_aux] >= grid[row][col]) {
break;
}
} else {
visible += 1;
continue: elem;
}
}
}
col = 0;
}
return visible;
}