Skip to content

Instantly share code, notes, and snippets.

@jakcharvat
Created November 23, 2021 14:22
Show Gist options
  • Save jakcharvat/6252cd21e72edfdf0047645b08bcd6ff to your computer and use it in GitHub Desktop.
Save jakcharvat/6252cd21e72edfdf0047645b08bcd6ff to your computer and use it in GitHub Desktop.
//
// WordSearch.swift
// progtest
//
// Created by Jakub Charvat on 22.11.2021.
//
import Foundation
@main
struct WordSearch {
static func main() {
print("Zadej osmismerku ukoncenou prazdnym radkem:")
let wordSearch = readInput()
print("Zadej hledane slovo:")
guard let search = readSearch() else { return }
print("Hledam...")
let directions = [
Coord(x: -1, y: 1),
Coord(x: 0, y: 1),
Coord(x: 1, y: 1),
Coord(x: -1, y: 0),
Coord(x: 1, y: 0),
Coord(x: -1, y: -1),
Coord(x: 0, y: -1),
Coord(x: 1, y: -1),
]
var results = [Result]()
for step in directions {
for (y, line) in wordSearch.enumerated() {
for (x, _) in line.enumerated() {
let startCoord = Coord(x: x, y: y)
let endCoord = startCoord + ((search.count - 1) * step)
if !(endCoord.x >= 0 && endCoord.x < line.count &&
endCoord.y >= 0 && endCoord.y < wordSearch.count) {
continue
}
var found = true
for i in 0..<(search.count) {
let coord = startCoord + (i * step)
if (wordSearch[coord.y][coord.x] != search[i]) {
found = false
break
}
}
if found {
results.append(Result(start: startCoord, direction: step, word: search))
}
}
}
}
printResults(results, in: wordSearch)
}
static func readInput() -> [[Character]] {
var lines = [[Character]]()
while true {
guard let line = readLine(),
!line.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { break }
lines.append(Array(line))
}
return lines
}
static func readSearch() -> [Character]? {
if let input = readLine()?.trimmingCharacters(in: .whitespacesAndNewlines) {
return Array(input)
}
return nil
}
static func printResults(_ results: [Result], in wordSearch: [[Character]]) {
for (y, line) in wordSearch.enumerated() {
var printLine = ""
for (x, char) in line.enumerated() {
let coord = Coord(x: x, y: y)
let emphasize = results.some { result in
result.shouldEmphasize(coord)
}
if emphasize {
printLine += "\u{001B}[1;7;31m"
}
printLine += String(char)
if emphasize {
printLine += "\u{001B}[0m"
}
}
print(printLine)
}
}
struct Coord: Equatable {
let x: Int
let y: Int
static func + (lhs: Coord, rhs: Coord) -> Coord {
return Coord(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
}
static func - (lhs: Coord, rhs: Coord) -> Coord {
return Coord(x: lhs.x - rhs.x, y: lhs.y - rhs.y)
}
static func * (lhs: Int, rhs: Coord) -> Coord {
return Coord(x: lhs * rhs.x, y: lhs * rhs.y)
}
var asDirection: Coord {
if (x == 0) {
return Coord(x: x, y: y / (abs(y)))
}
if (y == 0) {
return Coord(x: x / (abs(x)), y: y)
}
if (x == y || x == -y) {
return Coord(x: x / (abs(x)), y: y / (abs(y)))
}
return self
}
static var zero: Coord { Coord(x: 0, y: 0) }
static func == (lhs: Coord, rhs: Coord) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
}
struct Result {
init(start: WordSearch.Coord, direction: WordSearch.Coord, word: [Character]) {
self.start = start
self.direction = direction
self.word = word
}
let start: Coord
let direction: Coord
let word: [Character]
var idx: Int = 0
func shouldEmphasize(_ coord: Coord) -> Bool {
let diff = coord - start
let inRange = max(abs(diff.x), abs(diff.y)) < word.count
return diff == .zero || (diff.asDirection == direction && inRange)
}
}
}
extension Array {
func some(predicate: (Element) -> Bool) -> Bool {
for element in self {
if predicate(element) {
return true
}
}
return false
}
}
/*
cwaqmsqtrtivcoegeqraskeeawkujcvuiqfqqbkuaqmpouuwhyshiixljrtl
qdummqhbjdklnfkgfjaxffngrhridhiygsvxtibhqlviefaolrjlqvifphso
woneatlxgnmqurlgdpfgahavrrbmpctcfefcjllgxmrgjsqkpcebifcoeveu
qsvghsabfkevgmsmfqlxffsqlxxssingeuukkimcbgivygigjloqusoxovro
hydxoikigwtovjucnsjqjluadrmvycowtafinmhovdjxuyextvegtejeouqq
dqhewkjbdrgmnmdeamwciavsrmqndhqxwbnkfjqqqsrastmxbilrgogmmrly
jnuunhleggxocerjjbvliahhbhvrxarafcfuqyxabnfgtwpshjmmlqgyjcpr
diljhewfadraopicaujwkuflhaildelerwrnrisybgmhsjdpkuctruoaebno
xrutpidvactexqgtxvpjdpkbxvgaqkdpdksdpduheeacyhsompjejbishgog
mqjoyfjrptqddsnvldcksmrjoholxemyehaxkdkuoltcorilyyynspgxbtly
ckukqqtbexcnrnxjpxxgnyybauotwsmwcrxuqikdklthhgqjetpdjdvhqaut
dlrukpufcmotsosscqkfsjnybvcwmjjolkjqfgmgjnrutneeafsjumfvrnau
jewpgouxytpnrsjngdwwpfysybhogrqbqmcjbsjavovsqqskfmhtpamoflmx
crtkxdctflobroaskxixcxjisacviaqdlnytaebjppicrpsdivgavvhvvwyh
jrehgklpvtcbiswdfepegrbpvnglaygkyybdxjswyhchenjgqpmycvpvwasb
tjjpfniwiflfueunaiqsjfcqfmtybeyyvyshiaaabiqmlqqjynutjsdponwm
ngawsauopwjkkhbpogogoofahatkdoxqiafboppdpnwpxadxfgmnopayaglu
njebbpbfdqisltnyxlgofeccwgepnbsmsjjwkeeawtlbrcmhunbysbyiggno
iqvsvacqeitjyxjmapdtiovfniqwxgahuxppjfvaalubvcltojfoesggfrrc
gjcflgurgxlhsmlcetittjtutofgtvmcbliyxircgjoddesslvwyfvwnxbrb
foccskvlqibqtkotqtvbklryrhdrjngqxfmtkqhxdonotinclcpeoirgpfcf
sscdsykjmosvluuvcugcskuvdqqmwxodkkwlrcevdeyhaatrqobklovblicd
seebuobasuaolmegajfrkrmjausayvigfhhdsbpupgfblxvedctfyyrcuprr
eoutvutssadjtfvstdhspncogxfopaejyikuqssraqrlasiggqbxqtrttlfu
jaynmuujldiyrmktppeyxuybsxuwjsiqfmcemwrbahxubkghyrwrfrlotpst
mdvrxiwnxhhwjbfayjfiqblatnpvxjluwbydfnjxxnfnjmyjmxsbxdyqosan
mhenpanbtppdyggflpdqwyxbyetldsrlhxapwhaymrjnhehfokxrpccbqeon
onpneyvwkipovlfjoxfxqgcqbxofxdmrryvbboxhtxyktwmhmkkkyojejbgf
ajuvcwjccegwhpxtubdpalvyhfmgqyysyuwuyuffkcmueeuatpvkehqxfkhu
tssjrmlqmauqqsmdcqvufbcltmalhiojdbdrunapiysrcbttogoexrkmbswk
mwaddcgspdgxosqliguscdeudeeixeblnftiamkgalumgbfuimshnxplurrs
bqtfwvmgdiwpfftncanrkrqrdkpwloxdipeqgoybfvacuiisvtvkjtvexxjk
yijuvupbdijfjmpqjckyfbflpsgpqlqvequlvmsgkcajijlqjnvyeypuibtg
akqiopusrrvjaetsflnvakubobbdxcgadpjtgyumoiljlwtqemeypjadnceh
tieokwrqmnlllpirrfyaxnbeqtvambmomqsfvdjmlbffnbobxobmwifuqlpj
sqkirkngtdhfpssmvefhxxvugvvapeayysoqjnakbthsyjuiaxiqgvbutfoq
gihrreoyyiatkqarnxdmxuhcbitwhxjttcrlulvfjsbrcqvbyapgquotyedd
jgwoqrijoejygnbcxdjxrrikxbtwwfbpayxdvjbowurwmeqwafimceqnghxs
xumtxpejtmsexpqqebrqkgyyetvexdtshlwttxedgjgxltqbauvqrupovvav
xfdxjtdpdcsxkvfdmmsimeyvimmrycykdxpppvytjmdtadbxgtdsxhnxwvvg
trfalkpxtfylwhbruowgupmaynfkmbeldqnimglofnyapekybhvqggvvnrkf
ikyndjjfxgwcfeiojrelooaccowdxwamgoqvrdscdaeljxilhchfdjelimrq
ipyrxqtugwmwsjvjqgnrucmdnoumisyyptxlbqlenmfnbetwijvjraxvbcao
mhuoimacnvgcnlrgmdjoxjgogcqbdpfxwxcqrpdppgsfaiqxodltahabyhob
kndxbnubkcjdvrhuihvpunjaqsrytfdakqirvfgnpwsydsaldlxljdesniwe
altxcpoyljeayipdlgovxlwpovsvfxglirvkdagybnhnvdtsljcynwqiawoh
flmhkycttksbwejyojgybpvhlwwtcwissebpjidxuixwgdvdhbwrfpjufgao
nlxismtkmmtokqfimifexxfxpqdswmcloveelpjbdynrfnloflvwjnkyrhxp
sytpywnipqbaqvlcsklqpolnotiagwnsxjhwpfxcoismoaouxmyhflcdnwqu
oxqwknbhvmsamauorvhqvoglnmbpwlptmfctjqgnmpcrsueypdvjocyfqnhe
lwxgqqftvsimpuwjhpddbfuoiraqebkxloiexxvgjcpdrnxyjkccooorrdnd
furpolcrrfgkcxybuvlgvcodgdawfpggetduawfryiydnewywlnamxytsbqe
aebqxcwvdnwqgtcjtgcrdnexaginwgswpvqbiytvcsjwpwvjqetgfekcycys
kymkexpnspuqcmwwpiflckodaeocdjkivjsaiwplwdpaknxxhketfhxemyag
rgybcosclppncjwbthiscypqlfhgnocydmjygmmpbjypnglwhoqeqgexocdb
xewfmkntwdwsvbwybionbilllbuorwuxgdvqdtnjurlctgkbewcbbowxdvxj
dhjtpdohidsfcvgclhwgjafdtgcjrqqrjkcmpgbadhmyunvmrmjdqhnkgctj
igbiwqutlwhtxamuhpfkwefacjwkdegsdjsjfvlqmvnjqauenspwxjyxahjs
cqglvsqfdfwniwnikfxakiqhjqevcxrysfjvlkwqtocusqrusbdukhgkjsjt
ucquqaldluidpsuwogballapghsnlvchfcrsrykjhqbtqxkgnxjoahsfnnuj
qfbcltmalhiojdkdruna
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment