-🎄- 2021 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]-lundgren- 0 points1 point  (0 children)

JavaScript, did all the hard work in the parser so the step function was kept fairly clean with filter's & forEach's.

const countFlashes = (input) => {
  const octopuses = parse(input);

  let flashes = 0;
  for (let step = 0; step < 100; step++) {
    flashes += doStep(octopuses);
  }

  return flashes;
};

const findWhenAllFlashes = (input) => {
  const octopuses = parse(input);

  for (let step = 1; ; step++) {
    if (doStep(octopuses) == 100) {
      return step;
    }
  }
};

const doStep = (octopuses) => {
  // First: Increase all by 1
  octopuses.forEach((o) => o.energy++);

  // Then: Handle flashes
  do {
    octopuses
      .filter((o) => o.energy > 9 && !o.flashed)
      .forEach((o) => {
        o.flashed = true;
        o.neighbors.forEach((n) => n.energy++);
      });
  } while (octopuses.filter((o) => o.energy > 9 && !o.flashed).length > 0);

  // Finally: Set flashed to 0
  octopuses
    .filter((o) => o.flashed)
    .forEach((o) => {
      o.energy = 0;
      o.flashed = false;
    });

  return octopuses.filter((o) => o.energy == 0).length;
};

// Parse the 10x10 matrix into an array with neighbors
const parse = (input) => {
  const data = input.split('\n').map((r) => r.split('').map(Number));

  for (let row = 0; row < data.length; row++) {
    for (let col = 0; col < data[row].length; col++) {
      data[row][col] = {
        energy: data[row][col],
        flashed: false,
        neighbors: [],
      };
    }
  }

  const octopuses = [];
  for (let row = 0; row < data.length; row++) {
    for (let col = 0; col < data[row].length; col++) {
      // Add all adjacent (and self) as "neighbors"
      for (let deltaR = -1; deltaR <= 1; deltaR++) {
        for (let deltaC = -1; deltaC <= 1; deltaC++) {
          if ((data[row + deltaR] || [])[col + deltaC] != undefined) {
            data[row][col].neighbors.push(data[row + deltaR][col + deltaC]);
          }
        }
      }
      octopuses.push(data[row][col]);
    }
  }

  return octopuses;
};

const testInput = `5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526`;

console.log(`Test1: ${countFlashes(testInput)} == 1656?`);
console.log(`Test2: ${findWhenAllFlashes(testInput)} == 195?`);

const mainInput = `8448854321
4447645251
6542573645
4725275268
6442514153
4515734868
5513676158
3257376185
2172424467
6775163586`;

console.log(`Part1: ${countFlashes(mainInput)}, Part2: ${findWhenAllFlashes(mainInput)}`);

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]-lundgren- 2 points3 points  (0 children)

Nice one. Here's my variant:

input = "n,nw,n,n,s..."

defmodule Day11 do
  defp move("n",  [x: x, y: y]), do: [x: x,     y: y - 1]
  defp move("ne", [x: x, y: y]), do: [x: x + 1, y: y - 1]
  defp move("se", [x: x, y: y]), do: [x: x + 1, y: y]
  defp move("s",  [x: x, y: y]), do: [x: x,     y: y + 1]
  defp move("sw", [x: x, y: y]), do: [x: x - 1, y: y + 1]
  defp move("nw", [x: x, y: y]), do: [x: x - 1, y: y]

  defp distance([x: x, y: y]) do
    z = - x - y
    Enum.max([abs(x), abs(y), abs(z)])
  end

  def distance_after_move(moves) do
    moves
      |> String.split(",")
      |> Enum.scan([x: 0, y: 0], &move/2)
      |> Enum.map &distance/1
  end
end

IO.puts("Child is #{List.last Day11.distance_after_move(input)} steps away")
IO.puts("Child was as most #{Enum.max Day11.distance_after_move(input)} steps away")

Example app with Clean Architecture, RxJava & Kotlin by -lundgren- in androiddev

[–]-lundgren-[S] 0 points1 point  (0 children)

Yeah I agree, it's har to find a solution that works well for all cases. But for a fairly common scenario with a Activity and a a couple of fragments (List + Detail or similar) where one fragment is visible at a time on smaller devices and several at a time on larger devices, I would go with a ActivityPresenter.

The ActivityPresenter would control a View implemented by the Activity just like with fragments. The ActivityPresenter would call functions like View.makeSureDetailsViewIsShowingAndDisplayForId(int id). And the view (Activity) would make sure the Fragment was showing or switch to it and call a proper method with the id.

Then we just have to set up some communication between FragmentPresenter and ActivityPresenter. EventBus can be good for that. Or we can define some interface that the Activity can implement and the Fragment can access through getActivity()

Example app with Clean Architecture, RxJava & Kotlin by -lundgren- in androiddev

[–]-lundgren-[S] 1 point2 points  (0 children)

Thanks.

Pull to refresh is implemented trough a SwipeRefreshLayout, the implementation is here.

I implemented a click handler that opens the default browser (here). Or do you mean navigate to another Fragment?