[Advent of Code Day 2](https://adventofcode.com/2018/day/2) asks us to do some string searches. It took me about 40 minutes, and I can't say I'm particularly happy with the results, but it worked. Part 1 asks us to find the number of words in the input with exactly two of some letter, or exactly three of some letter. I decided this would be easiest if we sorted the letters in each word. (In Python I'd probably just use a dictionary and count, tbh.) I started out with a recursive solution trying to pattern match; my idea was: [] -> false xxyZ -> true xxxZ -> recursively solve Z xyZ -> recursively solve yZ Unfortunately the case where there are three matching letters at the start is buggy, Z might start with another couple of the same letter. Also, it's more idomatic in Haskell to use a higher-order function instead of writing the recursion yourself. I came up with a way to use `foldl` to handle one letter at a time, and build up the frequency table in reverse order. I don't think that lazy evaluation prevents us from processing the whole word before looking for frequency 2 or frequency 3, though. If the output list were in-order, this would work but is accessing the last element of the list fast? Or would reversing the list help? I don't know. ``` countLetter :: [(Char,Int)] -> Char -> [(Char,Int)] countLetter [] c = [(c,1)] countLetter l@((c,n):rest) d | c == d = [(c,n+1)] ++ rest | otherwise = [(d,1)] ++ l countLetters :: String -> [(Char,Int)] countLetters s = foldl countLetter [] ( sort s ) exactly :: Int -> (Char,Int) -> Bool exactly n (_,k) = (n == k) exactlyTwoOfSomeLetter :: String -> Bool exactlyTwoOfSomeLetter s = any (exactly 2) ( countLetters s ) exactlyThreeOfSomeLetter :: String -> Bool exactlyThreeOfSomeLetter s = any (exactly 3) ( countLetters s ) checksum :: [String] -> Int checksum l = (length (filter exactlyTwoOfSomeLetter l)) * (length (filter exactlyThreeOfSomeLetter l)) ``` Part 2 asks us to find two words in the list that differ by just one letter (in a particular position.) I think there are ways to solve this that aren't O(N^2) but I didn't believe any of them were going to be easy, or necessary. I wrote a recursive test for differing by one character: ``` differsByOne :: String -> String -> Bool differsByOne [] [] = False differsByOne (a:b) (c:d) | a/=c = (b==d) | otherwise = differsByOne b d ``` Maybe it would be better to zip the two words and then check the number of mismatches? But this lets us use a whole-string compare on the tail which might be more efficient if the words were large. They were 26 letters each, so I didn't need to worry about mismatched lengths. The rest of the part 2 solution is just gross: ``` findMatch :: String -> [String] -> Maybe String findMatch x l = case filter (differsByOne x) l of [] -> Nothing (a:b) -> Just a distance1 :: [String] -> Maybe (String, String) distance1 [] = Nothing distance1 (x:s) = case findMatch x s of Nothing -> distance1 s Just y -> Just (x, y) ``` I looked for an equivalent to Python's `itertools.combinations( list, 2 )` which would be a better way to get all pairs. Strangely, this does not seem to be part of the standard library. There are various horrific-looking pieces of code here: https://stackoverflow.com/questions/21265454/subsequences-of-length-n-from-list-performance/21288092#21288092 and here: https://stackoverflow.com/questions/21775378/get-all-possible-combinations-of-k-elements-from-a-list when I was really expecting to see "just use X." This Haskell package is closest to what I was expecting to find: http://hackage.haskell.org/package/permutation-0.5.0.5/docs/Data-Choose.html, but it works only on integers and so I'd still have to index into the original list. [RosettaCode](https://rosettacode.org/wiki/Combinations#Haskell) gives this one-liner: ``` import Data.List (sort, subsequences) comb m n = sort . filter ((==m) . length) $ subsequences [0..n-1] ``` which doesn't seem like a very efficient way to go about it, but it's hard for me to tell. I'm kind of boggled that there isn't a standard way of doing this common combinatorial operation. Full solution: https://github.com/mgritter/aoc2018/blob/master/day2/day2.hs Things I learned today: * Not-equals is /= instead of != * fold comes in at least 4 varieties and I understand the difference between "left" and "right" but not the primed and unprimed versions * No "all K-combinations" function?!?!
author | markgritter |
---|---|
permlink | advent-of-code-day-2-spoilers |
category | adventofcode |
json_metadata | {"tags":["adventofcode","programming","puzzle","haskell","functionalprogramming"],"links":["https://adventofcode.com/2018/day/2","https://stackoverflow.com/questions/21265454/subsequences-of-length-n-from-list-performance/21288092#21288092","https://stackoverflow.com/questions/21775378/get-all-possible-combinations-of-k-elements-from-a-list","http://hackage.haskell.org/package/permutation-0.5.0.5/docs/Data-Choose.html","https://rosettacode.org/wiki/Combinations#Haskell","https://github.com/mgritter/aoc2018/blob/master/day2/day2.hs"],"app":"steemit/0.1","format":"markdown"} |
created | 2018-12-02 19:28:09 |
last_update | 2018-12-02 19:59:57 |
depth | 0 |
children | 3 |
last_payout | 2018-12-09 19:28:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.846 HBD |
curator_payout_value | 0.238 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 4,493 |
author_reputation | 7,057,249,855,552 |
root_title | "Advent of Code day 2 [spoilers]" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,257,226 |
net_rshares | 1,817,543,257,748 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
wackou | 0 | 21,240,477,075 | 0.32% | ||
lafona-miner | 0 | 190,614,106,554 | 5% | ||
eric-boucher | 0 | 1,753,105,948 | 0.54% | ||
anwenbaumeister | 0 | 12,341,641 | 1.08% | ||
mammasitta | 0 | 240,452,761 | 0.05% | ||
liberosist | 0 | 2,908,584,150 | 1.08% | ||
cmp2020 | 0 | 25,045,296,451 | 40% | ||
lemouth | 0 | 19,915,698,116 | 3.75% | ||
lamouthe | 0 | 2,138,865,928 | 5% | ||
lk666 | 0 | 88,672,273 | 0.54% | ||
remlaps1 | 0 | 25,564,234,871 | 40% | ||
curie | 0 | 29,710,626,474 | 1.08% | ||
cub1 | 0 | 110,133,827,398 | 40% | ||
hendrikdegrote | 0 | 503,629,754,414 | 1.08% | ||
vact | 0 | 20,668,439,938 | 1.08% | ||
golbang | 0 | 2,553,622,643 | 0.32% | ||
steemstem | 0 | 198,492,274,923 | 5% | ||
remlaps2 | 0 | 3,709,697,563 | 100% | ||
dna-replication | 0 | 1,368,015,169 | 5% | ||
lisa.palmer | 0 | 3,384,602,613 | 40% | ||
cub2 | 0 | 3,521,514,551 | 100% | ||
astronomyizfun | 0 | 2,665,051,746 | 40% | ||
elviento | 0 | 415,769,443 | 1.12% | ||
moksamol | 0 | 94,157,129 | 0.54% | ||
bloom | 0 | 20,812,109,527 | 5% | ||
iansart | 0 | 695,072,911 | 0.54% | ||
kryzsec | 0 | 1,988,299,086 | 4% | ||
helo | 0 | 2,157,593,180 | 2.5% | ||
samminator | 0 | 1,450,193,761 | 2.5% | ||
locikll | 0 | 399,137,333 | 2.16% | ||
mahdiyari | 0 | 3,885,154,761 | 2.5% | ||
lorenzor | 0 | 4,743,403,610 | 50% | ||
aboutyourbiz | 0 | 171,935,425 | 1.08% | ||
paulag | 0 | 36,587,746,113 | 15% | ||
alexander.alexis | 0 | 1,865,183,006 | 5% | ||
suesa | 0 | 115,936,239,725 | 25% | ||
cryptokrieg | 0 | 86,559,300 | 1.08% | ||
corsica | 0 | 2,227,950,005 | 5% | ||
fancybrothers | 0 | 554,717,701 | 1.5% | ||
howo | 0 | 6,746,540,190 | 2.5% | ||
tsoldovieri | 0 | 211,349,933 | 2.5% | ||
nitego | 0 | 83,913,601 | 0.32% | ||
neumannsalva | 0 | 108,206,966 | 0.54% | ||
wargof | 0 | 292,812,196 | 10% | ||
abigail-dantes | 0 | 84,994,389,719 | 5% | ||
zonguin | 0 | 120,533,133 | 1.25% | ||
alexzicky | 0 | 858,002,970 | 1.25% | ||
mountain.phil28 | 0 | 3,584,526,325 | 25% | ||
iamphysical | 0 | 13,216,911,629 | 90% | ||
zest | 0 | 926,299,958 | 2.5% | ||
felixrodriguez | 0 | 167,391,113 | 2.5% | ||
azulear | 0 | 3,017,748,903 | 100% | ||
psicoluigi | 0 | 332,280,668 | 50% | ||
massivevibration | 0 | 3,538,233,169 | 5% | ||
fbslo | 0 | 170,290,838 | 0.5% | ||
nicola71 | 0 | 268,450,779 | 0.87% | ||
espoem | 0 | 1,617,631,359 | 1% | ||
erikkun28 | 0 | 0 | 1% | ||
mayowadavid | 0 | 164,918,678 | 2.5% | ||
peaceandwar | 0 | 121,016,163 | 0.54% | ||
enzor | 0 | 104,543,247 | 5% | ||
jesusj1 | 0 | 84,950,256 | 100% | ||
carloserp-2000 | 0 | 33,343,084,725 | 100% | ||
gra | 0 | 1,850,944,482 | 5% | ||
drmake | 0 | 540,113,286 | 0.54% | ||
guga34 | 0 | 86,128,562 | 3.75% | ||
amestyj | 0 | 1,737,450,055 | 50% | ||
skycae | 0 | 95,578,363 | 1.08% | ||
woolnami | 0 | 659,398,532 | 0.32% | ||
kenadis | 0 | 1,111,384,300 | 5% | ||
maticpecovnik | 0 | 181,869,376 | 2% | ||
robotics101 | 0 | 462,095,507 | 5% | ||
gentleshaid | 0 | 4,214,839,977 | 10% | ||
rantar | 0 | 25,260,915,550 | 20% | ||
ivymalifred | 0 | 1,175,768,069 | 50% | ||
sco | 0 | 3,769,790,193 | 4.5% | ||
ennyta | 0 | 1,841,374,851 | 50% | ||
rharphelle | 0 | 1,040,664,153 | 25% | ||
stahlberg | 0 | 160,258,470 | 0.54% | ||
vjap55 | 0 | 370,442,698 | 100% | ||
monie | 0 | 376,005,771 | 100% | ||
mangoish | 0 | 86,519,651 | 10% | ||
langford | 0 | 306,490,333 | 5% | ||
mathowl | 0 | 13,381,730,653 | 45% | ||
silkroadgo | 0 | 1,414,771,078 | 0.32% | ||
terrylovejoy | 0 | 717,861,682 | 2% | ||
thabiggdogg | 0 | 94,834,317 | 0.54% | ||
thetroublenotes | 0 | 244,834,071 | 2% | ||
traviseric | 0 | 236,826,878 | 50% | ||
yrmaleza | 0 | 955,692,793 | 50% | ||
penghuren | 0 | 611,552,671 | 8% | ||
mondodidave73 | 0 | 262,552,364 | 0.75% | ||
kingabesh | 0 | 113,769,661 | 2.5% | ||
miguelangel2801 | 0 | 791,012,752 | 50% | ||
didic | 0 | 487,944,020 | 0.54% | ||
wdoutjah | 0 | 177,674,422 | 7.5% | ||
emiliomoron | 0 | 1,803,354,516 | 50% | ||
dexterdev | 0 | 335,864,763 | 2.5% | ||
oghie | 0 | 557,338,966 | 50% | ||
zelenicic | 0 | 135,075,922 | 100% | ||
robertbira | 0 | 466,676,514 | 1.25% | ||
alexdory | 0 | 1,949,702,551 | 2% | ||
flugschwein | 0 | 2,122,815,403 | 4.75% | ||
benleemusic | 0 | 155,188,664 | 0.1% | ||
lianaakobian | 0 | 516,626,181 | 5% | ||
kid1412 | 0 | 0 | 1% | ||
ulisesfl17 | 0 | 1,901,035,507 | 100% | ||
arac | 0 | 1,040,851,829 | 100% | ||
francostem | 0 | 650,303,049 | 5% | ||
smawkward | 0 | 1,160,256,148 | 25% | ||
ivan-g | 0 | 79,448,281 | 0.54% | ||
croctopus | 0 | 1,416,211,878 | 100% | ||
zipporah | 0 | 223,414,503 | 0.21% | ||
sissyjill | 0 | 71,774,169 | 7% | ||
deusjudo | 0 | 88,615,251 | 0.5% | ||
emmanuel293 | 0 | 100,953,588 | 25% | ||
morbyjohn | 0 | 128,672,041 | 7% | ||
positiveninja | 0 | 96,267,501 | 0.54% | ||
tomastonyperez | 0 | 5,499,909,063 | 50% | ||
elvigia | 0 | 4,532,988,114 | 50% | ||
lesmouths-travel | 0 | 201,917,146 | 3.75% | ||
effofex | 0 | 457,226,068 | 2.5% | ||
luiscd8a | 0 | 1,957,324,957 | 80% | ||
eniolw | 0 | 1,993,816,335 | 50% | ||
de-stem | 0 | 3,222,266,471 | 4.95% | ||
geadriana | 0 | 842,580,421 | 50% | ||
elpdl | 0 | 433,838,950 | 100% | ||
serylt | 0 | 1,442,053,920 | 4.9% | ||
josedelacruz | 0 | 2,597,395,389 | 50% | ||
joseangelvs | 0 | 835,696,469 | 100% | ||
viannis | 0 | 1,937,583,270 | 50% | ||
flores39 | 0 | 431,411,205 | 100% | ||
majapesi | 0 | 251,728,291 | 50% | ||
kendallron | 0 | 226,365,303 | 15% | ||
erickyoussif | 0 | 3,526,164,892 | 100% | ||
deholt | 0 | 135,396,702 | 5% | ||
sbi4 | 0 | 8,979,381,744 | 2% | ||
indayclara | 0 | 255,829,601 | 7.5% | ||
temitayo-pelumi | 0 | 339,479,068 | 5% | ||
yusvelasquez | 0 | 436,214,125 | 50% | ||
shookriya | 0 | 999,318,773 | 22.2% | ||
alexworld | 0 | 343,819,914 | 25% | ||
frost1903 | 0 | 46,123,262 | 50% | ||
metama | 0 | 215,287,658 | 0.54% | ||
acont | 0 | 250,028,741 | 50% | ||
anaestrada12 | 0 | 8,102,514,348 | 100% | ||
steemzeiger | 0 | 227,292,337 | 4.95% | ||
blewitt | 0 | 246,804,025 | 0.1% | ||
biomimi | 0 | 191,929,499 | 40% | ||
drsensor | 0 | 520,025,667 | 3% | ||
reyvaj | 0 | 784,393,702 | 2.5% | ||
jesusfl17 | 0 | 425,700,987 | 100% | ||
ilovecryptopl | 0 | 79,497,859 | 0.86% | ||
emsonic | 0 | 257,088,939 | 0.54% | ||
yomismosoy | 0 | 205,893,105 | 50% | ||
ubaldonet | 0 | 1,894,825,206 | 80% | ||
bestofph | 0 | 5,818,660,590 | 15% | ||
call-me-howie | 0 | 349,150,259 | 0.54% | ||
mary11 | 0 | 286,092,496 | 75% | ||
doctorworm | 0 | 52,581,360,815 | 100% | ||
rgkmb-unofficial | 0 | 2,937,261,075 | 40% | ||
rgkmb | 0 | 219,702,608 | 40% | ||
wstanley226 | 0 | 1,710,674,433 | 50% | ||
gpcx86 | 0 | 2,009,314,963 | 25% | ||
reinaseq | 0 | 2,078,632,644 | 70% | ||
osariemen | 0 | 1,136,576,162 | 90% | ||
eclipticmelange | 0 | 501,607,958 | 100% | ||
markgritter | 0 | 13,326,471,774 | 100% | ||
lupafilotaxia | 0 | 6,352,423,717 | 100% | ||
fran.frey | 0 | 1,820,113,917 | 50% | ||
alaiza | 0 | 435,718,244 | 100% | ||
jrevilla | 0 | 157,316,232 | 50% | ||
particleman | 0 | 25,270,511,922 | 100% | ||
cmp2020-lite | 0 | 186,957,120 | 40% | ||
remlaps-lite | 0 | 195,356,421 | 40% | ||
themesopotamians | 0 | 4,366,886,703 | 50% | ||
m-ashurbanipal | 0 | 561,603,155 | 100% | ||
m-gilgamesh | 0 | 1,308,088,251 | 100% | ||
stem-espanol | 0 | 31,782,957,011 | 100% | ||
praditya | 0 | 652,878,312 | 24% | ||
lapp | 0 | 435,801,888 | 100% | ||
steemtpistia | 0 | 438,647,994 | 100% | ||
crassipes | 0 | 434,741,751 | 100% | ||
gbemy | 0 | 70,771,262 | 20% | ||
agrovision | 0 | 435,782,777 | 100% | ||
cryptorunway | 0 | 65,248,673 | 50% | ||
xeliram | 0 | 249,907,129 | 50% | ||
giulyfarci52 | 0 | 324,179,560 | 50% | ||
serkrav1984 | 0 | 505,335,642 | 100% | ||
eu-id | 0 | 2,443,767,417 | 10% | ||
homefree | 0 | 246,491,121 | 22.2% | ||
stem.witness | 0 | 1,100,474,718 | 5% | ||
monkeycatwithowl | 0 | 555,800,896 | 100% | ||
wilmer14molina | 0 | 150,700,087 | 10% | ||
kingnosa | 0 | 52,164,005 | 50% | ||
pamahdoo | 0 | 205,310,020 | 50% | ||
amin-ove | 0 | 89,771,668 | 50% | ||
hairgistix | 0 | 154,863,290 | 0.54% | ||
huilco | 0 | 230,652,623 | 50% | ||
hanyseek | 0 | 3,394,812 | 25% | ||
cerd26 | 0 | 81,414,413 | 100% | ||
steemmyphoto | 0 | 570,559,194 | 100% |
Im impresses, because I love learning and I love to see people learn. Keep it up, and keep sharing :-)
author | paulag |
---|---|
permlink | re-markgritter-advent-of-code-day-2-spoilers-20181205t213631120z |
category | adventofcode |
json_metadata | {"tags":["adventofcode"],"app":"steemit/0.1"} |
created | 2018-12-05 21:36:30 |
last_update | 2018-12-05 21:36:30 |
depth | 1 |
children | 0 |
last_payout | 2018-12-12 21:36:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 103 |
author_reputation | 274,264,287,951,003 |
root_title | "Advent of Code day 2 [spoilers]" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,417,952 |
net_rshares | 0 |
Wow that one looked trickier in Haskell, but good for you.
author | rantar |
---|---|
permlink | re-markgritter-advent-of-code-day-2-spoilers-20181204t031909388z |
category | adventofcode |
json_metadata | {"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["adventofcode"],"users":[],"links":[],"image":[]} |
created | 2018-12-04 03:19:09 |
last_update | 2018-12-04 03:19:09 |
depth | 1 |
children | 0 |
last_payout | 2018-12-11 03:19:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 58 |
author_reputation | 5,404,054,664,190 |
root_title | "Advent of Code day 2 [spoilers]" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,325,161 |
net_rshares | 0 |
<div class='text-justify'> <div class='pull-left'> <br /> <center> <img width='125' src='https://i.postimg.cc/9FwhnG3w/steemstem_curie.png'> </center> <br/> </div> <br /> <br /> This post has been voted on by the **SteemSTEM** curation team and voting trail in collaboration with **@curie**. <br /> If you appreciate the work we are doing then consider [voting](https://www.steemit.com/~witnesses) both projects for witness by selecting [**stem.witness**](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=stem.witness) and [**curie**](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=curie)! <br /> For additional information please join us on the [**SteemSTEM discord**]( https://discord.gg/BPARaqn) and to get to know the rest of the community! </div>
author | steemstem |
---|---|
permlink | re-markgritter-advent-of-code-day-2-spoilers-20181206t104256943z |
category | adventofcode |
json_metadata | {"app":"bloguable-bot"} |
created | 2018-12-06 10:43:00 |
last_update | 2018-12-06 10:43:00 |
depth | 1 |
children | 0 |
last_payout | 2018-12-13 10:43:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 800 |
author_reputation | 262,017,435,115,313 |
root_title | "Advent of Code day 2 [spoilers]" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,443,323 |
net_rshares | 0 |