Day 10 part 2 and cleanup

This commit is contained in:
Daniel Flanagan 2020-12-10 12:37:10 -06:00
parent 014890191a
commit 84a210170f
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
2 changed files with 23 additions and 4 deletions

View file

@ -1,4 +1,4 @@
import streams, input_helpers, algorithm, sequtils import streams, input_helpers, algorithm, sequtils, math
proc part1*(s: Stream): int = proc part1*(s: Stream): int =
var d1 = 0 var d1 = 0
@ -11,6 +11,25 @@ proc part1*(s: Stream): int =
else: discard else: discard
d1 * d3 d1 * d3
proc part2*(s: Stream): int = proc part2*(s: Stream): int =
0 result = 1
var nums = @[0].concat(toSeq(s.asInts).sorted)
nums = nums.concat @[nums.max + 3]
var l,r = 0
while l < nums.len:
r = l
while r < (nums.len-1) and nums[r+1] - nums[r] == 1: inc r
let ln = r+1-l
case ln:
of 0..2: discard
of 3: result *= 2
else: result *= 1 + (ln - 2) + (ln - 2).fac.div(2)
l = r + 1
# needed the tests on this one - math was a doozy
const t2 = "1\n4\n5\n6\n7\n10\n11\n12\n15\n16\n19".newStringStream().part2()
const t = ("1\n2\n3\n4\n7\n8\n9\n10\n11\n14\n17\n18\n19\n20\n23\n24\n25\n" &
"28\n31\n32\n33\n34\n35\n38\n39\n42\n45\n46\n47\n48\n49").newStringStream().part2()
doAssert 8 == t2
doAssert 19208 == t

View file

@ -1,4 +1,4 @@
import streams, sequtils, sets, options, deques, input_helpers, day1 import streams, sequtils, options, deques, input_helpers, day1
const WINDOW = 25 const WINDOW = 25