https://kotlinlang.org logo
#advent-of-code
Title
# advent-of-code
u

Ume Channel

12/01/2023, 7:34 PM
Copy code
Good day everyone - someone help me!
I compare my solution in host in live youtube -  I really struggle this what went wrong - (src/dayprac)
I spend a time on this
As you can see there's empty list in the minus result, but the difference between the first and second result is 233, and the difference between the first and third result is 226, and the difference between the second and third result is -7, so the minus result should be [233, 226, -7] but it's empty list, why?
https://github.com/georgekarlr/aoc-2023-in-kotlin.git
m

mulhollandj

12/01/2023, 8:44 PM
I don't see any tests. Please write some tests first.
o

Ozioma Ogbe

12/02/2023, 12:43 AM
1. The reason your getting an empty list is because both list contains the same numbers. youSolutionList - hostSolutionList would give empty. Example
listOf(1,1,1,1,1,1,2,2,2,2,2,2,2) - listOf(1,2) = emptyList()
they contain the same number even though its different frequency. 2. The reason your solution does not work is because regex matcher works from left to right so your solution would match give you
two
and
three
for this test case
twohdggfgffhfhthreeight
when it should give you two and eight notice how threeight is overlapping between three and eight. here is an example from the actual input line 228
dtkjdncq73threechgcccdgqqsixthreehlfroneightn
you should get
7
and
eight
but you got
7
and
one
because of the overlap. One way to work around this is to use different regex to match the first number and the second number. For the first number you can use your regex as it is, for the second number you have to reverse the string and match against the numbers reversed, like nine -> enin and one -> eno.
💯 2
K 1
🦜 1
🐕 1
K 1
u

Ume Channel

12/02/2023, 3:19 AM
Wow that's amazing - how did you find out. You are really amazing - damn! What tricks did you use?
o

Ozioma Ogbe

12/02/2023, 3:48 AM
I actually made the same mistake when solving mine using regex. I googled and learned about look ahead regex and that’s what I used to solve mine.
That’s the look ahead solution
💯 1
❤️ 1
🐕 1
Here is a solution using the method I described of reversing the numbers but I wrote it in typescript typescript import * as fs from 'fs';
Copy code
fs.readFile('input1.txt', 'utf8', function(err, input){ 
  console.log(input.split('\n').map((line) => {
    let digitmap = new Map<string, string>([
        ["1", "1"],["2", "2"],["3", "3"],["4", "4"],["5", "5"],
        ["6", "6"],["7", "7"],["8", "8"],["9", "9"]
    ])
    let forWardMap = new Map([
            ...digitmap.entries(),
        ...new Map<string, string>( [["one", "1"],["two", "2"],["three", "3"],["four", "4"],
        ["five", "5"],["six", "6"],["seven", "7"],["eight", "8"],["nine", "9"],]).entries()
    ])

    let reverseMap = new Map([
        ...digitmap.entries(),
        ...new Map<string, string>(  [["eno", "1"],["owt", "2"],["eerht", "3"],["ruof", "4"],["evif", "5"],["xis", "6"],["neves", "7"],["thgie", "8"],["enin", "9"],]).entries()
    ])
    const line2 = line.split("").reverse().join("")
      const matched = line.match(/(\d|eight|one|two|three|four|five|six|seven|nine)/g)
      const matched1 = line2.match(/(\d|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin)/g)
      return +(forWardMap.get(matched[0]) +  reverseMap.get(matched1[0]))
  }).reduce((acc, curr) => acc + curr, 0))
});
K 1
🦜 1
🐕 1
💯 1
24 Views