Skip to content

Commit 4280150

Browse files
committed
day 14 comments
1 parent c6bd58c commit 4280150

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

2021/Day14/Solution.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ from line in blocks[1].Split("\n")
2525
p => new string[] { p.molecule[0] + p.element, p.element + p.molecule[1] }
2626
);
2727

28-
// cut the polymer into molecules, to get initial count:
28+
// it's enough to maintain the molecule counts in each step, no
29+
// need to deal with them one by one.
30+
31+
// cut the polymer into molecules first:
2932
var moleculeCount = (
3033
from i in Enumerable.Range(0, polymer.Length - 1)
3134

@@ -48,12 +51,17 @@ group count by generatedMolecule into g
4851
).ToDictionary(g => g.newMolecule, g => g.count);
4952
}
5053

51-
// get occurrences by element, it's enough to take just one end of each molecule:
54+
// now we need to switch to element counts, it's enough to take just one
55+
// end of each molecule, so that we don't count elements twice.
5256
var elementCounts = (
5357
from kvp in moleculeCount
54-
group kvp.Value by kvp.Key[0] into g
58+
let molecule = kvp.Key
59+
let count = kvp.Value
60+
let element = molecule[0] // take the start of the molecule
61+
group count by element into g
5562
select (element: g.Key, count: g.Sum())
5663
).ToDictionary(kvp => kvp.element, kvp => kvp.count);
64+
5765
// but then, the count of the last element of the polymer is off by one:
5866
elementCounts[polymer.Last()]++;
5967

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy