Advent of Code 2023 - Day 1

preview_player
Показать описание
I'm trying to solve this video series's Advent of Code puzzles.

Unlock unlimited opportunities with 50% off your first month of Coursera Plus

Get on the fast track to a career in cybersecurity. In this certificate program, you'll learn in-demand skills, and get AI training from Google experts. Learn at your own pace, no degree or experience required.

DigitalOcean - Dream it. Build it. Grow it.

Boost your software development career with Gen AI. Build in-demand, hands-on, Generative AI skills for elevating your software engineering game in 1 month or less.

CodeCombat Live Online Coding Classes
Use promo code explorer10 at checkout for 10% discount.

Join the channel to get access to more perks:

Or visit my blog at:

Outro music: Sanaas Scylla

#adventofcode #coding #puzzle
Рекомендации по теме
Комментарии
Автор

I always find myself surprisingly humbled by these simpler coding challenges

scubasteveVII
Автор

I found today's challenge to be surprisingly difficult for a Day 1. In previous years I've been able to do the first several days in about 10 minutes each, but this one took me some time. I had a solution that worked on the Part 2 sample inputs, but failed on my test input. Eventually I had to look up a hint, because I could not see what was wrong with my solution... If you have an input string such as "sevenine", this should be treated as "79", not "77" as I had assumed, and my code was doing.

I ended up going through the input string character by character. If it's a digit character, use that. Otherwise, compare a substring starting at the current position against each of the digit-strings ("one", "two", etc) and if it matches, use the corresponding value, and jump to the (end-1) position of the substring, so that the ending character could be used as the start of the next digit string.

It was a fun challenge, aside from the ambiguous interpretation of some input strings :)

return inputLines.sumOf { line ->
var firstDigit = -1
var lastDigit = 0

var index = 0
val char = line[index]

if (char.isDigit()) {
lastDigit = char.digitToInt()
if (firstDigit == -1) {
firstDigit = lastDigit
}
} else {
for (digit in digitMap) {
val digitCandidate = line.substring(index, min(index+digit.key.length, line.length))

if (digitCandidate == digit.key) {
lastDigit = digit.value
if (firstDigit == -1) {
firstDigit = lastDigit
}

// skip past the rest of the letters in the digit-word,
// BUT include the last letter, as it may be part of the next digit-word, eg: sevenine
// and stop checking the digitMap; continue from the top
index += digit.key.length-1
}
}
}

index++
}

val combined = firstDigit*10 + lastDigit
logger.debug("$line -> $firstDigit, $lastDigit => $combined")

combined.toLong()
}

aaronperl
Автор

I think you got a little lucky with your input... Correct me if I am wrong... You are taking the whole word number (one) and replacing it with 1?
If this is so... it does not work with my puzzle input. I had to change my code just a little bit to leave the last char of the word on because it remove expected whole word numbers in my puzzle.

Here is one line in my puzzle
The code to change it your way results in "94ctfzmjhzlpkjnbrrt1ightsrm" or 91
But for my puzzle to be correct it had to be "94ctfzmjhzlpkjnbrrt18srm" or 98

Not sure if that is a bug in AoC or if my puzzle just got really unlucky.

xenoidian
Автор

I managed todo first one in like 10 minutes, then the second one, I thought I will quit this stupid challenge :D :D I am new to coding, and did not like that you dont get any hints on how to solve it. First I did a string-search with all the different "one", "two", "three" etc, and then if I found something, I made an array with index of where it was found, and also the value (integer) of what I found. Then parsed the string for numbers and then I sorted the index and took the first and last of the array and later just like in the first challenge. But no.. its wrong answer.. tried to do a replace("one", "1") etc instead.. still dont the right answer, and since its 1000 lines of numbers, no way im going to search thru all those to find it. So no more adventure of code for me this year :) edit: can add that I took 10 lines from the data, and manually did the challenge, and then run my code on these 10 lines and it was the same result.. so yeah :/ feels too much of an elitist challenge, which is ok, but then they should write this is not for beginners :D (yes im angry :D :D

AndrewTSq
Автор

i did a funny workaround for part one: i just repalced all occurences of "one" with "one1one" and reused the same code from part 1. should cover all edgecases as well

Needforspeedfreaknd
Автор

Arguably, you might also call this video "Advent of retro coding with Java 1.2".

stakezero
Автор

does not work for some reason. i guess because some edge cases
but thanks for the video!

bcsvgabor
Автор

function compute() {
NUMBER=$(echo "$1" | tr -dc '0-9');
n1=$(printf %.1s "$NUMBER")
n2=$(printf "%s" "$NUMBER" | tail -c 1)
echo "$n1$n2"
}

total=0
input="Q1-DATA.TXT"
while IFS= read -r line
do
val=$(compute $line)
total=$(echo $total + $val | bc)
val=0
done < "$input"

echo $total

rastojanotka
Автор

at 7:03 help me understand why the multiplication by 10?

nixoncode
Автор

I think your solution not working for all cases for example 7eightwo it will produce 78 correct me if I wrong

AbdulHadiMohammed