23

Day 3: Foggy Forest

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] eta@feddit.org 4 points 1 day ago* (last edited 1 day ago)

Elixir

For part 1 I iterated over the lists and took some time figuring out what the end condition for this should actually be. Not really proud of this part but it works.

So for part 2 I started over and did it with an approach kind of using a starting point, vector to a second point and then calculating point 3 and 4 from there. Then each point is checked wether it's out of bounds and if it has a tree planted there. It's taking a few seconds to complete so it's not running really fast but I like the approach more than my part 1 and it could probably be optimised by rearranging some checks so it stop checking earlier.

Edit: Rearranging checks doesn't do much, this approach is just slow. It's probably better to get the coordinates for all trees and work with that as some other solutions did.

solution

#!/usr/bin/env elixir

defmodule AOC do

def getData(inputStr) do
	inputStr
	|> String.split("\n", trim: true)
	|> Enum.map(fn s -> String.graphemes(s) end)
end

def part1(data) do
	0..length(data)-2
	|> Enum.map(
		fn r ->
			0..length(Enum.at(data,0))-2
			|> Enum.map(
				fn c ->
					case Enum.at(Enum.at(data,r),c) do
						"#" -> checkPos(data, r, c)
						_ -> 0
					end
				end
			)
			|> Enum.sum()
		end
	)
	|> Enum.sum()
end

def checkPos(data, r, c) do
	ub = min(length(data)-1-r, length(Enum.at(data,0))-1-c)
	c+1..c+ub
	|> Enum.map(
		fn c2 ->
			dist = c2 - c
			if Enum.at(Enum.at(data,r),c2) == "#" and
			Enum.at(Enum.at(data,r+dist),c) == "#" and
			Enum.at(Enum.at(data,r+dist),c2) == "#" do
				1
			else
				0
			end
		end
	)
	|> Enum.sum()
end

def part2(data) do
	for r <- 0..length(data)-2, c <- 0..length(data)-2 do
		case Enum.at(Enum.at(data,r),c) do
			"#" -> checkPos2(data, r, c)
			_ -> 0
		end
	end
	|> Enum.sum()
end

def checkPos2(data, r, c) do
	for dr <- 1..length(data)-1, dc <- -c..0 do
		{r2, c2, r3, c3} = {r+dr, c+dc, r-dc, c+dr}
		{r4, c4} = {r3+dr, c3+dc}
		if r3 < length(data) and c3 < length(data) and
		r4 < length(data) and c4 < length(data) do
			if Enum.at(Enum.at(data,r2),c2) == "#" and
			Enum.at(Enum.at(data,r3),c3) == "#" and
			Enum.at(Enum.at(data,r4),c4) == "#" do
				1
			else
				0
			end
		else
			0
		end
	end
	|> Enum.sum()
end

end # module

input = AOC.getData(File.read!("input/day03.txt"))
IO.puts "part 1 #{AOC.part1(input)}"
IO.puts "part 2 #{AOC.part2(input)}"

this post was submitted on 03 Dec 2026
23 points (100.0% liked)

Advent Of Code

1278 readers
56 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2026

Solution Threads

M T W T F S S
1 2 3 4 5 6
7 8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 3 years ago
MODERATORS