#advent code

1 messages · Page 2 of 1

frail dew
#

||```swift
let speltNumbersStrings = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

extension Substring {
var wholeNumberValue: Int? {
if let index = speltNumbersStrings.firstIndex(of: String(self)) {
return index + 1
} else {
return nil
}
}
}

extension AdventOfCode.Day1 {
struct Part2: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Part 2: With spelt numbers"
)

    @OptionGroup var options: AdventOfCode.Options

    mutating func run() { 
        let input = options.input
        let lines = input.components(separatedBy: .newlines)
        
        var combinedNumbers: [Int] = []

        for var line in lines {
            for (index, speltNumberString) in speltNumbersStrings.enumerated() {
                line = line.replacingOccurrences(of: speltNumberString, with: String("\(speltNumberString.first!)\(index + 1)\(speltNumberString.last!)"))
            }
            
            let numbers = line.compactMap { $0.wholeNumberValue }
            
            guard let first = numbers.first, let last = numbers.last else {
                continue
            }
            
            let lineCombinedNumbers = Int("\(first)\(last)")!

            combinedNumbers.append(lineCombinedNumbers)
        }

        let sum = combinedNumbers.reduce(0, +)

        print(sum)
    }
}

}

#

there

grave vortex
#

.reduce(0, +) is beautiful syntax
I love that language for that

frail dew
#

i really like guards and closures in swift

frail dew
#

the rest is me though

grave vortex
#

is speltNumberString.first! equivalent to speltNumberString.at(0)?

frail dew
#

yes

#

gets the first of the string and forces it to not be optional

grave vortex
#

oh so its like ts ! not ruby !

frail dew
#

yes(?)

#

what is ruby !

grave vortex
#

throw error if it fucks iirc

frail dew
#

yeah

violet crow
#

ruby ! means basically mutate in place

frail dew
#

well this will crash if it isnt optional

#

but it isnt..

#

sooo

grave vortex
#

Ima eep

frail dew
#

i wont xd

#

gn

grave vortex
#

Aaaad

#

Delete

#

Spoiler

#

Help

winter gust
#

deleted

frail dew
#

@shell roost you soon

grave vortex
#

How the fuck do I link my git without it doing that

frail dew
#

maybe it respects <>

shell roost
#

wtf is hyperfine

frail dew
grave vortex
shell roost
#

i think you can also trash react

frail dew
#

huh?

#

oh

#

for the bot nvm

shell roost
#

nop

frail dew
#

huh

shell roost
#

ah you can ❌ react

frail dew
#

oh

#

lol

frail dew
#

cool

#

ven wat are u aocing in

grave vortex
winter gust
#

rini went to sleep

#

i had to upgrade uiua to latest git

frail dew
#

gonna aim for sub-30 minutes tmrw

winter gust
#

to solve today

frail dew
#

i spent too long setting up my repo

#

gonna scaffold day2 i think

grave vortex
shell roost
winter gust
grave vortex
#

I think they mean just start new thread

winter gust
#

oh ytur

grave vortex
winter gust
#

looks fun to make

grave vortex
winter gust
#

tsx

grave vortex
#

It is tsx

winter gust
#

oh true

frail dew
#

i was gonna set up a code snippet but idk icba rn

#

too much work splitting lines into array for vsc

violet crow
#

make a test runner that checks your solve against the example every time you make a change

frail dew
#

LMAO true

violet crow
#

then cry when the example doesn't account for the edge case

frail dew
#

wait

#

its fucking friday???

#

i thought it was thursday wtf

winter gust
#

decemba firstt

#

oh yea

violet crow
grave vortex