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

bjorng
Here is my solution for day 2 of Advent of Code: https://github.com/bjorng/advent-of-code/blob/main/2024/day02/lib/day02.ex
New
bjorng
This topic is about Day 16 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
christhekeele
Continuation of Advent of Code 2022​:christmas_tree:, Day 1: Day 2! Leaderboard:
New
coen.bakker
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New
sukhmeetsd
All in all, from what I understand, it is better not to use GenServer.cast when we want some concurrent operations to happen for sure, be...
New
shritesh
This was way too easy after the last few days. Simple map, filter and count. https://github.com/shritesh/advent/blob/main/2023/06.livemd
New
New
bjorng
This topic is about Day 1 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adven...
New
stevensonmt
Anyone else think the prompt for this challenge is contradictory? The rules for comparing packets include If both values are lists, c...
New
DmitriyChernyavskiy
Hello everyone, I’m a new in elexir and functional language. I’m trying to implement Websocket interraction with server. On first layer...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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 41539 114
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement