ken-kost

ken-kost

I thought I could edit my post :japanese_goblin:
Anyway, @bjorng

input
|> Input.stream!(trim: true) #=> #Stream<[
  enum: %File.Stream{
    path: "/home/ken/aoc_2024/priv/input/2024/day-21.inp",
    modes: [:raw, :read_ahead, :binary],
    line_or_bytes: :line,
    raw: true,
    node: :nonode@nohost
  },
  funs: [#Function<49.118167795/1 in Stream.map/2>,
   #Function<39.118167795/1 in Stream.filter/2>]
]>
|> Enum.map(fn code ->
  {number, _} = Integer.parse(code)
  {number, code}
end) #=> [{803, "803A"}, {528, "528A"}, {586, "586A"}, {341, "341A"}, {319, "319A"}]

[lib/aoc_2024/solutions/y24/day21.ex:15: Aoc2024.Solutions.Y24.Day21.parse/2]
parse2(Input.stream!(input, trim: true)) #=> [{803, "803A"}, {528, "528A"}, {586, "586A"}, {341, "341A"}, {319, "319A"}]

parse2 is your function - they return the same. I checked every other line of code multiple times, nothing stands out to me - everything is the same AFAIK. I’m flabbergasted to say the least. :face_with_monocle: :joy:

Here is some more debug data I ran with pads = [numeric() | List.duplicate(direct(), 3)]:

"BUTTONS ~c\"^>A\""
[lib/aoc_2024/solutions/y24/day21.ex:103: Aoc2024.Solutions.Y24.Day21.press_recursive/2]
cache_key #=> {65, {1, 2}, 2}

"LENGTH OF PADS 2"
[lib/aoc_2024/solutions/y24/day21.ex:105: Aoc2024.Solutions.Y24.Day21.press_recursive/2]
pad #=> %Aoc2024.Solutions.Y24.Day21.Keypad{
  position: {1, 2},
  grid: %{
    {0, 0} => :panic,
    {0, 1} => {-1, 0},
    {0, 2} => :activate,
    {1, 0} => {0, -1},
    {1, 1} => {1, 0},
    {1, 2} => {0, 1}
  },
  parse: #Function<1.134059619/1 in Aoc2024.Solutions.Y24.Day21.vector_dir>
}

[lib/aoc_2024/solutions/y24/day21.ex:107: Aoc2024.Solutions.Y24.Day21.press_recursive/2]
Process.get(cache_key) #=> {{17},
 %Aoc2024.Solutions.Y24.Day21.Keypad{
   position: {3, 2},
   grid: %{
     {0, 0} => 7,
     {0, 1} => 8,
     {0, 2} => 9,
     {1, 0} => 4,
     {1, 1} => 5,
     {1, 2} => 6,
     {2, 0} => 1,
     {2, 1} => 2,
     {2, 2} => 3,
     {3, 0} => :panic,
     {3, 1} => 0,
     {3, 2} => :activate
   },
   parse: #Function<2.134059619/1 in Aoc2024.Solutions.Y24.Day21.numeric/0>
 }}

"VALUE FOUND"
[lib/aoc_2024/solutions/y24/day21.ex:138: Aoc2024.Solutions.Y24.Day21.press_recursive/2]
pad #=> %Aoc2024.Solutions.Y24.Day21.Keypad{
  position: {3, 2},
  grid: %{
    {0, 0} => 7,
    {0, 1} => 8,
    {0, 2} => 9,
    {1, 0} => 4,
    {1, 1} => 5,
    {1, 2} => 6,
    {2, 0} => 1,
    {2, 1} => 2,
    {2, 2} => 3,
    {3, 0} => :panic,
    {3, 1} => 0,
    {3, 2} => :activate
  },
  parse: #Function<2.134059619/1 in Aoc2024.Solutions.Y24.Day21.numeric/0>
}

"BUTTONS ~c\">^A\""
[lib/aoc_2024/solutions/y24/day21.ex:103: Aoc2024.Solutions.Y24.Day21.press_recursive/2]
cache_key #=> {62, {3, 2}, 2}

