aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConor Hoekstra <codereport@outlook.com>2024-09-24 11:20:17 -0400
committerConor Hoekstra <codereport@outlook.com>2024-09-24 11:20:17 -0400
commit717c612bec44c64cb53373152aa0b42e95cabd0d (patch)
tree7687e2fe2e71eb9b54c347da9746be7fc6767aa9
parent30666aff7cf5d9c063bcb71246c1470f1119404c (diff)
downloadperlweeklychallenge-club-717c612bec44c64cb53373152aa0b42e95cabd0d.tar.gz
perlweeklychallenge-club-717c612bec44c64cb53373152aa0b42e95cabd0d.tar.bz2
perlweeklychallenge-club-717c612bec44c64cb53373152aa0b42e95cabd0d.zip
:sparkles: Week 288 in BQN + 1 Python
-rw-r--r--challenge-288/conor-hoekstra/ch-1.bqn25
-rw-r--r--challenge-288/conor-hoekstra/ch-1.py30
-rw-r--r--challenge-288/conor-hoekstra/ch-2.bqn33
3 files changed, 88 insertions, 0 deletions
diff --git a/challenge-288/conor-hoekstra/ch-1.bqn b/challenge-288/conor-hoekstra/ch-1.bqn
new file mode 100644
index 0000000000..893baf3412
--- /dev/null
+++ b/challenge-288/conor-hoekstra/ch-1.bqn
@@ -0,0 +1,25 @@
+# For up to date code:
+# https://github.com/codereport/bqn-code/blob/main/pwc/288-1.bqn
+
+# Slow One-Line Solution
+ClosestPalindrome ← {⊑(⌽⊸≡•Fmt)¨⊸/∾<˘⍉>((•BQN𝕩)(-⋈+)(1+↕))10⋆≠𝕩}
+
+# Faster Solution
+Find ← { 𝕩 < 0 ? ¯∞ ; ⌽⊸≡•Fmt 𝕩 ? 𝕩 ; 𝕨 𝕊 𝕨 + 𝕩 }
+ClosestPalindrome2 ← { ⊑(⌊´⊸=·|t-⊢)⊸/¯1 ⋈○(Find⟜(+⟜(t←•BQN𝕩))) 1 }
+
+# Tests
+•Show ClosestPalindrome "123" # "121"
+•Show ClosestPalindrome "2" #"1"
+•Show ClosestPalindrome "1400" # "1441"
+•Show ClosestPalindrome "1001" # "999"
+•Show ClosestPalindrome "999" # "10001"
+•Show ClosestPalindrome "0" # "1"
+
+•Show ClosestPalindrome2 "123" # "121"
+•Show ClosestPalindrome2 "2" #"1"
+•Show ClosestPalindrome2 "1400" # "1441"
+•Show ClosestPalindrome2 "1001" # "999"
+•Show ClosestPalindrome2 "999" # "10001"
+•Show ClosestPalindrome2 "0" # "1"
+•Show ClosestPalindrome2 "1231234" # "12311321"
diff --git a/challenge-288/conor-hoekstra/ch-1.py b/challenge-288/conor-hoekstra/ch-1.py
new file mode 100644
index 0000000000..98587b5bad
--- /dev/null
+++ b/challenge-288/conor-hoekstra/ch-1.py
@@ -0,0 +1,30 @@
+# Link: https://theweeklychallenge.org/blog/perl-weekly-challenge-288
+
+def is_palindrome(n):
+ return str(n) == str(n)[::-1]
+
+def find(start: int, delta: int) -> int:
+ while not is_palindrome(start):
+ start += delta
+ print(start)
+ if start < 0:
+ return None
+ return start
+
+def closest_palindrome(n: str) -> str:
+ i = int(n)
+ a = find(i - 1, -1)
+ b = find(i + 1, 1)
+ print(a, b)
+ if a is not None and i - a <= b - i:
+ return str(a)
+ return str(b)
+
+# Tests
+print(closest_palindrome("123")) # "121"
+print(closest_palindrome("2")) # "1"
+print(closest_palindrome("1400")) # "1441"
+print(closest_palindrome("1001")) # "999"
+print(closest_palindrome("999")) # "10001"
+print(closest_palindrome("12312345")) # "12311321"
+print(closest_palindrome("0")) # "1"
diff --git a/challenge-288/conor-hoekstra/ch-2.bqn b/challenge-288/conor-hoekstra/ch-2.bqn
new file mode 100644
index 0000000000..0a450ff6a6
--- /dev/null
+++ b/challenge-288/conor-hoekstra/ch-2.bqn
@@ -0,0 +1,33 @@
+# For up to date code:
+# https://github.com/codereport/bqn-code/blob/main/pwc/288-2.bqn
+
+deltas ← ⌽¨⊸∾0⋈¨1‿¯1
+
+MaxContiguousBlocks ← { 𝕊orig:
+ empty ← 0⥊˜≢orig
+ IsValid ← ∧´≥⟜0‿0∾(≢orig)>⊢
+ FloodFill ← { 𝕊sym‿todo‿mat:
+ { 0=≠todo ? +´⥊mat ;
+ pos ← ⊑todo
+ new ← IsValid¨⊸/pos⊸+¨deltas
+ FloodFill { (sym=pos⊑orig)∧¬pos⊑mat
+ ? sym‿(new∾˜1↓todo)‿(1⊸+⌾(pos⊸⊑)mat)
+ ; sym‿(1↓todo)‿mat }}
+ }
+ ⌈´FloodFill¨{(𝕩⊑orig)‿⟨𝕩⟩‿empty}¨⥊↕≢orig
+}
+
+# Tests
+•Show MaxContiguousBlocks >⟨"xxxxo",
+ "xoooo",
+ "xoooo",
+ "xxxoo"⟩ # 11
+
+•Show MaxContiguousBlocks >⟨"xxxxx",
+ "xoooo",
+ "xxxxo",
+ "xoooo"⟩ # 11
+•Show MaxContiguousBlocks >⟨"xxxoo",
+ "oooxx",
+ "oxxoo",
+ "oooxx"⟩ # 7