Skip to content

Commit cb56eb0

Browse files
committed
done with day12 part2
1 parent 65b3465 commit cb56eb0

File tree

4 files changed

+17
-50
lines changed

4 files changed

+17
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Below is my current advancement in Advent of code:
1919
| Day 9 | :star: (1h51) | :star: (43min) |
2020
| Day 10 | :star: (28min) | :star: (3min) |
2121
| Day 11 | :star: (28min) | :star: (1min) |
22-
| Day 12 | :star: | |
22+
| Day 12 | :star: | :star: |
2323
| Day 13 | :star: | :star: (5min) |
2424
| Day 14 | | |
2525
| Day 15 | | |

Sources/AdventOfCode.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import ArgumentParser
33
// Add each new day implementation to this array:
44
let allChallenges: [any AdventDay] = [
55
Day01(),
6-
// Day02(),
7-
// Day03(),
8-
// Day04(),
9-
// Day05(),
10-
// Day06(),
11-
// Day07(),
12-
// Day08(),
13-
// Day09(),
14-
// Day10(),
15-
// Day11(),
6+
Day02(),
7+
Day03(),
8+
Day04(),
9+
Day05(),
10+
Day06(),
11+
Day07(),
12+
Day08(),
13+
Day09(),
14+
Day10(),
15+
Day11(),
1616
Day12(),
1717
Day13(),
1818
]

Sources/Day12.swift

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ enum Border: Hashable {
2424
case .west: .west
2525
}
2626
}
27-
2827
}
2928

3029
enum CardDirection: Int, CustomStringConvertible {
@@ -36,7 +35,6 @@ enum CardDirection: Int, CustomStringConvertible {
3635
case .west: "W"
3736
}
3837
}
39-
4038
case north = 0
4139
case east
4240
case south
@@ -47,7 +45,6 @@ struct Side: CustomStringConvertible {
4745
var description: String {
4846
"Side(\(direction)|\(border) \(values))"
4947
}
50-
5148
var direction: CardDirection
5249
var border: Border
5350
var values: [Int]
@@ -60,34 +57,15 @@ struct Field {
6057
var coords: [Coord] = [Coord]()
6158
var sides: [Border: [Int]] = [Border: [Int]]()
6259
var sideCount: Int {
63-
let allSides = sides.values.map { side in
60+
let allSides = sides.map { (border, side) in
6461
let sortedSide = side.sorted()
65-
print("sorted", sortedSide)
66-
return zip(sortedSide, sortedSide.dropFirst()).map { $1 - $0 }.filter { $0 > 1 }.count + 1
62+
let diffs = zip(sortedSide, sortedSide.dropFirst()).map { $1 - $0 }
63+
let nbSides = diffs.filter { $0 > 1 }.count + 1
64+
return nbSides
6765
}
68-
print("allsides", allSides)
69-
return allSides.compactMap { $0 }.count
66+
return allSides.sum
7067
}
7168

72-
// mutating func extendSides(idx: Int, pos: Coord) {
73-
// let (border, otherAxis) = Border.create(direction: idx, coord: pos)
74-
// let sideIdx = sides.firstIndex { side in
75-
// if side.direction == border.toCardDirection() && side.border == border {
76-
// let max = side.values.max() ?? -1
77-
// let min = side.values.min() ?? -1
78-
// if max + 1 == otherAxis || min - 1 == otherAxis {
79-
// return true
80-
// }
81-
// }
82-
// return false
83-
// }
84-
// if let sideIdx {
85-
// sides[sideIdx].values.append(otherAxis)
86-
// } else {
87-
// sides.append(Side(direction: border.toCardDirection(), border: border, values: [otherAxis]))
88-
// }
89-
// }
90-
9169
mutating func addSide(idx: Int, pos: Coord) {
9270
let (border, otherAxis) = Border.create(direction: idx, coord: pos)
9371
sides[border, default: []].append(otherAxis)
@@ -136,15 +114,11 @@ struct Day12: AdventDay {
136114
// this is a frontier
137115
field.perimeter += 1
138116
field.addSide(idx: idx, pos: neighbor)
139-
// print("border found at", pos)
140-
// print(field.sides)
141117
}
142118
} else {
143119
// this is the frontier of the grid
144120
field.perimeter += 1
145121
field.addSide(idx: idx, pos: neighbor)
146-
// print(field.sideCount)
147-
148122
}
149123
}
150124
return field
@@ -160,7 +134,6 @@ struct Day12: AdventDay {
160134
guard record[currentPos] == nil else {
161135
continue
162136
}
163-
164137
if let gridValue = grid[currentPos] {
165138
// start a new field
166139
let field = exploreField(
@@ -169,22 +142,17 @@ struct Day12: AdventDay {
169142
field.coords.forEach { s in
170143
record[s] = true
171144
}
172-
print("sides of ", gridValue, field.sideCount)
173145
}
174146
}
175147
}
176148
return fields
177149
}
178150

179151
func part1() -> Int {
180-
181152
return getFields().map { $0.compute1() }.sum
182153
}
183154

184155
func part2() -> Int {
185-
// merge sides that are next to each other
186-
187156
return getFields().map { $0.compute2() }.sum
188-
189157
}
190158
}

Tests/Day12.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ struct Day12Tests {
5959
EEEC
6060
"""
6161
).part2() == 80)
62-
print("------")
6362
#expect(
6463
Day12(
6564
data: """
@@ -80,7 +79,7 @@ struct Day12Tests {
8079
ABBAAA
8180
AAAAAA
8281
"""
83-
).part2() == 236)
82+
).part2() == 368)
8483
}
8584

8685
@Test("part2")

0 commit comments

Comments
 (0)