this post was submitted on 03 Dec 2024
19 points (100.0% liked)

Advent Of Code

885 readers
133 users here now

An unofficial home for the advent of code community on programming.dev!

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.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 18 20 21 22
23 24 25

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 1 year ago
MODERATORS
 

Day 3: Mull It Over

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
[โ€“] Karmmah 1 points 1 day ago* (last edited 1 day ago)

Julia

I did not try to make my solution concise and kept separate code for part 1 and part 2 with test cases for both to check if I broke anything. But after struggling with Day 2 I am quite pleased to have solved Day 3 with only a little bugfixing.

function calcLineResult(line::String)
	lineResult::Int = 0
	enabled::Bool = true
	for i=1 : length(line)
		line[i]!='m' ? continue : (i<length(line) ? i+=1 : continue)
		line[i]!='u' ? continue : (i<length(line) ? i+=1 : continue)
		line[i]!='l' ? continue : (i<length(line) ? i+=1 : continue)
		line[i]!='(' ? continue : (i<length(line) ? i+=1 : continue)
		num1Str::String = ""
		while line[i] in ['0','1','2','3','4','5','6','7','8','9'] #should check for length of digits < 3, but works without
			num1Str = num1Str*line[i]; (i<length(line) ? i+=1 : continue)
		end
		line[i]!=',' ? continue : (i<length(line) ? i+=1 : continue)
		num2Str::String = ""
		while line[i] in ['0','1','2','3','4','5','6','7','8','9'] #should check for length of digits < 3, but works without
			num2Str = num2Str*line[i]; (i<length(line) ? i+=1 : continue)
		end
		line[i]==')' ? lineResult+=parse(Int,num1Str)*parse(Int,num2Str) : continue
	end
	return lineResult
end

function calcLineResultWithEnabling(line::String,enabled::Bool)
	lineResult::Int = 0
	for i=1 : length(line)
		if enabled && line[i] == 'm'
			i<length(line) ? i += 1 : continue
			line[i]!='u' ? continue : (i<length(line) ? i+=1 : continue)
			line[i]!='l' ? continue : (i<length(line) ? i+=1 : continue)
			line[i]!='(' ? continue : (i<length(line) ? i+=1 : continue)
			num1Str::String = ""
			while line[i] in ['0','1','2','3','4','5','6','7','8','9']
				num1Str = num1Str*line[i]; (i<length(line) ? i+=1 : continue)
			end
			line[i]!=',' ? continue : (i<length(line) ? i+=1 : continue)
			num2Str::String = ""
			while line[i] in ['0','1','2','3','4','5','6','7','8','9']
				num2Str = num2Str*line[i]; (i<length(line) ? i+=1 : continue)
			end
			line[i]==')' ? lineResult+=parse(Int,num1Str)*parse(Int,num2Str) : continue
		elseif line[i] == 'd'
			i<length(line) ? i += 1 : continue
			line[i]!='o' ? continue : (i<length(line) ? i+=1 : continue)
			if line[i] == '('
				i<length(line) ? i += 1 : continue
				line[i]==')' ? enabled=true : continue
				#@info i,line[i-3:i]
			elseif line[i] == 'n'
				i<length(line) ? i += 1 : continue
				line[i]!=''' ? continue : (i<length(line) ? i+=1 : continue)
				line[i]!='t' ? continue : (i<length(line) ? i+=1 : continue)
				line[i]!='(' ? continue : (i<length(line) ? i+=1 : continue)
				line[i]==')' ? enabled=false : continue
				#@info i,line[i-6:i]
			else
				nothing
			end
		end
	end
	return lineResult,enabled
end

function calcMemoryResult(inputFile::String,useEnabling::Bool)
	memoryRes::Int = 0
	f = open(inputFile,"r")
	lines = readlines(f)
	close(f)
	enabled::Bool = true
	for line in lines
		if useEnabling
			lineRes::Int,enabled = calcLineResultWithEnabling(line,enabled)
			memoryRes += lineRes
		else
			memoryRes += calcLineResult(line)
		end
	end
	return memoryRes
end

if abspath(PROGRAM_FILE) == @__FILE__
	@info "Part 1"
	@debug "checking test input"
	inputFile::String = "day03InputTest"
	memoryRes::Int = calcMemoryResult(inputFile,false)
	try
		@assert memoryRes==161
	catch e
		throw(ErrorException("$e memoryRes=$memoryRes"))
	end
	@debug "test input ok"
	@debug "running real input"
	inputFile::String = "day03Input"
	memoryRes::Int = calcMemoryResult(inputFile,false)
	try
		@assert memoryRes==153469856
	catch e
		throw(ErrorException("$e memoryRes=$memoryRes"))
	end
	println("memory result: $memoryRes")
	@debug "real input ok"

	@info "Part 2"
	@debug "checking test input"
	inputFile::String = "day03InputTest"
	memoryRes::Int = calcMemoryResult(inputFile,true)
	try
		@assert memoryRes==48
	catch e
		throw(ErrorException("$e memoryRes=$memoryRes"))
	end
	@debug "test input ok"
	@debug "running real input"
	inputFile::String = "day03Input"
	memoryRes::Int = calcMemoryResult(inputFile,true)
	try
		@assert memoryRes==77055967
	catch e
		throw(ErrorException("$e memoryRes=$memoryRes"))
	end
	println("memory result: $memoryRes")
	@debug "real input ok"

end