Today, my girlfriend and I started with this year's Advent of Code to improve our Python. So far we only did Day 1 and the first part of Day 2. The latter, I found to be very fun, so, to spice it up a little, and with a tip o' the head to Eric Wastl, here's my attempt at turning it into a Code Golf challenge.
Task
Your task, should you accept it, would be to determine the number of lines where the number of occurrences of char in each string is at least min and at most max. With the above data, a valid entry will output 23¹.
Valid entries shall not only work with the below two example data sets, but with any data set produced by the Advent of Code website's Day 2 Part 1 puzzle (so head on over there already - but just going with the examples is fine, too). Other formats of input are not allowed. Specifically, the entry has to handle the newlines itself.
I've posted an answer with my Ruby version along with this Question in the hopes that it will help.
Input
- A multiline string where each line/element contains a number
min, a numbermax, a characterchar, and a stringpassword² (i.e.,min-max char: password). minandmaxare positive integers, andmaxis guaranteed to be greater than or equal tomin.- As far as I can tell, the
passwords are all ASCII-alphabetical, as is thechar.
Output
- A number representing how many lines had at least
minand at mostmaxoccurrences ofcharinpassword.
Scoring
- The winner shall be the shortest entry by characters³, not bytes (to make sure that variable names such as
🎅,🦌and🎄don't put contestants in the mood of the season at a disadvantage).
(Shorter) Test Case
Output: 3 Input: 1-5 c: abcdefg 5-12 j: abcdefg 1-5 z: zzzzzzzz 3-3 h: hahaha 4-20 e: egejeqwee (Longer) Example
Output: 23 Input: 3-8 j: ksjjtvnjbjppjjjl 6-10 s: sszlkrsssss 1-4 z: znzfpz 7-11 m: dfkcbxmxmnmmtvmtdn 6-9 h: hlhhkhhhq 8-9 p: ppppppppvp 6-9 c: cccgccccmcch 5-8 g: bgkggjgtvggn 3-4 x: xxhk 10-12 h: hhhhhhhhhlhhhhdnh 5-6 j: zjjsjn 9-14 s: ksclwttsmpjtds 6-7 l: gtdcblql 1-4 d: dkndjkcd 1-6 k: kkkkkckmc 10-12 x: xxxxxxxxxrxl 2-5 v: fxdjtv 3-5 q: pjmkqdmqnzqppr 5-8 t: xtgtgtcht 6-11 j: nftjzjmfljqjrc 6-9 g: rskgggmgmwjggggvgb 11-12 v: vkvdmvdvvvdv 15-16 z: zzzzzzzzzzzzzzgxz 13-17 v: vvvvvvvvvvvvvvvvnv 9-14 j: clrfkrwhjtvzvqqj 1-12 l: lljljznslllffhblz 4-7 c: cccgcxclc 6-11 z: tbxqrzzbwbr 18-19 m: jmbmmqmshmczlphfgmf 1-5 s: ssvsq 4-5 k: kkkkw 4-7 f: fcqffxff 3-8 f: fffzthzf 9-12 v: vvvvvvvvvvvmvv 14-17 f: ftflcfffjtjrvfkffmvf 4-7 n: pnmnxnqqjp 6-7 r: rrrkrmr 4-13 k: drckdzxrsmzrkqckn 5-14 t: dttttttttttttt 13-14 v: vvvvfvvvvvvvvvv 7-12 j: jrljwnctjqjjxj 6-7 v: wchfzvm 1-8 d: dddddddzd 10-12 g: gvvzrglgrgggggggggg ¹: But, say puts 23 (that is, without doing the actual work) is no valid entry.
²: On Advent of Code, the background to the puzzle is that you're helping a toboggan rental company with their computer trouble. For some reason, their password database has been jumbled up and you're trying to fix it, which requires you to figure out which passwords adhere to the password policy, which solely consists of a specific character (char) to be present at least and at most a certain number of times.
³: As determined via wc -m.
minandmax? Also, reading from a file is a rather strict input format - some languages can't do that, you can't really test it online, and it's just inconvenient. \$\endgroup\$