From 464bbcb15c09813172c79820bcf526bb10cf4208 Mon Sep 17 00:00:00 2001 From: sterni Date: Thu, 9 Dec 2021 16:51:02 +0100 Subject: feat(sterni/aoc/2021): day 9 solution Change-Id: I90e7a47d5418abeff1ae7cc5757b6a8b3b0d1086 --- users/sterni/exercises/aoc/2021/solutions.bqn | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/users/sterni/exercises/aoc/2021/solutions.bqn b/users/sterni/exercises/aoc/2021/solutions.bqn index d965e6047fe6..7aac53b9eaa7 100755 --- a/users/sterni/exercises/aoc/2021/solutions.bqn +++ b/users/sterni/exercises/aoc/2021/solutions.bqn @@ -13,6 +13,8 @@ ReadInput ← {•file.Lines ∾ •path‿"/input/day"‿(•Fmt 𝕩)} SplitOn ← ((⊢ (-1˙)⍟⊣¨ +`)∘=⊔⊢) +_fix ← {𝕩 𝕊∘⊢⍟≢ 𝔽 𝕩} + # # 2021-12-01 # @@ -148,3 +150,39 @@ FuelConsumption2 ← +˝∘(TriNum¨)∘|∘(-⌜) ! 168 = FuelConsumption2 _lowestFuelPossible day6ExampleData •Out "Day 7.2: "∾•Fmt FuelConsumption2 _lowestFuelPossible day6Input + +# +# 2021-12-09 +# + +# part 1 + +ParseHeightMap ← ((≠≍(≠⊑))⥊∾)∘-⟜'0' + +day9ExampleData ← ParseHeightMap ⟨ + "2199943210", + "3987894921", + "9856789892", + "8767896789", + "9899965678" +⟩ +day9Input ← ParseHeightMap ReadInput 9 + +Rotate ← (⍉⌽)∘⊢⍟⊣ # counter clockwise +LowPoints ← {∧´𝕩⊸(⊣<((-⊢) Rotate ∞⊸»˘∘Rotate˜))¨ ↕4} + +RiskLevelSum ← (+´⥊)∘(1⊸+×LowPoints) + +! 15 = RiskLevelSum day9ExampleData + +•Out "Day 9.1: "∾•Fmt RiskLevelSum day9Input + +# part 2 + +NumberBasins ← ((1⊸+⊒⌾⥊)×⊢)∘LowPoints +Basins ← {𝕩⊸((<⟜9⊣)∧(«⌈»⌈«˘⌈»˘⌈⊢)∘⊢) _fix NumberBasins 𝕩} +LargestBasinsProduct ← {×´ 3↑ ∨ 1↓ ≠¨ ⊔⥊Basins 𝕩} + +! 1134 = LargestBasinsProduct day9ExampleData + +•Out "Day 9.2: "∾•Fmt LargestBasinsProduct day9Input -- cgit 1.4.1