From: Brendan Hansen Date: Thu, 10 Dec 2020 14:41:55 +0000 (-0600) Subject: added day 10 X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=cb232e37cbca4e27fc3ced90a1d24e9913b7de5e;p=onyx-aoc-2020.git added day 10 --- diff --git a/day10.onyx b/day10.onyx new file mode 100644 index 0000000..a2181d0 --- /dev/null +++ b/day10.onyx @@ -0,0 +1,54 @@ +#include_file "core/std/wasi" + +use package core +use package core.str.reader as reader + +count_ending_paths :: proc (nums: [..] u32) -> u64 { + tally: [..] u64; + array.init(^tally, nums.count); + defer array.free(^tally); + + for ^t: tally do *t = 0; + tally[nums.count - 1] = 1; + + while i := cast(i32) (nums.count - 2); i >= 0 { + for diff: 1 .. 4 { + if i + diff >= nums.count do continue; + + if nums[i + diff] - nums[i] <= 3 do tally[i] += tally[i + diff]; + } + + i -= 1; + } + + return tally[0]; +} + +main :: proc (args: [] cstring) { + contents := file.get_contents("input/day10.txt"); + defer cfree(contents.data); + + file := reader.make(contents); + + nums: [..] u32; + array.init(^nums); + defer array.free(^nums); + + while !reader.empty(^file) { + array.push(^nums, reader.read_u32(^file)); + reader.advance_line(^file); + } + + // Slight hack, but having 0 in the array makes both parts easier + array.push(^nums, 0); + + array.sort(^nums, cmp_asc); + + diffs: [3] u32; + for ^d: diffs do *d = 0; + for i: 1 .. nums.count do diffs[nums[i] - nums[i - 1] - 1] += 1; + diffs[2] += 1; + + printf("Diff prod: %i\n", diffs[0] * diffs[2]); + printf("Arrangements: %l\n", count_ending_paths(nums)); +} diff --git a/input/day10.txt b/input/day10.txt new file mode 100644 index 0000000..ccf88b7 --- /dev/null +++ b/input/day10.txt @@ -0,0 +1,92 @@ +17 +110 +146 +144 +70 +57 +124 +121 +134 +12 +135 +120 +19 +92 +6 +103 +46 +56 +93 +65 +14 +31 +63 +41 +131 +60 +73 +83 +71 +37 +85 +79 +13 +7 +109 +24 +94 +2 +30 +3 +27 +77 +91 +106 +123 +128 +35 +26 +112 +55 +97 +21 +100 +88 +113 +117 +25 +82 +129 +66 +11 +116 +64 +78 +38 +99 +130 +84 +98 +72 +50 +36 +54 +8 +34 +20 +127 +1 +137 +143 +76 +69 +111 +136 +53 +43 +140 +145 +49 +122 +18 +42 diff --git a/out.wasm b/out.wasm index b4c5fb6..ac69ff6 100644 Binary files a/out.wasm and b/out.wasm differ