Ume Channel
12/01/2023, 7:34 PMGood 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.gitmulhollandj
12/01/2023, 8:44 PMOzioma Ogbe
12/02/2023, 12:43 AMlistOf(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.Ume Channel
12/02/2023, 3:19 AMOzioma Ogbe
12/02/2023, 3:48 AMOzioma Ogbe
12/02/2023, 3:50 AMOzioma Ogbe
12/02/2023, 4:09 AMfs.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))
});