Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.

Commit 9ce32a4

Browse files
authored
Merge pull request #4 from MarkSherstan/Day25
Day25
2 parents efb1092 + bb2201f commit 9ce32a4

5 files changed

Lines changed: 271 additions & 2 deletions

File tree

AOC2021.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
1D0D2DD628B88304001677F5 /* Day23.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D0D2DD528B88304001677F5 /* Day23.swift */; };
1313
1DC00D6A295A2A3700E458BB /* Day24.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DC00D69295A2A3700E458BB /* Day24.swift */; };
1414
1DC00D6C295A4D1F00E458BB /* Day24.txt in Resources */ = {isa = PBXBuildFile; fileRef = 1DC00D6B295A4D1F00E458BB /* Day24.txt */; };
15+
1DC00D6E295D0E5700E458BB /* Day25.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DC00D6D295D0E5700E458BB /* Day25.swift */; };
16+
1DC00D70295D0E7200E458BB /* Day25.txt in Resources */ = {isa = PBXBuildFile; fileRef = 1DC00D6F295D0E7200E458BB /* Day25.txt */; };
1517
1DDAA13928A5FF76008573D1 /* AOC2021App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DDAA13828A5FF76008573D1 /* AOC2021App.swift */; };
1618
1DDAA13B28A5FF76008573D1 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DDAA13A28A5FF76008573D1 /* ContentView.swift */; };
1719
1DDAA13D28A5FF7B008573D1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1DDAA13C28A5FF7B008573D1 /* Assets.xcassets */; };
@@ -68,6 +70,8 @@
6870
1D0D2DD528B88304001677F5 /* Day23.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Day23.swift; sourceTree = "<group>"; };
6971
1DC00D69295A2A3700E458BB /* Day24.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Day24.swift; sourceTree = "<group>"; };
7072
1DC00D6B295A4D1F00E458BB /* Day24.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = Day24.txt; sourceTree = "<group>"; };
73+
1DC00D6D295D0E5700E458BB /* Day25.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Day25.swift; sourceTree = "<group>"; };
74+
1DC00D6F295D0E7200E458BB /* Day25.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = Day25.txt; sourceTree = "<group>"; };
7175
1DDAA13528A5FF76008573D1 /* AOC2021.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AOC2021.app; sourceTree = BUILT_PRODUCTS_DIR; };
7276
1DDAA13828A5FF76008573D1 /* AOC2021App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AOC2021App.swift; sourceTree = "<group>"; };
7377
1DDAA13A28A5FF76008573D1 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -197,6 +201,7 @@
197201
1DDAA1A928AA0F6D008573D1 /* Day22.swift */,
198202
1D0D2DD528B88304001677F5 /* Day23.swift */,
199203
1DC00D69295A2A3700E458BB /* Day24.swift */,
204+
1DC00D6D295D0E5700E458BB /* Day25.swift */,
200205
);
201206
path = Code;
202207
sourceTree = "<group>";
@@ -228,6 +233,7 @@
228233
1D0710D628ADD17200C00CE2 /* Day22.txt */,
229234
1D0D2DD328B882D8001677F5 /* Day23.txt */,
230235
1DC00D6B295A4D1F00E458BB /* Day24.txt */,
236+
1DC00D6F295D0E7200E458BB /* Day25.txt */,
231237
);
232238
path = Data;
233239
sourceTree = "<group>";
@@ -295,6 +301,7 @@
295301
1DDAA17628A85AA7008573D1 /* Day10.txt in Resources */,
296302
1DDAA15828A85159008573D1 /* Day3.txt in Resources */,
297303
1DDAA14028A5FF7B008573D1 /* Preview Assets.xcassets in Resources */,
304+
1DC00D70295D0E7200E458BB /* Day25.txt in Resources */,
298305
1DDAA16E28A85A6E008573D1 /* Day6.txt in Resources */,
299306
1D0710D728ADD17200C00CE2 /* Day22.txt in Resources */,
300307
1DDAA16A28A85A52008573D1 /* Day4.txt in Resources */,
@@ -353,6 +360,7 @@
353360
1D0D2DD628B88304001677F5 /* Day23.swift in Sources */,
354361
1DDAA14D28A60D18008573D1 /* Index.swift in Sources */,
355362
1DDAA1A628A88CFD008573D1 /* Day21.swift in Sources */,
363+
1DC00D6E295D0E5700E458BB /* Day25.swift in Sources */,
356364
);
357365
runOnlyForDeploymentPostprocessing = 0;
358366
};