"LENGTH OF PADS 2"
[lib/aoc_2024/solutions/y24/day21.ex:105: Aoc2024.Solutions.Y24.Day21.press_recursive/2]
pad #=> %Aoc2024.Solutions.Y24.Day21.Keypad{
  position: {3, 2},
  grid: %{
    {0, 0} => 7,
    {0, 1} => 8,
    {0, 2} => 9,
    {1, 0} => 4,
    {1, 1} => 5,
    {1, 2} => 6,
    {2, 0} => 1,
    {2, 1} => 2,
    {2, 2} => 3,
    {3, 0} => :panic,
    {3, 1} => 0,
    {3, 2} => :activate
  },
  parse: #Function<2.134059619/1 in Aoc2024.Solutions.Y24.Day21.numeric/0>
}

[lib/aoc_2024/solutions/y24/day21.ex:107: Aoc2024.Solutions.Y24.Day21.press_recursive/2]
Process.get(cache_key) #=> nil

">"
part_two: Aoc2024.Solutions.Y24.Day21.part_two/1 error: ** (CaseClauseError) no case clause matching: 62
    (aoc_2024 0.1.0) lib/aoc_2024/solutions/y24/day21.ex:53: anonymous fn/1 in Aoc2024.Solutions.Y24.Day21.numeric/0
    (aoc_2024 0.1.0) lib/aoc_2024/solutions/y24/day21.ex:178: Aoc2024.Solutions.Y24.Day21.Keypad.press/2
    (aoc_2024 0.1.0) lib/aoc_2024/solutions/y24/day21.ex:124: anonymous fn/3 in Aoc2024.Solutions.Y24.Day21.press_recursive/2
    (elixir 1.18.0) lib/enum.ex:1840: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (aoc_2024 0.1.0) lib/aoc_2024/solutions/y24/day21.ex:115: anonymous fn/2 in Aoc2024.Solutions.Y24.Day21.press_recursive/2
    (elixir 1.18.0) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
    (aoc_2024 0.1.0) lib/aoc_2024/solutions/y24/day21.ex:114: anonymous fn/3 in Aoc2024.Solutions.Y24.Day21.press_recursive/2
    (elixir 1.18.0) lib/enum.ex:1840: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3

The previous step succeeded because cache was found, but for the fail step nothing was found. :thinking: I’ll give it another go later, this is a puzzle beyond advent of code for me, an exercise in debugging. :sweat_smile:

0
Post #11
lud

lud

AoC.Input.stream!(path) is just File.stream!(path)

AoC.Input.stream!(path, trim: true) is the same mapped with this:

      stream
      |> Stream.map(&String.trim/1)
      |> Stream.filter(fn
        "" -> false
        _ -> true
      end)

@ken-kost your code works on my machine and finds the right answer with my input. I just copypasted everything and replaced the Aoc2024 namespace with my own.

EDIT: OK I can reproduce it now. I was testing in an ExUnit test and it worked properly, but not straight from iex

EDIT2: OK I get it, it’s the part 1 that pollutes the process dictionary for part two. I guess you tried to run that with mix aoc.run and it keeps the same process. I’m very sorry for all the time you lost because of it. I’ll make sure to spawn a new process in the command to prevent process dictionary cache pollution.

1
Post #12
ken-kost

ken-kost

I’m very sorry for all the time you lost because of it

It’s totally fine, I’m glad you figured it out. :tada:
Yea, I do mix aoc.run

I enjoy using your lib, consider me QA. :japanese_ogre:

So the process dictionary got polluted. Interesting. Makes sense now in hindsight. :face_with_peeking_eye:

1
Post #13
lud

lud

<3

I guess because how you compute the cache level. But I am not sure why.

When I read your code I see that the cache key (using length(pads)) should have 0 for the numeric keypad, as it enters map_reduce first.

But I probably missed some logic because on part_one with this code:

        [pad | pads] ->
          pad_is_numeric? =
            case map_size(pad.grid) do
              12 -> true
              6 -> false
            end

          if pad_is_numeric? do
            length(pads) |> dbg()
          end

          cache_key = {button, pad.position, length(pads)}

You get 2,

And on part_two you get 25.

1
Post #14
lud

lud

@ken-kost Can you upgrade :aoc to 0.14.0 and tell me if it’s working?

0
Post #15
ken-kost

ken-kost

Yep, working. :+1:

1
Post #16
— All posts loaded —