seeplusplus

seeplusplus

Advent of Code 2024 - Day 7

This one was much easier for me than yesterday’s. Part 1 runs in 22ms and part 2 runs in ~3s.

defmodule BridgeRepair do
  def parse_input(input) do
     input
        |> String.trim()
        |> String.split("\n")
        |> Stream.map(fn l -> 
          [acc, nums] = String.split(l, ":")
          nums = nums |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)
          {acc |> String.to_integer(), nums}
        end)
  end
  def combine_ints(acc, [], _), do: acc
  def combine_ints(acc, [i | rest], ops) do
    ops
      |> Enum.flat_map(fn op -> 
        acc |> Enum.map(fn j ->
          case op do
            :plus -> j + i
            :mul -> j * i
            :cat -> "#{j}#{i}" |> String.to_integer()
          end
        end)
      end)
    |> combine_ints(rest, ops)
  end

  def part_one_ops() do
    [:plus, :mul]
  end

  def part_two_ops() do
    [:cat | part_one_ops()]
  end
  
  def solve(input, part) do
    input 
     |> BridgeRepair.parse_input()
     |> Stream.filter(fn {sum, [i | rest]} -> 
        BridgeRepair.combine_ints(
          [i],
          rest, 
          (if part == :part1, do: part_one_ops(), else: part_two_ops())
        ) |> Enum.any?(&(&1 == sum)) 
      end)
    |> Stream.map(fn {i, _} -> i end)
    |> Enum.sum
  end
end

Edit small optimization on the concat operation:

:cat -> j*10**(i |> Integer.digits() |> Enum.count) + i

reduces part 2 runtime to 800ms.

Most Liked

sevenseacat

sevenseacat

Author of Ash Framework

Much easier than yesterday! I still went through a few iterations before landing on this one:

https://github.com/sevenseacat/advent_of_code/blob/main/lib/y2024/day07.ex

This takes about 250ms to run on my machine for part 2.

My aim for this year is to have each solution run in less than a second - we’ll see how far I get lol

rugyoga

rugyoga

billylanchantin

billylanchantin

LOC: 11

defmodule Aoc2024.Day07 do
  def part1(file), do: main(file, [&+/2, &*/2])
  def part2(file), do: main(file, [&+/2, &*/2, &concat/2])
  def main(file, fs), do: file |> f2ls(&p/1) |> Enum.reduce(0, &(&2 + value(&1, fs)))
  def f2ls(f, fun), do: f |> File.read!() |> String.trim() |> String.split("\n") |> Enum.map(fun)
  def p(l), do: l |> String.replace(":", "") |> String.split() |> Enum.map(&String.to_integer/1)
  def value([out, in1 | rest], fs), do: if(search?(out, in1, rest, fs), do: out, else: 0)
  def search?(out, acc, [], _), do: out == acc
  def search?(out, acc, [h | t], fs), do: Enum.any?(fs, &search?(out, &1.(acc, h), t, fs))
  def concat(a, b), do: Integer.undigits(Integer.digits(a) ++ Integer.digits(b))
end

In my actual solution, f2ls and p are better named via an import:

import Aoc2024.Util
#...
def main(file, fs), do: file |> file_to_lines(&parse/1) |> Enum.reduce(0, &(&2 + value(&1, fs)))
def parse(line), do: line |> String.replace(":", "") |> line_to_list_of_ints

But that bumps up the line count.

Where Next?

Popular in Challenges Top

dominicletz
This topic is about Day 8 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
antoine-duchenet
Everything went smoothly today. Nothing to change to solve part 2 because I already used memoization for part 1 (it looked like an AoC e...
New
bjorng
Note: This topic is to talk about Day 4 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
New
bjorng
Note: This topic is to talk about Day 5 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
Aetherus
I tried to use combinatorial to solve today’s puzzles but failed (my brain burned out :exploding_head:). In the end I just used brute for...
New
bjorng
Note: This topic is to talk about Day 9 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
Aetherus
This topic is about Day 16 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
lud
Hello everyone! This year is going to be shorter, but the difficulty will grow faster. Today I already feel that this is not standard “D...
New
bjorng
Note: This topic is to talk about Day 13 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement