added day 9 of aoc-2021
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 9 Dec 2021 19:31:49 +0000 (13:31 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 9 Dec 2021 19:31:49 +0000 (13:31 -0600)
core/container/iter.onyx
modules/opengles/build.sh
modules/opengles/onyx_opengles.so
tests/aoc-2021/day09 [new file with mode: 0644]
tests/aoc-2021/day09.onyx [new file with mode: 0644]
tests/aoc-2021/input/day09.txt [new file with mode: 0644]

index 63e0e73488a52a732e67c03bbc49a4aa476d2883..9b8066833d013575a810a4ce78662e010a0b7e0d 100644 (file)
@@ -365,21 +365,21 @@ fold :: #match {}
 @Cleanup // some way to shorten this would be nice
 #match fold macro (it: $T, init: $R, combine: $S) -> #auto where Iterable(T) {
     fold :: fold
-    return fold(it, init, combine);
+    return fold(as_iterator(it), init, combine);
 }
 
-#match fold (it: Iterator($T), initial_value: R, combine: (T, $R) -> R) -> R {
+#match fold (it: Iterator($T), initial_value: $R, combine: (T, R) -> R) -> R {
     for value: it {
         initial_value = combine(value, initial_value);
     }
-    
+
     return initial_value;
 }
 
 count :: #match {}
 #match count macro (it: $T, cond: $F) -> #auto where Iterable(T) {
     count :: count
-    return count(it, cond);
+    return count(as_iterator(it), cond);
 }
 
 #match count (it: Iterator($T), cond: (T) -> bool) -> i32 {
@@ -391,7 +391,7 @@ count :: #match {}
 some :: #match {}
 #match some macro (it: $T, cond: $F) -> #auto where Iterable(T) {
     some :: some
-    return some(it, cond);
+    return some(as_iterator(it), cond);
 }
 
 #match some (it: Iterator($T), cond: (T) -> bool) -> bool {
@@ -402,7 +402,7 @@ some :: #match {}
 every :: #match {}
 #match every macro (it: $T, cond: $F) -> #auto where Iterable(T) {
     every :: every
-    return every(it, cond);
+    return every(as_iterator(it), cond);
 }
 
 #match every (it: Iterator($T), cond: (T) -> bool) -> bool {
index 91be60c0d893cf37195bf5c8678d0b6c52edb8a0..7702b2562ffb40f655c22274855c929dbf9c6bfc 100755 (executable)
@@ -1,3 +1,3 @@
 #!/bin/sh
 
-gcc -shared -fPIC modules/opengles/onyx_opengles.c -I include -I lib/common/include -o ./modules/opengles/onyx_opengles.so -lGL
\ No newline at end of file
+gcc -shared -fPIC modules/opengles/onyx_opengles.c -I include -I lib/common/include -o ./modules/opengles/onyx_opengles.so
\ No newline at end of file
index b16665b89d1b890d8345235552fbd09128eea6be..532c064c3de5788b048b21f7328710746b5e5a69 100755 (executable)
Binary files a/modules/opengles/onyx_opengles.so and b/modules/opengles/onyx_opengles.so differ
diff --git a/tests/aoc-2021/day09 b/tests/aoc-2021/day09
new file mode 100644 (file)
index 0000000..4f19ca5
--- /dev/null
@@ -0,0 +1,2 @@
+Part 1: 575
+Part 2: 1019700
diff --git a/tests/aoc-2021/day09.onyx b/tests/aoc-2021/day09.onyx
new file mode 100644 (file)
index 0000000..4750f7c
--- /dev/null
@@ -0,0 +1,130 @@
+#load "core/std"
+
+use package core
+
+Pos :: struct {x,y: i32;}
+#match hash.to_u32 (use p: Pos) => x * 5039473 + 59362447 * y;
+#operator == (p1, p2: Pos) => p1.x == p2.x && p1.y == p2.y;
+
+Cell :: struct {
+    height: i32;
+    dx: i32 = 0;  // height - (right of cell).height
+    dy: i32 = 0;  // height - (below cell).height
+}
+
+find_span :: macro (low: Pos) -> u32 {
+    queued:    [..] Pos;
+    included:  [..] Pos;
+    potential: [..] Pos;
+
+    queued << low;
+
+    while queued.count != 0 {
+        t := queued[0];
+        array.delete(^queued, 0);
+
+        top := ^heightmap[t];
+        if top.height == 9 do continue;
+
+        included << t;
+
+        array.clear(^potential);
+        if t.x < width - 1 && top.dx < 0 {
+            potential << Pos.{t.x+1,t.y};
+        }
+        if t.y < height - 1 && top.dy < 0 {
+            potential << Pos.{t.x,t.y+1};
+        }
+        if t.x > 0 {
+            if heightmap[Pos.{t.x-1,t.y}].height > 0 {
+                potential << Pos.{t.x-1,t.y};
+            }
+        }
+        if t.y > 0 {
+            if heightmap[Pos.{t.x,t.y-1}].height > 0 {
+                potential << Pos.{t.x,t.y-1};
+            }
+        }
+
+        for p: potential {
+            if !array.contains(included, p) && !array.contains(queued, p) {
+                queued << p;
+            }
+        }
+    }
+
+    return included.count;
+}
+
+#match io.write (w: ^io.Writer, x: ^Map($K, $V)) {
+    io.write(w, "{\n");
+    for e: x.entries {
+        io.write(w, "    {} => {}\n", e.key, e.value);
+    }
+    io.write(w, "}");
+}
+
+main :: (args) => {
+    for file: os.with_file("./tests/aoc-2021/input/day09.txt") {
+        reader := io.reader_make(file);
+
+        heightmap: Map(Pos, Cell);
+
+        height, width := 0, 0;
+        while !io.reader_empty(^reader) {
+            line := io.read_line(^reader, consume_newline=false, inplace=true);
+            io.skip_whitespace(^reader);
+
+            for line.count {
+                heightmap[Pos.{it, height}] = Cell.{cast(u32) (line[it] - #char "0")};
+            }
+
+            width = line.count;
+            height += 1;
+        }
+
+        for y: height - 1 do for x: width {
+            map.update(^heightmap, .{x,y}) #{
+                it.dy = it.height - heightmap[Pos.{x,y+1}].height;
+            };
+        }
+        for x: width - 1 do for y: height {
+            map.update(^heightmap, .{x,y}) #{
+                it.dx = it.height - heightmap[Pos.{x+1,y}].height;
+            };
+        }
+
+        lowest: [..] Pos;
+        risk_sum := 0;
+        for y: height do for x: width {
+            h := ^heightmap[Pos.{x,y}];
+            if x < width  - 1 && h.dx >= 0 do continue;
+            if y < height - 1 && h.dy >= 0 do continue;
+
+            if x > 0 {
+                if heightmap[Pos.{x-1, y}].dx <= 0 do continue;
+            }
+
+            if y > 0 {
+                if heightmap[Pos.{x, y-1}].dy <= 0 do continue;
+            }
+
+            lowest << .{x, y};
+            risk_sum += h.height + 1;
+        }
+
+        printf("Part 1: {}\n", risk_sum);
+
+        lowest_count: Map(Pos, i32);
+        for low: lowest do lowest_count[low] = find_span(low);
+
+        array.quicksort(lowest_count.entries, (a, b) => b.value - a.value);
+
+        answer := iter.as_iterator(lowest_count.entries)
+               |> iter.map((x) => x.value)
+               |> iter.take(3)
+               |> iter.fold(1, (x, y) => x * y);
+
+        printf("Part 2: {}\n", answer);
+    }
+}
\ No newline at end of file
diff --git a/tests/aoc-2021/input/day09.txt b/tests/aoc-2021/input/day09.txt
new file mode 100644 (file)
index 0000000..40cc129
--- /dev/null
@@ -0,0 +1,100 @@
+8754399998765678934569897649898767987432123986543213567997681024589763459854327895454567899979876567
+9865989879854578923998789438789656795421013499854101278976542137678954598765456964323456789768995456
+1979879965913569899878679626598947987532194987653232389898756398889865679876587953212457899656789345
+0999767894301346798767568917467898998999989998754343499769877479996986789987678943103998988545999156
+9875658789212457899653498902345789989987978999875454678956998567895397898998789653239899876839878997
+2984545678923498987732497893459898679876867899986568789545459789954298967899898764349768995424456989
+1973123789434589876521256789767986598765458998797678899632349895432109456789909865997659789312349979
+9862012578955678965410345789979895439897346789698989998921234999543212367898919979876545689101298864
+7653223467896799876321235899998754320989477897569099987892545898767323498987898998765435679232987743
+8868434578987899965434345678959965439876568999432129876989656799875456999456997898765323578949896542
+9876545699198978976745676789349878545987679998764598765679767899876567894359876987653214789898785621
+5987676789019567898956897899956989787898989999975987654568978999987879945298765698775623456789654310
+3298798993123458999498969999899996999929497899986798923589989998798989932129854349986737687899794322
+2129899986548899992359356789798765678912345678997976534678999987689999893998743234598898788929876533
+3348998997656789989467969899659978799323567789989998645689999874578999789897654345689949899434987545
+5656987898767899878979899998743989976539678999978909897898979953489987656799765656792125989695997657
+6879876789888989867999789897654599987898989999868912998997567892198986545698976768921014568989898798
+8989865678999876548987678789765679099987899898756793569989998989987665434567897979432123459879789899
+9498654569643987656996545678978789129876989654345679699879899878998543212367998989543456998767689998
+4349865678954598987894323489989899298765678973216789989765679869898664323458999297677679877654568997
+3239876789985699898965412345996998989874567892101234978954398756789775434567893139798899965433699976
+4109987899876987679876103457895987879983459989235349867895987645678996755678921029929989954321788965
+3298998965998976569954323568999876767892598768976798658999876535869899876989432198939767895434567893
+4397899893219987698765454567897965656991698657997899545699965423456789987897543987898956789567789912
+6976976789909998789896566788945694547789987645789999434589876534787895898998659876787645678998992101
+9965865667897899890987989999896983234679876535678998545678987645678974799329969865486536789999543322
+8754323458976899921298991556789975135798765323456987678789999876899753689919899986321017899987678433
+9843212357894567893989632345899989245899986434567898989899876987976974567898798765432128989999989994
+9954301348943656789876546786999895346953497565778949799998764398975965689998659986554534678998998789
+9865412367892347998987697997899765456932987678899235678929865459989878789987643987866545799567897678
+1996537569901456987898989998939876767891299899910123799219876567897999891987654598977656892456789567
+0989987678912367896999878999212987978910123992391234942109998678986579910199799679198787891367993456
+9878999899953567895698767898923498989521345679989349899298769999975437921988978989019898942478902567
+8767899989879789934989656987994989995432656789778997678965459889876556899876567899999999543589323478
+9657789875999892129878549876789879876543467898656789498976797679987987898765456789879987674696534567
+9545678954569954298765432345689756997654578909947992347897987568899898949993234898968698985897647688
+8434567893498765459986321235799842398767678919899743456999876456789789234989148897657569697898798799
+7545798932349876578996410134999921269878789998789654567899765347898698949878956789543454598999899912
+8786899543467987889987821235689543459989899886599995698998654236987567896967899898432123999898965301
+9897999964878998999998642547898654567993998765457889789879764345897678985756789987645239899767993212
+6989989895989979479899753457929865788901989654345679998769865456789799864347999897656798778456789323
+4579876789998768366789876569019879899899876545234567899656976577892987643238998789978989563348995434
+3398765678929854255899989678923989976789985432125678932987987698921098432159876567899765431237896545
+2129654569919867134989999989434599965691096421034789321398999789993298541099965467909876210176989766
+1019993457899773235678998996545699894989297533145895435459799899989987693989876378919986321245678997
+2298789967899654547999997987699989792165987654256789945997689998878998989878987459998765432346799789
+3987679899998765659531986798988777689014598765468899899886599987567899878767898567899878643456789678
+9876545788999878967999994339876566579923459876979956789765498765468998764356789679932989987567994599
+8765434567894989879878789212965452467894567988989245999854329878312349875457898789641094597679999689
+9965325789912399989865678909854321378995678999297457898763219885101256988768949896532123498789878993
+9978412892109989998754567898765210135789789980196568999874598764312345999879235987643435679893467892
+8989523993298678998653278999879321245678999871987979698765679984323679893989123498754545678942378901
+7995436789396545698764345699875432356789498762398989549896893986654598782393234569767657899321999992
+6789545678989434579975456789986743456789219654569991239989921098765697654459945679898968998939889989
+5678967899678924569996567897399865767898999765678989998878932129976798765667897789959979987998775678
+4567898934569319698789978965246978978987878976789679876767893239987899986899939893244598976899654678
+2458979320178998987678989954358989989876767987898789765456789398698912399996547992123497865678943789
+1278965491389987896567898765467999998965656898959898654345678976569699498989956789019986564989654894
+2367896989499896985456789876569878987654548789349999875234567895434578997879899892198765473699879932
+3456789878998765674367898988998767996543237678998998692123458954325678986567798943999654312579989321
+4767898769987654561234567899987856789542123567897987543254569543212349895454687959876543201467898730
+5978997658998963210123679901976545878953234689995498954345678969101998765343456799987875412345679641
+6789899867899654721234569219875634767894645789789599898758789998929876543212345689998964323456789432
+9896799978939875674348678939994323456789656898678988789869899987999987672101234578998765537577997545
+9985989989012987786759789998963213697898767956569876679979989876989899865216795789569876646989798986
+9874578992124598999899899987954325989999998943498865567899879765679766994345678995459987658994679987
+8763469789235789921932999896899476978999989432976543456799854323456954986776899212398799869543456798
+7542345678945699892921298765778999869689976569877331357898763210567893098987942101987689978954569899
+9321234567896798759893987654567897654567897698765210237999895432378932199798994299876579989765678912
+5456347698997987645789199873456789543456998799854325446789976556489653987649789978765467899879889923
+6565456789999765434679297632347997654868949989965436556892989667689769999735699866212345999989999899
+7676567997898652123589986544459899765689939878976787667921998789799898989129789754323456789992345798
+8798678956789843254678987676598798976789898769987898989939879899899987678998998765458578998921959897
+9899989767898764567889998787987687897999797659899969396899965910979876567887999896567699987899899986
+1932199878969975678999989899895456798998678645779954245789874321256975453456789987779789876799799875
+0193998999456989789998776987678347989997576534667895345699765432346965322345678998989998765678678964
+1989897632357899899889645987563239867976434323456987456798976543499876410238789879997897654234567973
+2979789543468921998765439765432153458985321012667896567987997654987654321234894569876789543123457899
+9865698954599893987656023986549012369876432523678987679876889765698766432457943498765679764034598978
+9764567895987759876543234797898137478987675434567898798765679896789879745567892987654567932125899568
+8643786789876545987654345698996546789198786545678979898664589987899987656678979699843457897236789346
+6532345678985432399895456789987857994019897656799567997543468998998998767799764598764598976347895459
+7653567989876531569979967891098968943235998768923456985432377999987549878894643469975689765456789568
+9765678998983210998767898962239979654459999879436569876621456789896432989943012979896799876568998779
+9876789467974329899879999543478989795998987989999978986532567896795321299932199898789899987879999889
+7987994359865439789989397654567899989787895699888899986543478965989432349893989747678989998999899998
+6798987212976798698993298776678999878576996898777789987654567894678943498789767634599878999986788976
+7999876434987977567979999897899898756455989989665568998765678923499854598678954523989769798765667895
+8987987565699765454567896998956796544234769876533457899876789765789767898789763219979945698754656789
+9876598978798754323478965449349897932123456965412345678987892976799899989999854998767824598743445678
+4989499989899998454589986321298989891015569654323456889798901987899989878899969876543213987652134567
+2398989990999997567995975410987677789234698765434568997659213499967879965799989987632101298543245898
+1987679891298997678954986829876566689345689876987678989998994569656567894678998996545212349654456789
+9876566789987898789993497845985434578956890998999799679886989798743459965699987989694393498766567899
+8765465698765699897989598956987316789897921989878954599765678999995998977789896578976989999897878989
+9943344987654987945678999767999402356789549976767893987654569899889897989899765467899878789998989879
+8432123498743495434389899879873212345898798865656899998543654698767786999987674356898765699019698768
+7643234898767894321235789998764325476789897654345788999652123598654645898797543237899654578924569978
+9654655679879985410134567899865445567899986545234567898761024696543234789643210123978976689635678989
+8765678799989876621245789999876786798998765432123456789432123987654345698775431234569987796545689999
\ No newline at end of file