Skip to content

Commit b8c4ecb

Browse files
committed
speeeeed day4
1 parent 2718b4c commit b8c4ecb

File tree

2 files changed

+61
-55
lines changed

2 files changed

+61
-55
lines changed

Sources/Day04.swift

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
11
import Algorithms
22

33
func searchInNeighbors(pos: Coord, _ grid: Grid) -> [Coord] {
4-
pos.fullNeighbors.included(in: grid).filter { n in
5-
grid[n] == "M"
6-
}
4+
pos.fullNeighbors.included(in: grid).filter { n in
5+
grid[n] == "M"
6+
}
77
}
88

99
func isMAS(pos: Coord, _ grid: Grid) -> Bool {
10-
let candidates = pos.cornerNeighbors
11-
if (candidates.allSatisfy { $0.isInside(grid: grid) }) {
12-
let l = candidates.map { grid[$0] }
13-
let letterSet = Set(l)
14-
// same letter can not be on opposite side
15-
return l[0] != l[2] && l[1] != l[3] && letterSet.count == 2 && letterSet == ["M", "S"]
16-
}
17-
return false
10+
let candidates = pos.cornerNeighbors
11+
if (candidates.allSatisfy { $0.isInside(grid: grid) }) {
12+
let l = candidates.map { grid[$0] }
13+
let letterSet = Set(l)
14+
// same letter can not be on opposite side
15+
return l[0] != l[2] && l[1] != l[3] && letterSet.count == 2 && letterSet == ["M", "S"]
16+
}
17+
return false
1818
}
1919

2020
func isWhole(pos: Coord, inDir: Coord, grid: Grid) -> Bool {
21-
let aPos = pos + inDir
22-
let sPos = aPos + inDir
23-
if aPos.isInside(grid: grid) && sPos.isInside(grid: grid) {
24-
return grid[aPos] == "A" && grid[sPos] == "S"
25-
}
26-
return false
21+
let aPos = pos + inDir
22+
let sPos = aPos + inDir
23+
if aPos.isInside(grid: grid) && sPos.isInside(grid: grid) {
24+
return grid[aPos] == "A" && grid[sPos] == "S"
25+
}
26+
return false
2727
}
2828

2929
struct Day04: AdventDay {
30-
var data: String
31-
var grid: Grid {
32-
Grid(from: data)
33-
}
34-
35-
func countAllWhere(letter search: Character, predicate: (Coord, Grid) -> Bool) -> Int {
36-
grid.raw.enumerated().map { (y, line) in
37-
line.enumerated().map { (x, letter) in
38-
if letter == search {
39-
return predicate(Coord(x: x, y: y), grid)
40-
}
41-
return false
42-
}
43-
}.flatMap { $0 }.count { $0 == true }
44-
}
45-
46-
func part1() -> Int {
47-
var count = 0
48-
// get all positions of "X"
49-
for (y, line) in grid.raw.enumerated() {
50-
for (x, letter) in line.enumerated() {
51-
if letter == "X" {
52-
let currentPos = Coord(x: x, y: y)
53-
let results = searchInNeighbors(pos: currentPos, grid)
54-
if results.count > 0 {
55-
for candidate in results {
56-
let inDir = candidate - currentPos
57-
if isWhole(pos: candidate, inDir: inDir, grid: grid) {
58-
count += 1
59-
}
30+
var grid: Grid
31+
init(data: String) {
32+
grid = Grid(from: data)
33+
}
34+
35+
func countAllWhere(letter search: Character, predicate: (Coord, Grid) -> Bool) -> Int {
36+
grid.raw.enumerated().map { (y, line) in
37+
line.enumerated().map { (x, letter) in
38+
if letter == search {
39+
return predicate(Coord(x: x, y: y), grid)
40+
}
41+
return false
42+
}
43+
}.flatMap { $0 }.count { $0 == true }
44+
}
45+
46+
func part1() -> Int {
47+
var count = 0
48+
// get all positions of "X"
49+
for (y, line) in grid.raw.enumerated() {
50+
for (x, letter) in line.enumerated() {
51+
if letter == "X" {
52+
let currentPos = Coord(x: x, y: y)
53+
let results = searchInNeighbors(pos: currentPos, grid)
54+
if results.count > 0 {
55+
for candidate in results {
56+
let inDir = candidate - currentPos
57+
if isWhole(pos: candidate, inDir: inDir, grid: grid) {
58+
count += 1
59+
}
60+
}
61+
}
62+
}
6063
}
61-
}
6264
}
63-
}
65+
return count
66+
}
67+
68+
func part2() -> Int {
69+
countAllWhere(letter: "A", predicate: isMAS)
6470
}
65-
return count
66-
}
67-
68-
func part2() -> Int {
69-
countAllWhere(letter: "A", predicate: isMAS)
70-
}
7171
}

Sources/Helpers/Grid.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ extension Grid {
2525
init(from data: String) {
2626
self.raw = data.lines().map { Array($0) }
2727
}
28+
29+
func toText() -> String {
30+
String(self.raw.map {
31+
String($0)
32+
}.joined(by: "\n"))
33+
}
2834

2935
func includes(coord: Coord) -> Bool {
3036
coord.x >= 0 && coord.x <= self.width - 1 && coord.y >= 0 && coord.y <= self.height - 1

0 commit comments

Comments
 (0)