this post was submitted on 15 Dec 2024
5 points (100.0% liked)

Advent Of Code

980 readers
20 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 19 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
 

So this is really kind of tangential, I am just messing around with the data and trying to at least nail down some basics so I can do sanity checks in my actual code. I am running into an odd discrepancy when trying to determine the number of times "XMAS" occurs in the input. I am only concerned with the straight forward matches, the instances of the substring "XMAS" appearing in the raw data.

When i do "grep -c XMAS input" or "rg -c XMAS input" they both show 107. But when I use regex101.com and search for the pattern XMAS, it shows 191 matches. Please help, I am truly at a loss here. While writing this, it occurred to me to just try using string.count("XMAS") in python on the data as a raw string, and it also returns 191. So really this question is more about grep and rp than anything. why are they only returning 107?

top 1 comments
sorted by: hot top controversial new old
[–] [email protected] 7 points 1 week ago* (last edited 1 week ago)

Which input are you using? https://adventofcode.com/2024/day/4/input? It looks like you're not accounting right for times when XMAS appears multiple times per line. grep counts the number of lines where something appears, not the total number of times it appears. Here's a quick way to do it, although my numbers don't match yours:

$ grep -c XMAS words
112
$ grep -c 'XMAS.*XMAS' words
56
$ grep -c 'XMAS.*XMAS.*XMAS' words
22
$ grep -c 'XMAS.*XMAS.*XMAS.*XMAS' words
7
$ grep -c 'XMAS.*XMAS.*XMAS.*XMAS.*XMAS' words
1
$ grep -c 'XMAS.*XMAS.*XMAS.*XMAS.*XMAS.*XMAS' words
0
$ echo '1*5+(7-1)*4+(22-7)*3+(56-22)*2+(112-56)' | bc
198

So 198 instances of XMAS, appearing horizontally from left to right.

Also, yes, I could have just added up all the numbers, I didn't realize that right away.

Edit: Better:

$ grep -o XMAS words | wc -l
198