From f5d9d75eb7c9528fedbacce3761c02ad1c2646f5 Mon Sep 17 00:00:00 2001 From: Dory Date: Sat, 14 Dec 2024 11:37:57 -0800 Subject: [PATCH] d10 --- day10/d10p1.swift | 49 ++++++++++++++++++++++++++++++++++++++++++++ day10/d10p2.swift | 46 +++++++++++++++++++++++++++++++++++++++++ day10/input.txt | 52 +++++++++++++++++++++++++++++++++++++++++++++++ day10/test.txt | 8 ++++++++ day10/test0.txt | 4 ++++ 5 files changed, 159 insertions(+) create mode 100644 day10/d10p1.swift create mode 100644 day10/d10p2.swift create mode 100644 day10/input.txt create mode 100644 day10/test.txt create mode 100644 day10/test0.txt diff --git a/day10/d10p1.swift b/day10/d10p1.swift new file mode 100644 index 0000000..5b55367 --- /dev/null +++ b/day10/d10p1.swift @@ -0,0 +1,49 @@ +import Foundation + +struct Coord : Hashable, CustomStringConvertible { + let x: Int + let y: Int + var description: String { return "(\(x), \(y))" } + func neighbors() -> [Coord] { + return [ + Coord(x: x-1, y: y), Coord(x: x+1, y: y), + Coord(x: x, y: y-1), Coord(x: x, y: y+1) + ] + } + func valid(xLimit: Int, yLimit: Int) -> Bool { + return x >= 0 && y >= 0 && x < xLimit && y < yLimit + } +} + +func readInput(_ filePath: String) throws -> ([Int:Set], Int, Int) { + let content = try String(contentsOfFile: filePath, encoding: .ascii) + let lines = content.split(separator: "\n").map(Array.init) + let map = lines.map { $0.map(String.init).map { Int($0)! } } + var topo: [Int:Set] = [:] + map.enumerated().forEach { i, row in + row.enumerated().forEach { j, h in + topo[h] = (topo[h] ?? []).union([Coord(x: i, y: j)]) + } + } + return (topo, lines.count, lines[0].count) +} + +let (topo, height, width) = try readInput(CommandLine.arguments[1]) +var answer: Int = 0 +//topo.forEach { k, v in print("\(k): \(v)") } + +for start in topo[0]! { + var froms: Set = [start] + for altitude in 1...9 { + var tos: Set = [] + for from in froms { + from.neighbors() + .filter { c in c.valid(xLimit: height, yLimit: width) } + .filter { c in topo[altitude]!.contains(c) } + .forEach { c in tos.insert(c) } + } + froms = tos + } + answer += froms.count +} +print(answer) diff --git a/day10/d10p2.swift b/day10/d10p2.swift new file mode 100644 index 0000000..7d5e9e9 --- /dev/null +++ b/day10/d10p2.swift @@ -0,0 +1,46 @@ +import Foundation + +struct Coord : Hashable, CustomStringConvertible { + let x: Int + let y: Int + var description: String { return "(\(x), \(y))" } + func neighbors() -> [Coord] { + return [ + Coord(x: x-1, y: y), Coord(x: x+1, y: y), + Coord(x: x, y: y-1), Coord(x: x, y: y+1) + ] + } +} + +func readInput(_ filePath: String) throws -> ([Int:Set], Int, Int) { + let content = try String(contentsOfFile: filePath, encoding: .ascii) + let lines = content.split(separator: "\n").map(Array.init) + let map = lines.map { $0.map(String.init).map { Int($0)! } } + var topo: [Int:Set] = [:] + map.enumerated().forEach { i, row in + row.enumerated().forEach { j, h in + topo[h] = (topo[h] ?? []).union([Coord(x: i, y: j)]) + } + } + return (topo, lines.count, lines[0].count) +} + +let (topo, height, width) = try readInput(CommandLine.arguments[1]) +topo.forEach { k, v in print("\(k): \(v)") } + +var currentLayer: [Coord:Int] = Dictionary( + uniqueKeysWithValues: topo[0]!.map { coord in (coord, 1) } +) +print("\n0: \(currentLayer)") +for alt in 1...9 { + var nextLayer: [Coord:Int] = [:] + for (from, score) in currentLayer { + from.neighbors() + .filter { c in c.x >= 0 && c.y >= 0 && c.x < height && c.y < width } + .filter { c in topo[alt]!.contains(c) } + .forEach { c in nextLayer[c] = (nextLayer[c] ?? 0) + score } + } + currentLayer = nextLayer + print("\(alt): \(currentLayer)") +} +print(currentLayer.values.reduce(0) { s, score in s + score }) diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000..ff85d75 --- /dev/null +++ b/day10/input.txt @@ -0,0 +1,52 @@ +0145677654325490845691012345621876560340100123012398 +1238988998216781932782121234010983401259230034563387 +4323589867205432801543210945125672378768341459654456 +1014678454102301765456901876034561769897652368776301 +6765521543201512350307872962120120850785677879789210 +0896430439810487461217965871298438941454980968654321 +1765012126723596543478014560167569032363211457256762 +2872121035434654302569123441455678121071102340145895 +3961230345985783211098710332334599432980098743232434 +4550145456676890100167000146721087641001217650141325 +9649056765489910651254121035890014550732309654210016 +8738769894676328762343236124301123669845498763227807 +7129787923565439651456347833210008778996787120156998 +1013496014567010340987656944782119211087898031343210 +1012345002198321232876501855693024302332196541034101 +1234567123023450901965432761054435678445087670123256 +0144898454910767845670301622169546589556764581214347 +4343732367867893034981243213678696765698873294309838 +1234351078754702121893214504589787894780987101456789 +0943765469843213210721303698921009683211074560167678 +7856874348765434785630410787652118701202983076298987 +6765989219210125698540521236543025654343892189347567 +5667867804321087582101638347432134509856782345456498 +1054876965691296443492789858901213212790101276012387 +2123965476780125356783498767654300103685230983401456 +3278954587821034219870567658967876234576541092560845 +2567543296930761006721498743478965987655612451076921 +1056230145945852345012345412661234554567803367987830 +2340167034876945123211296401780109693069954298756101 +7887658123985231034500787345691218782178769110343232 +6992349032100112985011216217885011071078978021232349 +5801239844301101676720105606976522362567987876541458 +4321023765432432369838234745983439453454376965430167 +3087610321396565458949549836112378321043105302301298 +2198565410187076327658678921001065439652234211017657 +3233478903216189014547664567632104508701230322928943 +4542369894103273223014523498549812012349821498834012 +7651423765764784132123210210038763676256734567765423 +8960314349845695041012396342121054985109875498656701 +4871005256736786780169487653434873014018766787567892 +5654196149821677893278565694565963223321051096450943 +6743287032120563034567684787696954101438142345321056 +7899180129061432125675893256787845698589233239885469 +3458098938778743210986710143876034787670132178596378 +2167347845609654331235430782932128236101056017687267 +6056256741012103220543521691047659145692347012570167 +7890165432343210110652434598798578036785498743456898 +6784567876758701328701223123623457629876901234347107 +5413218965869232499899810034510256510267892301298256 +4303409954978149581234745218760105100126765410789340 +3212567823019058670365634309451234981237898323870121 +4321016012108767621256565678321015676546767654965432 diff --git a/day10/test.txt b/day10/test.txt new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/day10/test.txt @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 diff --git a/day10/test0.txt b/day10/test0.txt new file mode 100644 index 0000000..a305b9d --- /dev/null +++ b/day10/test0.txt @@ -0,0 +1,4 @@ +0123 +1234 +8765 +9876