Skip to content

Commit 1fb0e30

Browse files
committed
check in
0 parents  commit 1fb0e30

File tree

21 files changed

+464
-0
lines changed

21 files changed

+464
-0
lines changed

Diff for: .DS_Store

8 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import Foundation
2+
3+
public let inputString = """
4+
132709
5+
102150
6+
126463
7+
85035
8+
77219
9+
86458
10+
119251
11+
121098
12+
118730
13+
122505
14+
127964
15+
68004
16+
55833
17+
77664
18+
142865
19+
124503
20+
115892
21+
87236
22+
122743
23+
127096
24+
94893
25+
62129
26+
56520
27+
117000
28+
81519
29+
121719
30+
96291
31+
96556
32+
79006
33+
137122
34+
124340
35+
125151
36+
51603
37+
50132
38+
67568
39+
132599
40+
149009
41+
60997
42+
99382
43+
96506
44+
57269
45+
118133
46+
115119
47+
126208
48+
101098
49+
60514
50+
146171
51+
70314
52+
76473
53+
51209
54+
99190
55+
57647
56+
126985
57+
142055
58+
99615
59+
146442
60+
129520
61+
145334
62+
57799
63+
87148
64+
118362
65+
80407
66+
106449
67+
57146
68+
129035
69+
60156
70+
120016
71+
147383
72+
68819
73+
83868
74+
81021
75+
131594
76+
137692
77+
86537
78+
110709
79+
127678
80+
106849
81+
137640
82+
108482
83+
131412
84+
70331
85+
90118
86+
117557
87+
117347
88+
84688
89+
108869
90+
145359
91+
127024
92+
100976
93+
90419
94+
53362
95+
106100
96+
129474
97+
56101
98+
99975
99+
79211
100+
99865
101+
121099
102+
74511
103+
123172
104+
"""
105+
106+
107+
let floatArray = inputString.split{ $0.isNewline }.compactMap{ Float($0) }
108+
//print(floatArray)
109+
let fuelArray = floatArray.map { module -> Float in
110+
var total: Float = 0.0
111+
var delta = module
112+
while delta > 0 {
113+
delta = floor(delta/3) - 2
114+
if delta > 0 {
115+
total += delta
116+
}
117+
}
118+
return total
119+
}
120+
//print(fuelArray)
121+
let total = fuelArray.reduce(0, +)
122+
print(total)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var program: [Int] = [1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,1,10,23,27,2,27,13,31,1,31,6,35,2,6,35,39,1,39,5,43,1,6,43,47,2,6,47,51,1,51,5,55,2,55,9,59,1,6,59,63,1,9,63,67,1,67,10,71,2,9,71,75,1,6,75,79,1,5,79,83,2,83,10,87,1,87,5,91,1,91,9,95,1,6,95,99,2,99,10,103,1,103,5,107,2,107,6,111,1,111,5,115,1,9,115,119,2,119,10,123,1,6,123,127,2,13,127,131,1,131,6,135,1,135,10,139,1,13,139,143,1,143,13,147,1,5,147,151,1,151,2,155,1,155,5,0,99,2,0,14,0]
2+
program[1] = 41
3+
program[2] = 12
4+
// var printProgram: [String] = program.map{String($0)}
5+
var position = 0
6+
while true {
7+
// print(position, program)
8+
var maybeoperation: ((Int, Int) -> Int)? = nil
9+
switch program[position] {
10+
case 1: maybeoperation = {$0+$1}
11+
case 2: maybeoperation = {$0*$1}
12+
case 99: print("halt"); break
13+
default: print("error"); break
14+
}
15+
guard let operation = maybeoperation else { break }
16+
let input1position = program[position+1]
17+
let input2position = program[position+2]
18+
let output = operation(program[input1position],program[input2position])
19+
let destination = program[position+3]
20+
// print(position,input1position,program[input1position],input2position,program[input2position],destination)
21+
program[destination] = output
22+
// printProgram[destination] = String(output) + "*"
23+
// printProgram[position] += "#"
24+
position += 4
25+
}
26+
print(program[0])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var count: Int = 0
2+
for num in 153517...630395 {
3+
let numString = String(num)
4+
guard String(numString.sorted()) == numString else { continue }
5+
var charCount: [Character: Int] = [:]
6+
7+
charCount = numString.reduce(into: charCount, { (result, element) in
8+
result[element] = (result[element] ?? 0) + 1
9+
})
10+
let hasExactDouble: Bool = charCount.contains(where: { (_, count) -> Bool in
11+
count == 2
12+
})
13+
guard hasExactDouble else { continue }
14+
15+
// guard Set(numString).count < 6 else {continue }
16+
print("Hit! \(num)")
17+
count += 1
18+
}
19+
print(count)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var count: Int = 0
2+
for num in 153517...630395 {
3+
let hasdouble = false
4+
let numString = String(num)
5+
guard String(numString.sorted()) == numString else { continue }
6+
let charCount = numString.reduce(into: [:], { result, element -> [Character:Int] in
7+
result[element] = result[element] ?? 0 + 1
8+
}
9+
guard charCount.contains(where: { _, count -> Bool in
10+
count == 2
11+
}) else { continue }
12+
13+
// guard Set(numString).count < 6 else {continue }
14+
print("Hit! \(num)")
15+
count += 1
16+
}
17+
print(count)

Diff for: Advent of Code.playground/contents.xcplayground

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='6.0' target-platform='ios'>
3+
<pages>
4+
<page name='Day 1'/>
5+
<page name='Day 2'/>
6+
<page name='Day 3'/>
7+
<page name='Day 4'/>
8+
</pages>
9+
</playground>

Diff for: Advent of Code.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Diff for: Day3.1.playground/Contents.swift

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
let wire1path = "R990,U408,L583,U275,R483,U684,R437,U828,R108,U709,R378,U97,R252,D248,R413,U750,R428,D545,R570,D795,L204,D975,L557,U160,L861,U106,R436,U934,R81,D237,R660,U704,L451,U135,R282,D391,R39,D109,R125,U918,R214,U481,R853,U825,L91,D763,R335,U868,R42,U218,R152,D429,R414,D607,R28,U436,R7,U770,L215,D373,R209,U440,L536,U120,R900,D46,R635,D75,R58,U267,L581,U474,L858,U172,R725,U54,R291,D274,L583,D743,L130,U563,R137,U524,R659,D997,R131,D364,R883,D222,R628,U579,R801,D890,L519,D749,L620,U60,L759,D759,R376,U769,L910,D570,L814,U954,L153,D42,L784,D66,L844,U29,L794,D342,L924,U825,R447,U828,R404,D52,L330,D876,R125,U203,R245,U936,R866,D804,L186,U693,L620,D722,L32,D735,L191,D217,R68,U209,L736,U365,R280,U608,L450,D240,L282,U434,R589,U94,R470,D5,R49,U407,R552,D651,L69,U518,L358,D130,L710,D929,L315,U345,L511,D229,L557,U44,L890,D702,L181,D61,L208,U553,R878,U354,R787,U624,L961,D92,L891,U70,R203,U255,R532,U154,R299,U934,L609,D985,R115,U757,L13,D368,R936,D742,L412,U346,R56,D67,R371,D175,R868,U107,R806,D530,L40,U153,R374,D223,R517,D481,L194,U545,L356,U906,L999,D885,R967,U407,L141,U927,L489,U959,L992,U638,R332,U51,R256,U901,L891,U803,L885,U804,L242,U180,R277,U693,R935,D253,L68,D153,L614,D596,L999,D633,R995,D803,R17,U303,L569,U231,R737,D970,L45,D860,L225,D65,R41,D313,R698,D340,R599,D531,R55,D568,L911,D547,R196,D228,R868,D227,R262,U525,R104,D625,R570,U968,L276,D586,R690,D73,L336,U287,R294,U148,R781,D395,R478,D804,L429,U872,L351,D910,L597,U726,L320,D964,R928,U2,R540,D325,L222"
2+
let wire2path = "L998,U662,R342,U104,R140,U92,R67,D102,L225,U265,R641,U592,L295,D77,R415,U908,L640,D381,R312,U44,R424,D847,R892,D625,L337,D344,L917,D914,R127,D273,L627,U812,L200,D262,R226,U273,R911,U597,L888,U28,R921,U464,R254,U771,R818,D808,L239,D225,L280,U785,R322,D831,L622,U506,R139,U12,L491,D572,L172,U685,R54,U747,L812,D717,R874,U428,L867,U174,R360,D36,R217,D539,R210,D791,L82,D665,L190,D313,R649,U849,R63,U385,R105,U806,L207,U697,L823,D272,R830,D952,L386,U987,R775,U517,R139,D756,R545,D973,L743,D286,R261,U448,R946,U884,L903,D142,R28,D374,R259,U403,R689,D245,L302,D134,R710,U762,L67,D561,R801,D140,L887,U346,L227,U682,L350,D218,L711,U755,R226,D277,R114,D61,R992,U602,L191,U640,R733,D329,R862,U242,R754,D161,L52,D974,L251,D444,L552,U977,R174,U483,R869,D955,R925,U693,R610,D353,L843,U148,L866,D167,R412,D31,L847,D979,L282,D797,L837,U473,L402,U193,L332,D603,R48,D589,L760,D673,L843,U428,R779,D592,L688,D141,R851,D642,R559,U939,R999,D64,L297,U817,R670,U322,L768,D936,L39,U95,L342,U849,L692,U714,L732,D734,L373,U66,L577,D453,R336,U760,L217,U542,R920,U24,R529,D594,L34,D79,R877,D965,R932,U460,R879,U26,R803,U876,L780,U956,L235,D270,L315,D577,R835,U750,R414,D584,L828,U335,L563,U238,L815,U780,L550,U18,R743,D54,L816,U344,L806,D197,L518,D682,L835,U255,L666,U442,L286,D543,R102,D52,L570,D787,L763,D223,R279,D892,L828,D111,L554,D452,R575,D299,R932,D187,L439,U616,L278,D701,L360,D524,L891,U953,L896,U788,R776,U782,L71,D741,L652,U121,R669,D809,L662,U319,R392,D313,R870,U794,R937,D469,R571,D761,R947"
3+
4+
struct Point {let x, y: Int}
5+
6+
extension Point: Hashable {
7+
}
8+
9+
extension Point {
10+
public static func + (lhs: Point, rhs: Point) -> Point {
11+
let x = lhs.x + rhs.x
12+
let y = lhs.y + rhs.y
13+
return Point(x: x, y: y)
14+
}
15+
}
16+
17+
extension Point: CustomStringConvertible {
18+
var description: String {
19+
return "(\(x), \(y))"
20+
}
21+
}
22+
23+
extension String {
24+
func toArray() -> [Substring] {
25+
return self.split{$0==","}
26+
}
27+
}
28+
29+
enum Direction: String {
30+
case U, D, L, R
31+
32+
var delta: Point {
33+
switch self {
34+
case .U: return Point(x: 1, y: 0)
35+
case .D: return Point(x: -1, y: 0)
36+
case .L: return Point(x: 0, y: -1)
37+
case .R: return Point(x: 0, y: 1)
38+
}
39+
}
40+
}
41+
42+
struct Vector {
43+
let direction: Direction
44+
let distance: Int
45+
}
46+
47+
extension Array where Element == Substring {
48+
func toVectors() -> [Vector] {
49+
self.map { string in
50+
let dir = Direction(rawValue: String(string.first!))!
51+
let dist = Int(string.dropFirst())!
52+
return Vector(direction: dir, distance: dist)
53+
}
54+
}
55+
}
56+
57+
let path1 = wire1path.toArray().toVectors()
58+
let path2 = wire2path.toArray().toVectors()
59+
60+
var points1: Set<Point> = []
61+
var currentPoint = Point(x: 0, y: 0)
62+
for vector in path1 {
63+
var remainingDist = vector.distance
64+
while remainingDist > 0 {
65+
currentPoint = currentPoint + vector.direction.delta
66+
points1.insert(currentPoint)
67+
remainingDist-=1
68+
}
69+
}
70+
// var collisions: Set<Point> = []
71+
var closestCollision: Int = 99999
72+
currentPoint = Point(x: 0, y: 0)
73+
for vector in path2 {
74+
var remainingDist = vector.distance
75+
while remainingDist > 0 {
76+
currentPoint = currentPoint + vector.direction.delta
77+
if points1.contains(currentPoint) {
78+
// collisions.insert(currentPoint)
79+
let distance = currentPoint.x + currentPoint.y
80+
print("Hit at \(currentPoint), distance: \(distance)")
81+
closestCollision = min(closestCollision, distance)
82+
}
83+
remainingDist-=1
84+
}
85+
}
86+
print(closestCollision)

Diff for: Day3.1.playground/contents.xcplayground

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><playground version="5.0" target-platform="macos" executeOnSourceChanges="true"> <timeline fileName="timeline.xctimeline"/></playground>

Diff for: Day3.1.playground/playground.xcworkspace/contents.xcworkspacedata

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)