AOC2021/Code/Day25.swift

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//
2+
// Day25.swift
3+
// AOC2021
4+
//
5+
// Created by Mark Sherstan on 2022-12-28.
6+
//
7+
8+
import Foundation
9+
10+
class Day25 {
11+
// Matrix structure
12+
struct Matrix {
13+
var mat: [[Character]]
14+
var mat2: [[Character]]!
15+
var rows: Int!
16+
var cols: Int!
17+
18+
init(matrix: [[Character]]) {
19+
self.mat = matrix
20+
self.rows = matrix.count
21+
self.cols = matrix[0].endIndex
22+
self.mat2 = Array(repeating: Array(repeating: Character("."), count: self.cols), count: self.rows)
23+
}
24+
25+
mutating func update() {
26+
mat = mat2
27+
mat2 = Array(repeating: Array(repeating: Character("."), count: self.cols), count: self.rows)
28+
}
29+
}
30+
31+
// Vars
32+
var matrix: Matrix
33+
34+
// Read in data
35+
init() {
36+
let url = Bundle.main.url(forResource: "Day25", withExtension: "txt")!
37+
let input = try! String(contentsOf: url).split(separator: "\n")
38+
39+
var temp: [[Character]] = []
40+
for row in input {
41+
temp.append(Array(row))
42+
}
43+
44+
matrix = Matrix(matrix: temp)
45+
}
46+
47+
// Execute move commands
48+
func move(matrix: inout Matrix) {
49+
for row in 0..<matrix.rows {
50+
for col in 0..<matrix.cols {
51+
if matrix.mat[row][col] != ">" {
52+
continue
53+
}
54+
55+
if (col + 1) < matrix.cols {
56+
// In bounds
57+
if matrix.mat[row][col + 1] == "." {
58+
matrix.mat2[row][col + 1] = ">"
59+
} else {
60+
matrix.mat2[row][col] = ">"
61+
}
62+
} else {
63+
// Out of bounds
64+
if matrix.mat[row][0] == "." {
65+
matrix.mat2[row][0] = ">"
66+
} else {
67+
matrix.mat2[row][col] = ">"
68+
}
69+
}
70+
}
71+
}
72+
73+
for row in 0..<matrix.rows {
74+
for col in 0..<matrix.cols {
75+
if matrix.mat[row][col] != "v" {
76+
continue
77+
}
78+
79+
if (row + 1) < matrix.rows {
80+
// In bounds
81+
if matrix.mat2[row + 1][col] == "." && matrix.mat[row + 1][col] != "v" {
82+
matrix.mat2[row + 1][col] = "v"
83+
} else {
84+
matrix.mat2[row][col] = "v"
85+
}
86+
} else {
87+
// Out of bounds
88+
if matrix.mat2[0][col] == "." && matrix.mat[0][col] != "v" {
89+
matrix.mat2[0][col] = "v"
90+
} else {
91+
matrix.mat2[row][col] = "v"
92+
}
93+
}
94+
}
95+
}
96+
}
97+
98+
// Generic solve function
99+
func solve() -> String {
100+
var i = 1
101+
while true {
102+
move(matrix: &matrix)
103+
if matrix.mat == matrix.mat2 {
104+
return String(i)
105+
}
106+
matrix.update()
107+
i += 1
108+
}
109+
}
110+
111+
/// Part 1
112+
func part1() -> String {
113+
return solve()
114+
}
115+
116+
/// Part 2
117+
func part2() -> String {
118+
return "NA"
119+
}
120+
}

AOC2021/ContentView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import SwiftUI
99

1010
struct ContentView: View {
11-
var idx = Index(numDays: 24)
12-
@State private var selection = "Day 24"
11+
var idx = Index(numDays: 25)
12+
@State private var selection = "Day 25"
1313
@State private var p1Result: String = ""
1414
@State private var p2Result: String = ""
1515
@State private var solveTime: String = ""

0 commit comments

Comments
 (0)