Skip to content

Commit 0353971

Browse files
committed
day5 1
1 parent b8c4ecb commit 0353971

File tree

6 files changed

+170
-63
lines changed

6 files changed

+170
-63
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Below is my current advancement in Advent of code:
1111
| Day 1 | :star: | :star: |
1212
| Day 2 | :star: | :star: |
1313
| Day 3 | :star: | :star: |
14-
| Day 4 | | |
15-
| Day 5 | | |
14+
| Day 4 | :star: | :star: |
15+
| Day 5 | :star: (22min) | |
1616
| Day 6 | | |
1717
| Day 7 | | |
1818
| Day 8 | | |

Sources/AdventOfCode.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ let allChallenges: [any AdventDay] = [
66
Day02(),
77
Day03(),
88
Day04(),
9+
Day05(),
910
]
1011

1112
@main

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 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-
}
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+
}
6360
}
61+
}
6462
}
65-
return count
66-
}
67-
68-
func part2() -> Int {
69-
countAllWhere(letter: "A", predicate: isMAS)
63+
}
7064
}
65+
return count
66+
}
67+
68+
func part2() -> Int {
69+
countAllWhere(letter: "A", predicate: isMAS)
70+
}
7171
}

Sources/Day05.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Algorithms
2+
3+
struct Rule {
4+
var before: Int
5+
var after: Int
6+
7+
}
8+
extension Rule {
9+
init(data: Substring) {
10+
let parts = data.split(separator: "|")
11+
before = Int(parts[0]) ?? 0
12+
after = Int(parts[1]) ?? 0
13+
}
14+
}
15+
16+
struct Day05: AdventDay {
17+
var rules: [Int: [Int]]
18+
var updates: [[Int]]
19+
20+
init(data: String) {
21+
let parts = data.split(separator: "\n\n")
22+
rules = [Int: [Int]]()
23+
for line in parts[0].lines() {
24+
let parts = line.split(separator: "|")
25+
let before = Int(parts[0]) ?? 0
26+
let after = Int(parts[1]) ?? 0
27+
rules[after, default: [Int]()].append(before)
28+
}
29+
updates = parts[1].lines().map { $0.integers(separator: ",") }
30+
}
31+
32+
func part1() -> Int {
33+
updates.filter { update in
34+
for (idx, page) in update.enumerated() {
35+
let rulesForPage = rules[page, default: [Int]()]
36+
let brokenRules = rulesForPage.map { update[idx + 1..<update.count].contains($0) }.count {
37+
$0 == true
38+
}
39+
if brokenRules > 0 {
40+
return false
41+
}
42+
}
43+
return true
44+
}.map { update in update[update.count / 2] }.sum
45+
}
46+
47+
func part2() -> Int {
48+
0
49+
}
50+
}

Sources/Helpers/Grid.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ 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-
}
28+
29+
func toText() -> String {
30+
String(
31+
self.raw.map {
32+
String($0)
33+
}.joined(by: "\n"))
34+
}
3435

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

Tests/Day05.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Testing
2+
3+
@testable import aoc
4+
5+
@Suite("Day05")
6+
struct Day05Tests {
7+
let testData = """
8+
47|53
9+
97|13
10+
97|61
11+
97|47
12+
75|29
13+
61|13
14+
75|53
15+
29|13
16+
97|29
17+
53|29
18+
61|53
19+
97|53
20+
61|29
21+
47|13
22+
75|47
23+
97|75
24+
47|61
25+
75|61
26+
47|29
27+
75|13
28+
53|13
29+
30+
75,47,61,53,29
31+
97,61,53,29,13
32+
75,29,13
33+
75,97,47,61,53
34+
61,13,29
35+
97,13,75,29,47
36+
"""
37+
38+
@Test("parsing")
39+
func testParsing() async throws {
40+
let challenge = Day05(data: testData)
41+
#expect(challenge.updates.count == 6)
42+
}
43+
44+
@Test("part1")
45+
func testPart1() async throws {
46+
let challenge = Day05(data: testData)
47+
#expect(challenge.part1() == 143)
48+
}
49+
50+
@Test("part2")
51+
func testPart2() async throws {
52+
let challenge = Day05(data: testData)
53+
#expect(challenge.part2() == 0)
54+
}
55+
}

0 commit comments

Comments
 (0)