Skip to content

Commit 41afb7e

Browse files
committed
2016 day 9 part 2
1 parent c0e7862 commit 41afb7e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

aoc16.playground/Pages/Day09.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
import Foundation
44

55
part1()
6+
part2()
67

78
//: [Next](@next)

aoc16.playground/Pages/Day09.xcplaygroundpage/Sources/Day09.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,30 @@ public func part1() -> Int {
3232
}
3333
return output.count
3434
}
35+
36+
public func part2() -> Int {
37+
let input = Array(stringsFromFile()[0])
38+
var result = 0
39+
var weights = Array(repeating: 1, count: input.count)
40+
var marker: [Character]? = nil
41+
for (i, ch) in input.enumerated() {
42+
if let buffer = marker {
43+
if ch == ")" {
44+
let parts = String(buffer).split(separator: "x")
45+
marker = nil
46+
let length = Int(parts[0])!
47+
let count = Int(parts[1])!
48+
for j in (i+1)...(i+length) where j < weights.count {
49+
weights[j] *= count
50+
}
51+
} else {
52+
marker?.append(ch)
53+
}
54+
} else if ch == "(" {
55+
marker = []
56+
} else {
57+
result += weights[i]
58+
}
59+
}
60+
return result
61+
}

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