Skip to content

Commit 0436d08

Browse files
committed
day2 done
1 parent 80da819 commit 0436d08

File tree

5 files changed

+1091
-7
lines changed

5 files changed

+1091
-7
lines changed

2021/adventofcode.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ABCB126BD61EBC097A1FBD5D /* SimpleError.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCB163CCB3DF19256E3ADCB /* SimpleError.swift */; };
1111
ABCB141AA99DBF03022E6C42 /* day01.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCB1F5CDAD661579BDFB97A /* day01.swift */; };
1212
ABCB1547F4757B4A5384C213 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCB1F64F82975B897AD4AA6 /* Utils.swift */; };
13+
ABCB1A8D8C8C8615FD68B07D /* day02.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCB14FA942C5989E6DC0A1A /* day02.swift */; };
1314
ABCB1C0BA0E8304627F64005 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCB1647013E256164F48DBD /* main.swift */; };
1415
/* End PBXBuildFile section */
1516

@@ -27,6 +28,7 @@
2728

2829
/* Begin PBXFileReference section */
2930
ABCB12FB57EF41342FB843A5 /* adventofcode */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = adventofcode; sourceTree = BUILT_PRODUCTS_DIR; };
31+
ABCB14FA942C5989E6DC0A1A /* day02.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day02.swift; sourceTree = "<group>"; };
3032
ABCB163CCB3DF19256E3ADCB /* SimpleError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleError.swift; sourceTree = "<group>"; };
3133
ABCB1647013E256164F48DBD /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
3234
ABCB1B9EE9236FD6BA3DE934 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
@@ -60,6 +62,7 @@
6062
ABCB1F5CDAD661579BDFB97A /* day01.swift */,
6163
ABCB163CCB3DF19256E3ADCB /* SimpleError.swift */,
6264
ABCB1F64F82975B897AD4AA6 /* Utils.swift */,
65+
ABCB14FA942C5989E6DC0A1A /* day02.swift */,
6366
);
6467
path = adventofcode;
6568
sourceTree = "<group>";
@@ -124,6 +127,7 @@
124127
ABCB141AA99DBF03022E6C42 /* day01.swift in Sources */,
125128
ABCB126BD61EBC097A1FBD5D /* SimpleError.swift in Sources */,
126129
ABCB1547F4757B4A5384C213 /* Utils.swift in Sources */,
130+
ABCB1A8D8C8C8615FD68B07D /* day02.swift in Sources */,
127131
);
128132
runOnlyForDeploymentPostprocessing = 0;
129133
};

2021/adventofcode/day02.swift

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// Created by Andrew Stanton-Nurse on 12/3/21.
3+
//
4+
5+
import Foundation
6+
7+
struct Coordinate {
8+
var depth = 0
9+
var position = 0
10+
}
11+
12+
extension Coordinate {
13+
static func +(left: Coordinate, right: Coordinate) -> Coordinate {
14+
Coordinate(depth: left.depth + right.depth, position: left.position + right.position)
15+
}
16+
}
17+
18+
class Submarine {
19+
var location = Coordinate()
20+
var aim = 0
21+
22+
func move(by: Coordinate) {
23+
aim += by.depth
24+
location = Coordinate(
25+
depth: location.depth + (by.position * aim),
26+
position: location.position + by.position)
27+
}
28+
}
29+
30+
func parseInstruction(s: Substring) throws -> Coordinate {
31+
let splat = s.split(separator: " ")
32+
if splat.count != 2 {
33+
throw SimpleError.error("Invalid instruction: \(s))")
34+
}
35+
guard let count = Int(splat[1]) else {
36+
throw SimpleError.error("Distance is not an integer: \(splat[1])")
37+
}
38+
39+
switch splat[0] {
40+
case "forward":
41+
return Coordinate(depth: 0, position: count)
42+
case "down":
43+
return Coordinate(depth: count, position: 0)
44+
case "up":
45+
return Coordinate(depth: -count, position: 0)
46+
case let x:
47+
throw SimpleError.error("Unknown direction: \(x)")
48+
}
49+
}
50+
51+
func day02(_ args: ArraySlice<String>) throws {
52+
guard let input = args.first else {
53+
throw SimpleError.error("Usage: adventofcode 2 <input>")
54+
}
55+
let data = try parseLines(path: input, converter: parseInstruction)
56+
57+
let part1 = data.reduce(Coordinate(), +)
58+
print("Part 1:")
59+
dumpResult(result: part1)
60+
61+
let sub = Submarine()
62+
for inst in data {
63+
sub.move(by: inst)
64+
}
65+
print("Part 2")
66+
dumpResult(result: sub.location)
67+
}
68+
69+
func dumpResult(result: Coordinate) {
70+
print(" Depth: \(result.depth)")
71+
print(" Position: \(result.position)")
72+
print(" Result: \(result.position * result.depth)")
73+
}

2021/adventofcode/main.swift

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88

99
import Foundation
1010

11+
let days = [
12+
day01,
13+
day02
14+
]
15+
1116
func runDay(number: Int, args: ArraySlice<String>) throws {
12-
switch number {
13-
case 1:
14-
try day01(args);
15-
break;
16-
default:
17-
fputs("Day \(number) not implemented!\n", stderr)
18-
exit(1)
17+
if number < 1 || number > days.count {
18+
throw SimpleError.error("Day not implemented: \(number)")
1919
}
20+
try days[number - 1](args)
2021
}
2122

2223
if CommandLine.arguments.count < 2 {

0 commit comments

Comments
 (0)