6
\$\begingroup\$

Consider a 10 by 10 grid of points at integer coordinates. Given a coordinate (x, y) of one of the points in the grid and a radius r, some of the points in the grid will fall inside a circle of radius r that is centered at (x, y). You will count the number of such points.

Input

An integer coordinate (x, y) in a 10 by 10 grid.

Output

All the different counts for the number of points in the grid that are inside a circle centered at (x, y), for every possible radius r. Your output should not contain duplicate values but can be in any order.

You may not assume that r is an integer so there are an infinite number of possible radii but your output will nonetheless be of finite size. For example, the same count arises for all 0<r<1.

The numbers 1 and 100 will always be in your output no matter what (x, y) are.

If (x, y) is a corner, the output should be:

[1, 3, 4, 6, 8, 9, 11, 13, 15, 17, 19, 20, 22, 26, 28, 30, 31, 33, 35, 37, 39, 41, 43, 45, 48, 50, 52, 54, 56, 58, 62, 64, 65, 67, 69, 71, 73, 75, 79, 81, 83, 85, 86, 88, 90, 92, 94, 95, 97, 99, 100]

If (x, y) is on a side and not too close to a corner then the output should include 1, 4, 6, 9, 13, 15, 18, 22, 26.

If (x, y) is somewhere in the middle then your output should include 1, 5, 9, 13, 21, 25, 29. For example if it is (4, 3) the output should be [1, 5, 9, 13, 21, 25, 29, 37, 45, 48, 54, 58, 64, 72, 76, 80, 82, 86, 87, 89, 91, 94, 96, 97, 99, 100].

\$\endgroup\$
3
  • 7
    \$\begingroup\$ can you add at least 5 test cases please? \$\endgroup\$
    – mousetail
    Commented Dec 29, 2023 at 20:06
  • 1
    \$\begingroup\$ Can we take input as a complex number? \$\endgroup\$
    – Tbw
    Commented Dec 29, 2023 at 22:06
  • \$\begingroup\$ @Tbw Yes you can. \$\endgroup\$
    – Simd
    Commented Dec 30, 2023 at 4:40

13 Answers 13

5
\$\begingroup\$

Vyxal 3, 12 bytes

₀ʀ2ẋᵗϩİ?ȧS†@

10x10 grid located in coordinates 0-9. Takes input as a complex number.

Try it Online!

Explanation

₀ʀ2ẋᵗϩİ?ȧS†@­⁡​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌­
  2ẋ          # ‎⁡2nd cartesian power of
₀ʀ            # ‎⁢‎⁢Range 0-9
    ᵗϩ        # ‎⁣Map through all coordinates and dump to stack
      İ?ȧ     # ‎⁤Absolute difference between input and coordinate
         S†   # ‎⁢⁡‎⁢⁡Sort list and return length of consecutive groups
           @  # ‎⁢⁢Cumulatively add all lengths
💎

Original answer, 13 bytes

₀ʀ2ẋƛ?-²∑}S†@­⁡​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠⁠‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏⁠⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁤⁢‏‏​⁡⁠⁡‌­
  2ẋ            # ‎⁡2nd cartesian power of
₀ʀ              # ‎⁢Range 0-9
    ƛ    }      # ‎⁣Map through all coordinates
     ?-²∑       # Squared euclidean distance‎⁤ between input and coordinate
          S†    # ‎⁢⁡Sort list and return length of consecutive groups
            @   # ‎⁢⁢Cumulatively add all lengths
💎

Created with the help of Luminespire.

\$\endgroup\$
3
  • \$\begingroup\$ Very nice method \$\endgroup\$
    – Simd
    Commented Dec 29, 2023 at 21:19
  • \$\begingroup\$ Maybe write "Explanation created..." instead of "Created..."? \$\endgroup\$ Commented Dec 29, 2023 at 21:36
  • 1
    \$\begingroup\$ That's just the default Luminespire text. \$\endgroup\$
    – Tbw
    Commented Dec 29, 2023 at 22:36
3
\$\begingroup\$

Wolfram Language (Mathematica), 101 bytes

Union@Table[p=#;Length@Select[Join@@Array[{#,#2}&,{10,10},0],p~EuclideanDistance~#<r&],{r,1,20,.01}]&

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Jelly, 10 bytes

⁵p`Æịạ⁸ĠẈÄ

Try it online!

A monadic link taking a co-ordinate on a grid from 1..10 on both axes and expressed as a complex number. A list of integers is returned.

Explanation

⁵p`        | Cartesian product of 1..10 with itself
   Æị      | Convert to complex
     ạ⁸    | Absolute difference with link argument
       Ġ   | Indices to group identical values (ordered by the values)
        Ẉ  | Group lengths
         Ä | Cumulative sum
\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 57 bytes

i+=#2&@@@Tally@Sort@{Array[# #+#2#2&,10+{i=0,0},-#,##&]}&

Try it online!

                     Array[# #+#2#2&,10+{  0,0},-#,   ]     distance to each point
               Sort@{                              ##& }    ascending by distance
   #2&@@@Tally@                                             counts of each
i+=  &@@@                                i=                 cumulative
\$\endgroup\$
2
\$\begingroup\$

Desmos, 75 bytes

f(p)=[L[L<=i].countfori=L].unique
I=[0...9]
L=[distance(p,(a,b))fora=I,b=I]

Input as a point (x,y), ranging between coordinates 0 to 9.

Try It On Desmos!

Try It On Desmos! - Prettified

\$\endgroup\$
2
\$\begingroup\$

R, 47 bytes

\(x)cumsum(table(abs(x-outer(0:9,0:9*1i,`+`))))

Attempt This Online!

A function taking a complex argument and returning a list of integers. Uses a zero-indexed grid from 0..9 on both axes.

\$\endgroup\$
2
\$\begingroup\$

TI-BASIC, 73 bytes

abs(Ans-seq(10fPart(.1I)𝒾+int(.1I),I,0,99→L
SortA(ʟL
ΔList(augment(ʟL,{0
cumSum(1 or Ans)not(not(Ans→L
SortD(ʟL
seq(ʟL(I),I,1,sum(1 and ʟL

Takes input in Ans as a complex number with the x and y from 0 to 9.

\$\endgroup\$
2
\$\begingroup\$

APL(Dyalog Unicode), 3227 bytes SBCS

{⍸⌽≠{⍵[⍒⍵]},+.×⍨⍤-∘⍵¨⍳2⍴10}

Try it on APLgolf!

-5 bytes thanks to att.

A dfn which takes a vector on the right and outputs the list of possible counts.

Old explanation

{+\{≢⍵}⌸{⍵[⍋⍵]},∘.(+.×⍨⍵-,)⍨⍳10}­⁡​‎‎⁡⁠⁢⁤⁡‏⁠‎⁡⁠⁢⁤⁢‏⁠‎⁡⁠⁢⁤⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁡⁡‏⁠‎⁡⁠⁢⁡⁢‏⁠‎⁡⁠⁢⁡⁣‏⁠‎⁡⁠⁢⁣⁣‏⁠‎⁡⁠⁢⁣⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁣⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁢⁤‏⁠‎⁡⁠⁢⁣⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏⁠‎⁡⁠⁢⁢⁢‏⁠‎⁡⁠⁢⁢⁣‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁤⁤‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁣⁡​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌­
                            ⍳10   # ‎⁡range 1-10
                ∘.(       )⍨      # ‎⁢For every pair of elements
                         ,        # ‎⁣pair into vector
                       ⍵-         # ‎⁤subtract from input vector
                   +.×⍨           # ‎⁢⁡dot product with itself
               ,                  # ‎⁢⁢matrix to list
        {⍵[⍋⍵]}                   # ‎⁢⁣sort
   {≢⍵}⌸                          # ‎⁢⁤frequencies of groups
 +\                               # ‎⁣⁡cumulative sums
💎

Created with the help of Luminespire.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 27 bytes \$\endgroup\$
    – att
    Commented Jan 3 at 10:48
2
\$\begingroup\$

Scala, 156 146 bytes

Use Double scala.math.hypot(Double length, Double base) to calculate the square root of the sum of squares of the 2 numbers.

Saved 10 bytes thanks to @ceilingcat


Golfed version. Try it online!

(x,y)=>{val p=for{i<-0 to 99}yield(i/10,i%10);p.foldLeft(Set(1,100)){(a,t)=>a+p.count{case(a,b)=>math.hypot(a-x,b-y)<=math.hypot(t._1-x,t._2-y)}}}

Ungolfed version. Try it online!

object Main {
  val gridSize = 10

  def main(args: Array[String]): Unit = {
    // Example usage:
    val countsForCorner = countPointsInCircleForAllRadii(0, 0)
    val countsForSide = countPointsInCircleForAllRadii(0, 5)
    val countsForMiddle = countPointsInCircleForAllRadii(5, 5)

    println(countsForCorner.toList.sorted.mkString("[", ", ", "]"))
    println(countsForSide.toList.sorted.mkString("[", ", ", "]"))
    println(countsForMiddle.toList.sorted.mkString("[", ", ", "]"))
  }

  def countPointsInCircleForAllRadii(x: Int, y: Int): Set[Int] = {
    val points = for {
      i <- 0 until gridSize
      j <- 0 until gridSize
    } yield (i, j)

    val counts = points.foldLeft(Set[Int]()) { (acc, point) =>
      val (px, py) = point
      val maxRadius = math.sqrt(math.pow(px - x, 2) + math.pow(py - y, 2))
      val count = points.count { case (cx, cy) =>
        math.sqrt(math.pow(cx - x, 2) + math.pow(cy - y, 2)) <= maxRadius
      }
      acc + count
    }

    // Ensure 1 and 100 are always included as per the specifications
    counts + 1 + 100
  }
}
\$\endgroup\$
0
1
\$\begingroup\$

Charcoal, 37 bytes

NθNηFχFχ⊞υ⁺X⁻θι²X⁻ηκ²Wυ«⟦ILυ⟧≔⁻υ⟦⌈υ⟧υ

Try it online! Link is to verbose version of code. 0-indexed. Outputs the counts in descending order. Explanation:

NθNη

Input the centre point.

FχFχ

Loop over all of the points in the grid.

⊞υ⁺X⁻θι²X⁻ηκ²

Calculate the squared distance from the centre to that point.

Wυ«

Repeat until no more squared distances remain.

⟦ILυ⟧

Output the current count of squared distances.

≔⁻υ⟦⌈υ⟧υ

Remove the highest squared distance(s).

Output in ascending order costs 2 bytes, but 2 bytes can be saved by using the newer version of Charcoal on ATO and taking input as a Gaussian integer:

Nθ≔ΣEχEχ↔⁻θ⁺ι×λI1jηW⁻ηυ⊞υ⌊ιIEυLΦ笛λι

Attempt This Online! Link is to verbose version of code. 0-indexed. Explanation:

Nθ

Input the centre point.

≔ΣEχEχ↔⁻θ⁺ι×λI1jη

Calculate the distances to all of the points in the grid.

W⁻ηυ⊞υ⌊ι

Sort and deduplicate the distances.

IEυLΦ笛λι

For each unique distance, output the count of points not greater than that distance.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 89 bytes

x=>y=>[...w=Array(i=100)].map(t=>w[j=(--i%10)**2+(i/10|0)**2]=-~w[j])&&w.flatMap(t=>i+=t)

Try it online!

JavaScript (Node.js), 104 bytes

x=>y=>[...[...Array(i=100)].map(e=t=>(--i%10)**2+(i/10|0)**2+1e7).sort(),0].flatMap((t,i)=>e-(e=t)?i:[])

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 5 -la, 69 66 bytes

map/.$/&++$a[("@F"-$`)**2+($F[1]-$&)**2],0..99;$_&&say$t+=$_ for@a

Try it online!

Grid is labeled 0-9 on both axes. Input of X and Y is on one line, space separated. Output is line separated.

\$\endgroup\$
1
\$\begingroup\$

Rust, 322 259 254 125 bytes

A port of my Scala answer in Rust.

Saved 68 bytes thanks to @ceilingcat

Saved 129 bytes thanks to @att


Golfed version. Attempt This Online!

|x,y|->HashSet<_>{let p=||(0..=99).map(|i|(|a,b|a*a+b*b)(i/10-x,i%10-y));p().map(|a|p().filter(|&b|b<=a).count()).collect()};

Ungolfed version. Attempt This Online!

use std::collections::HashSet;

const GRID_SIZE: i32 = 10;

fn main() {
    // Example usage:
    let counts_for_corner = count_points_in_circle_for_all_radii(0, 0);
    let mut sorted_counts_for_corner: Vec<_> = counts_for_corner.into_iter().collect();
    sorted_counts_for_corner.sort_unstable();
    println!("{:?}", sorted_counts_for_corner);

    let counts_for_side = count_points_in_circle_for_all_radii(0, 5);
    let mut sorted_counts_for_side: Vec<_> = counts_for_side.into_iter().collect();
    sorted_counts_for_side.sort_unstable();
    println!("{:?}", sorted_counts_for_side);

    let counts_for_middle = count_points_in_circle_for_all_radii(5, 5);
    let mut sorted_counts_for_middle: Vec<_> = counts_for_middle.into_iter().collect();
    sorted_counts_for_middle.sort_unstable();
    println!("{:?}", sorted_counts_for_middle);
}

fn count_points_in_circle_for_all_radii(x: i32, y: i32) -> HashSet<i32> {
    let points = (0..GRID_SIZE)
        .flat_map(|i| (0..GRID_SIZE).map(move |j| (i, j)))
        .collect::<Vec<_>>();

    let mut counts = HashSet::new();

    for point in &points {
        let (px, py) = *point;
        let max_radius = (((px - x).pow(2) + (py - y).pow(2)) as f64).sqrt();
        let count = points.iter().filter(|&&(cx, cy)| {
            (((cx - x).pow(2) + (cy - y).pow(2)) as f64).sqrt() <= max_radius
        }).count() as i32;

        counts.insert(count);
    }

    // Ensure 1 and 100 are always included as per the specifications
    counts.insert(1);
    counts.insert(100);

    counts
}
\$\endgroup\$
2
  • \$\begingroup\$ you want 0..10 or 0..=9 for inclusive range \$\endgroup\$
    – att
    Commented Jan 12 at 20:36
  • \$\begingroup\$ 125 \$\endgroup\$
    – att
    Commented Jan 12 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.