Compare commits

...

15 Commits

Author SHA1 Message Date
efim 1afaff0021 day18, example second parallel 2023-12-18 20:33:04 +00:00
efim 568fdd9a70 day18, example for part 2 2023-12-18 19:46:38 +00:00
efim 6dabe8bc66 day18, another example working 2023-12-18 19:37:17 +00:00
efim 86c9ad7653 day18, struggling 2023-12-18 14:50:51 +00:00
efim 4fc5caf228 day18: example optimized even more 2023-12-18 12:10:22 +00:00
efim b831e92e1f day18: example optimized 2023-12-18 11:41:55 +00:00
efim d799b122ce day18: reading instructions & hanging 2023-12-18 10:48:27 +00:00
efim 1d027d57fc day18, part1 2023-12-18 10:30:40 +00:00
efim 49fc57029f day18, example 2023-12-18 09:41:22 +00:00
efim 955bdc78c1 day17, example2 2023-12-17 13:58:34 +00:00
efim a7e06e7a6e day17, part 1 wow. 2023-12-17 13:29:22 +00:00
efim abca885f20 day17, so many tries for part 1 2023-12-17 13:28:18 +00:00
efim 81b8ddc8b0 day17, example attempt 2 2023-12-17 11:18:48 +00:00
efim 41e32d405b day17, example 2023-12-17 10:48:51 +00:00
efim 08c20ea6e0 day17, dijkstra doesn't work
right? just because if found a shorter path, doesn't mean i don't need
to check other directions.
i have, because maybe they are not blocked and will provide better
solution.
so, yikes
2023-12-17 09:22:31 +00:00
11 changed files with 1776 additions and 3 deletions

321
day17/clumsyCrucible.go Normal file
View File

@ -0,0 +1,321 @@
package day17
import (
"fmt"
"log"
"math"
"os"
"slices"
"strings"
)
func Run() int {
fmt.Println("hello from day 17")
filename := "day17/input"
field := NewField(filename)
log.Printf("%+v\n", field)
field.RunDijkstra()
lenToEnd := field.Paths[field.Finish].totalLength
fmt.Println("check visually:")
// fmt.Println(field.Paths[end].stringPathSoFar)
fmt.Println(field.Paths[field.Finish].stringPathSoFar)
return lenToEnd
}
// let's do dijkstra. it also needs a priority queue
// priority queue would be over vertice. and would have to have enough information to
// calc the distance from neighbors.
// how to check condition of max 3 in one row?
// with each vertice store [horizontal:n|vertical:n] and if it's 3 just dont consider?
// so in iteration, i have some vertice, with horizontal:2 for example,
// i check all neighbors, if path through 'this' is shorter, set that as path,
// but also mark the path with len of straight.
//
// so priority queue is with 'path to next'
// or rather 'path to i,j'
// then check for neighbors (non finished), calc distance to them through this
// checking neighbors via 'path get directions' 'path get geighbors from directions'
// if shorter - update
// mark current as 'finished'
// so, i'll be checking cost to enter directly from this table,
// but check path len
func ReadEnterCosts(filename string) [][]int {
bytes, err := os.ReadFile(filename)
if err != nil {
panic(fmt.Sprint("error reading file ", filename))
}
text := strings.TrimSpace(string(bytes))
result := make([][]int, 0)
for _, line := range strings.Split(text, "\n") {
numbers := make([]int, 0)
for _, digit := range line {
num := int(digit - '0')
numbers = append(numbers, num)
}
result = append(result, numbers)
}
return result
}
type Coord struct {
Row, Col int
}
func (c Coord) applyDirection(d Direction) (result Coord) {
result = c
switch d {
case Upward:
result.Row -= 1
case Downward:
result.Row += 1
case Leftward:
result.Col -= 1
case Rightward:
result.Col += 1
}
return
}
func (c Coord)String() string {
return fmt.Sprintf("(%d,%d)", c.Row, c.Col)
}
type Direction int
const (
Upward Direction = iota
Downward
Leftward
Rightward
)
func (d Direction) String() string {
strings := []string{"Up", "Down", "Left", "Right"}
return strings[d]
}
func (d Direction) AsSymbol() string {
strings := []string{"^", "v", "<", ">"}
return strings[d]
}
func (d Direction) GetPerpendicular() (directions []Direction) {
switch d {
case Upward:
directions = []Direction{Leftward, Rightward}
case Downward:
directions = []Direction{Leftward, Rightward}
case Leftward:
directions = []Direction{Upward, Downward}
case Rightward:
directions = []Direction{Upward, Downward}
}
return
}
type PathSegmentEnd struct {
endsAt Coord
totalLength int
lastSteps map[Direction]int
lastDirection Direction
stringPathSoFar string
done bool
}
func (p *PathSegmentEnd) NextDirections2() (next []Direction) {
// last steps of 2 is max allowed 3 tiles in row
lastSteps := p.lastSteps[p.lastDirection]
if lastSteps < 4 {
return []Direction{p.lastDirection}
}
next = append(next, p.lastDirection.GetPerpendicular()...)
if lastSteps < 10 {
next = append(next, p.lastDirection)
}
// log.Printf("getting directions from %+v they are %+v", p, next)
return
}
func (p *PathSegmentEnd) NextDirections() (next []Direction) {
next = append(next, p.lastDirection.GetPerpendicular()...)
// last steps of 2 is max allowed 3 tiles in row
lastSteps := p.lastSteps[p.lastDirection]
if lastSteps < 3 {
next = append(next, p.lastDirection)
}
// log.Printf("getting directions from %+v they are %+v", p, next)
return
}
type Field struct {
Paths map[Coord]*PathSegmentEnd
Costs [][]int
Height, Width int
Start Coord
Finish Coord
}
func NewField(filename string) Field {
enterCosts := ReadEnterCosts(filename)
startSegment := PathSegmentEnd{
endsAt: Coord{0, 0},
totalLength: 0,
lastSteps: make(map[Direction]int),
done: true,
lastDirection: Downward, // fake, need to init direct neighbors also
}
initialPaths := make(map[Coord]*PathSegmentEnd)
initialPaths[Coord{0, 0}] = &startSegment
height := len(enterCosts)
width := len(enterCosts[0])
return Field{
Paths: initialPaths,
Costs: enterCosts,
Height: height,
Width: width,
Start: Coord{0, 0},
Finish: Coord{height - 1, width - 1},
}
}
func (f *Field) isValid(c Coord) bool {
return c.Col >= 0 && c.Row >= 0 && c.Row < f.Height && c.Col < f.Width
}
// presupposes that direction is valid
func (f *Field) continuePathInDirection(curPath PathSegmentEnd, d Direction) (result PathSegmentEnd) {
// curPath := f.Paths[from]
from := curPath.endsAt
nextCoord := from.applyDirection(d)
moveCost := f.Costs[nextCoord.Row][nextCoord.Col]
newCost := curPath.totalLength + moveCost
lastSteps := make(map[Direction]int)
curPathStepsIntoThisDirection, found := curPath.lastSteps[d]
if !found {
lastSteps[d] = 1
} else {
lastSteps[d] = curPathStepsIntoThisDirection + 1
}
return PathSegmentEnd{
endsAt: nextCoord,
totalLength: newCost,
lastDirection: d,
lastSteps: lastSteps,
stringPathSoFar: curPath.stringPathSoFar + d.AsSymbol(),
}
}
func (p *PathSegmentEnd)StringKey() string {
return fmt.Sprintf("%s from %s with len %+v", p.endsAt.String(), p.lastDirection, p.lastSteps)
}
func (f *Field) RunDijkstra() {
checking := make([]PathSegmentEnd, 0)
distancesMap := make(map[string]int, 0)
startingPath := f.Paths[f.Start]
anotherStartingPath := PathSegmentEnd{
endsAt: Coord{0, 0},
totalLength: 0,
lastSteps: make(map[Direction]int),
done: true,
lastDirection: Rightward, // fake, need to init direct neighbors also
stringPathSoFar: ".",
}
checking = append(checking, *startingPath, anotherStartingPath)
distancesMap[startingPath.StringKey()] = 0
distancesMap[anotherStartingPath.StringKey()] = 0
for len(checking) > 0 {
var currentPath PathSegmentEnd
selectingMinDistanceOfVisited := math.MaxInt
for _, path := range checking {
if path.totalLength < selectingMinDistanceOfVisited {
currentPath = path
selectingMinDistanceOfVisited = path.totalLength
}
}
currentCoord := currentPath.endsAt
directions := currentPath.NextDirections2()
// fmt.Printf("> one more iteration for %+v ; directions will check %+v\n", currentPath, directions)
for _, direction := range directions {
neighborCoord := currentCoord.applyDirection(direction)
if !f.isValid(neighborCoord) {
continue // prevent going off the grid
}
// fmt.Printf("from %+v will examine in direction %s to %+v %+v\n", currentCoord, direction, neighborCoord, currentPath)
neighborPathSoFar, found := f.Paths[neighborCoord]
if !found {
neighborPathSoFar = &PathSegmentEnd{
totalLength: math.MaxInt,
}
f.Paths[neighborCoord] = neighborPathSoFar
}
pathIfWeGoFromCurrent := f.continuePathInDirection(currentPath, direction)
if pathIfWeGoFromCurrent.endsAt == f.Finish {
if pathIfWeGoFromCurrent.lastSteps[pathIfWeGoFromCurrent.lastDirection] < 4 {
continue
}
}
distFromThatSide, isKnown := distancesMap[pathIfWeGoFromCurrent.StringKey()]
if !isKnown {
distancesMap[pathIfWeGoFromCurrent.StringKey()] = pathIfWeGoFromCurrent.totalLength
// log.Printf("not known for %s \n", pathIfWeGoFromCurrent.StringKey())
checking = append(checking, pathIfWeGoFromCurrent)
}
if pathIfWeGoFromCurrent.totalLength < distFromThatSide {
f.Paths[neighborCoord] = &pathIfWeGoFromCurrent
// log.Printf("got update for %s \n", pathIfWeGoFromCurrent.StringKey())
distancesMap[pathIfWeGoFromCurrent.StringKey()] = pathIfWeGoFromCurrent.totalLength
checking = append(checking, pathIfWeGoFromCurrent)
} else {
continue // this path is better than existing
}
}
// f.Paths[currentCoord].done = true
checking = slices.DeleteFunc(checking, func (other PathSegmentEnd) bool { return other.stringPathSoFar == currentPath.stringPathSoFar })
storedPath, found := f.Paths[currentCoord]
if !found || storedPath.totalLength > currentPath.totalLength {
f.Paths[currentCoord] = &currentPath
}
// time.Sleep(time.Microsecond)
// fmt.Print(f.printLastDirection())
// time.Sleep(time.Second)
}
}
func (f *Field) printLastDirection() (result string) {
result += "\n"
for rowNum := 0; rowNum < f.Height; rowNum++ {
for colNum := 0; colNum < f.Width; colNum++ {
path, found := f.Paths[Coord{rowNum, colNum}]
if !found {
result += "."
} else {
result += path.lastDirection.AsSymbol()
}
}
result += "\n"
}
return
}

13
day17/example Normal file
View File

@ -0,0 +1,13 @@
2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533

5
day17/example2 Normal file
View File

@ -0,0 +1,5 @@
111111111111
999999999991
999999999991
999999999991
999999999991

141
day17/input Normal file
View File

@ -0,0 +1,141 @@
234224446511352516455541135325314531634727166622143245766763225647632451765767416671144613511443236745711614774573513156356624145646555245245
212322435323514544553641367363253666612634513752244741772464882587283261185481663234173466116432233354744314723223443226616466142554244615221
154152563214532244465661223532752765765536752111625474845637887368476118636565287714213441214743556752653777372231566152665555565542223661132
525351545121363546115654612131231624245142132533745467388828681775578761357857745526314153768275636564561457571644657123363231241411332326452
522114653525543314412571231775375576521726522526173787746687625733216838476458572461735582761552332354146473113664476437323544256213653465312
443461111453464425134533263471641143456312642513254553134486255281263333132627453587376148513841638112145441447467121325743662524522222564561
121561242535123615432415761156744117343342787528337632685333272267485453814315762514375344282355222836135147727216735114447424342623145611115
252364155625164632364226342712756457722482166171424514185723628628385346554137328865165246138577236216314644645345152746244165463554263431662
526235655521461214532363651147275227312867814244778334561422454447813222487123182687265214271615331836735221433453425273364571222611316513141
225613523613165246477246751254654753681146264113713375321815133313787726311628571367126163638537824746257617322231667614624625234561431115155
616563433623453266134751445441374188283828448111241335241358271167617623631466526171652755173476473628624453325743534253553643215315665446664
362454134346111664574275576134363881382835554627886265337151312271626774356824843526711868556657745186332348785164631216416322634341234413516
624336624532333445671425266574645652272115613176467134887187742741469411579948718446242474241162354716284775774771647436742135325654335522566
132343424642761631543123634645772441446845427562772253584555613884465971938339166852115365255171177614564262127653746766446335143674613123433
535535523125362516275233147278361816341145714212373261429933467545965438848556428291619597711642148812153188248135232621151315233111352664626
315343656233125631622636314237357726527844653166554352554933575475553534566517335193254452417712415515324211548464562731652665511511512253654
245346621476366211755722344353743773657654226437321698155539423337438917574628428885639646215654518165726815413468712371547455776676132656243
414551316523274412372317175216327536416562753587542796888542934873711444595783331864282184886161875526523451851636436814722427114752445365531
562663143716616766467664564174363675132344384574777184479621561743232265434434231935369454446266975363876764334367855477613562617435121536253
333251744334237531433255753676546564133517262375329868342183881735133142953538769718862388827716173671453554811618816365677225577565111344134
512661663126517112325472217657543714745735553916577177949218283171176592187821211794496862411899576961517632242138512638331655164553266254665
244227644642536633766824571648522446857399591619871385492483166519243226118878624437768759799251983569643248154211166213445165777234666464641
324626331157265155157741667232871854422398384135566321537312449412339576552296252386497358829297514485667786624681566152245124765225523676712
235175461263227616717532311838821361526356926784576534178895547893938757513753139533558433561823156636393612812576511244357754316122434344372
454551271165612352163544844775382463429373136432582264346514888937676299395477556354793172118843462717455359334461235517642641433143315324673
416777235642161515516777522231658872775586436867518275137556927363464472797246945994621431276264617637494126252357632267636242572624251211772
575512561173237458478861338838441972978863848866185494238353553765253724428398463789383193574682229233161522986384788872722638445261673456274
432461231356112282843327267713475289479639627883637285329736352263523668944488476829536699931496583343898715537781226624225246115566447124214
252374211644248241181722875581971815998627361756281283355698847254989548635729983476322282385822532133147958951348772444685135673375547112111
744751517727144286855173182671674768351646816849663957283526244234354549743269888853236478424856479232256548691921754871261651632127374773326
347346713512748345888728346126596745814927293432346969427775577329297989639265922864632254289837722247933768912811567686168868843565656653217
263525422653617877331388151423687859343291276798643458639499872848588365842774627258743984789859872779948615316313852576628322412161271277416
277113175314438882726783556873396413855231169332222256398724594787499249972764694354548667347376366499328522189845971158626881316477422427223
245677355173582872431581449915548919571284795989627935422527427683596825792637987238353427594946982728418324518961279422112637337471142624163
267354316271177145465486649797625579633264878858779569537863652839985535626474226832335689299483588652398435544987621954432653625233147551173
377654346388462182388671459629376356923778826739354739567967376956676676475646775265329749698236263866938779859135684496138268675617576414665
771754626518631562445589178277977182765474439572987274739268222633497565479748389525293666578349426979961779336492775716647444226316124512655
232677165584621238512574545123333857973584553287462722332937834949437747996976746325689466574343999457446979149255222712874245157455322253161
571517331668313885733787433842981235372267297852833756684977873598543647564674339786482286889793593647935699321881725769461878284244721524126
532213162631227782434387883462491267556949225489659973844878533548853853785345757484536544846887554546273496481544598171122555452171867424543
211233451885228341726955645125112515585522746724447664543555776843449658349758686847933942944526824425798375866989587953863528851883575732257
461673283885572418721956824884534117853785825835245753536773798749846489855758979583477964976962528886524484291832616559944782258142811254113
551745567215173881717465384242285886475968986496477846444344838468847889563747683844699878434799647432635288748175368314713153814671171583316
433136211825262278825549652223761352636945979765397635476539984848657445443793375637456955348734622577948439847242476876339788672616754773222
341476444277473656149363138182378655456396734378335886793793873964643865998394976458373365937348733496289268894571554998394665558346517171532
725148333332238721464119665117194535376789327288965665679644644849889355359637636575364545538834985277623753633296688395633394345638365141251
237733752121746652195652112461855435637442435369468347339547836585458374859445746866355579938788624834422823493824725835514514145252633111217
742173484483385623323737178513575879868346925664777384638899835397536798453557999739935988976993788948756778953321354545976365682473315831873
256162553288246862454931669768823755967327448644574654865398338646594775785845734838346993343964353956596849355391873252421731271378154158416
647611143287731788875665377547398763778946959366367468535776755896579896667774888566654676466854465465234899323699225115696914378788782261761
761584483441614646496258979977773637988422437459976888934949955888448847978846588785393643933759698533938252289955483438238344821416112135431
131653441781113528654819832795793723536858747568374353586645585579456598545786878747735688386453798489973229893572541932211763522545754288432
351435431774587738542714531244945446233565793677477556657866475985655674989947669744744833498483568563857529348972253818416477686632645617222
474833573242841478256345936362392996854889844895586943335546985765757966878465665597669844578549484969635755547986436374943152167431585427724
668874184642473919751153976864969787866297896464737784547795946547569494576846995766756738548589494956528446584679438364951963249487138322523
528725624761152327819595517454868825557798544787777668748684886944587886875784597564494955345468545557764664683692997796272664459383234468271
177437148884516181535654988786999739996678493436643755697498977987775798565946898889884577867886763979792697332792946798841654853731628523744
442361247181141182933518157353757765727538878755858474557755996494864876969578594588669746866956689553358636896564543761518884527661143322531
375251227671244772949529497956653786555839448577749749769654888645574864787864998687767898555567899563635343933337472963879652274874388156822
528858226558578556821285155698996484679446995674879994687857876767595796446896759794489559979683384366869269434996895238567686659384214676447
136263238663856349766786373944888986784864369665885574949979597644468788658685799698986768794337593377696744756996756723172285446462245728465
767223256145565586897633287358249238285396779986757478994569764845577997689589457955686858855463663485667696392425465253428235418156226256151
437775176261364587767849429958878855345499435634376668774568949777568776887597748587485784897759749468984385667483598878343637736486713363811
214436474531345195792918567557532395448683458888896855779677697985867879959577998686867849558878938333959823725857345712255479596583727545833
414758443545213224319231493927398357335449653347797747757849687757596755885966686654846554689534984885555933248756559948356274139793432274843
464165873353938783791212759986898624337359999933665694495697877758789989578656876878868668474776699744579462679968668748268121718868115527214
281418552824348619758435738856526466847658575477565769565589797978678988676857877567445988989677469469538539829686463432572149939875173433145
174745572187172734825327438492543924575974589439498578765746598899767757988956855785496888877558459337747787694597297962228175473327338484777
423561863457895625429957337935554753395958554744554766885876588597758565895778566777687765574496986947556946637957739771361764529851733731778
876464625342977414342158652568734625934775657348846666744756579888578769688896976589767479895447583959567439826398946571623698744513387161862
861524435137586438869557842356734487944663935494599899799687965887979778565885959677497645587789989738875346495894362792944358783326623864717
252162437585483184143333563589379384846535454987886746764794958895655596556598777898799798677965837694666889622667872721462528751722864574531
511835467648858463122855834823235252594346993435979575697859585787856759886889685959994484765896644589375446245555422761754213596566133655786
185767163152935413185483934749657393776997338375844977976999877696777885767957658878599568766597959677644694337432859589864618184367464663237
168352331587493643594282544262874648433436948954476859757786685998985686877996689784845779978789888367567753568222364473887654656831873518886
313847575343358369622475425393596573693553553475664966456468988556596966579695897966647774879958544966675886284764996391981912412686273864525
373283716753197975283815795263553242688656659394667497869488778578655597989779778678966748859587549566998575497752466827394482447342838558687
836764585368366932293427479724823928799384596667899864645965898686987768975986988968598745579674388483936764685998592549762341336622547118571
247882245157662991361453943787554369377534987474547945644678695868855779857988867745865484878656354465456887234799955694478693551968514531566
571745824313431258854696432362643266678736763794959846494594744699969798768675654768746549576975777495396784569274894757254671157467263337537
275382782161696438145337579287764348363465957967784446484488955786985868887786764645484487445587897476735675249884843668512937819945782888438
217118865135294215886286244356928338247374968985465849584876446499965788598767897674866859457883995589468837868668764946672362581157458453223
161762616588571177211757624985496884768757663548899465646697995957588866885879774694559595869535679899358587552833589569798385633683824477376
615738185278515945148931826499354984928337698948544695886769645955595854794467855979769766459398866945859864537966827295362723756851454667378
463858275648821252653439544785654984695649693834339356684947566497785794859445899577455595969447784764465834993256959555194238234428274766576
338526824731316778299327722883284957232494386489595686444589559979778766677669449569784497684534633973373369673487954545216614341354486546688
632663341188217371767432175779672344263585379573973398998568459976764465847449488495965985856776398569564967592352275586132667341372774582144
325327418886431163516783134822583489572569383579476977848996495497666599599859986968485869759576557439658784226222552141432856567122715223315
175626222822669261225782114655635489383869379798566579878595947674675967475666487769897769377955995976854626825643456128323475267184368481358
521323357458161622944913168548772657649499844398453597795696968688464675479887784797895439499343878895935872889348688549327979526282321534555
657685352472514713663558244566679583882284594596694645799956565484574989698659844865945858348354669445944379478334867693834555938122537147716
141488171222833987755851223559928238636427963876857645886446476578795976467947777658558593773699579953767927838358486398739277326554475442265
412228731552363175889135217262652782645923285376439895389973585975758446984555465573895895596848488625986957427972975581749123637831761353381
617657426244228783349859988279727555729665575366666558833739986957676488955584699954955898855544586592226593279252313954949991982333435345743
563442734644636355662243521542523458337282367653649638884398879578985456684668653974966989874646697672782569953496336899511812338637762232843
123663572448778825513745536862954443659747664546953765778543734458335968973986374649983369665896784968524444424982732395492581267433746863823
376137586674633422247188654418472528946595834784945567566555865934666645875353358387573569744966475669534496983426926432416113218734182381755
536512416727716348573211189799286479936666386849438359939695539833385979538849989684875449876478945525759697657772175829389678813764551371776
363762235558516186995537179287137923595878379285484597469333538564595445667659395973583984664463972748945252996983429483238836425782225683314
411567166341374665512465714148135639464364664678858934748484833547993355358584697736779897843654377746544924667542949676594752322771766675517
266463442531538515324415365824823343796529839682437757497647479747465599445594584493697737967736967359439396723339167681517716552748316854546
633435562174817662819725623494342626327278792489579386934644484644498873873868948379464469982422437757864357965387175465818761476538223856362
271474173321581181863484657984524328824246435927523486584385459438749874835766639984498748222533995293879343664866751243411775711247686463445
172532467668431552657851134947876991864446634488763265888467474978838337773547939668583324433527566227443264838614694858513778615328843151631
435124444672478328857526275643761216658887846797547794956838949975773449586349935373834828857947226423265395741533458688623884763564786266637
527444267343572326138358326494265251515445724778664453589368769497437586989568786664953228358866755867323355671375222573553884523886831565716
232337242156786171451425413151143419785995526686499884629874972235468659879944847389624798358326959692735154455522549119836424612438782565661
525324767315186524221162635561617576129944485347978995887487338596577476435565783459232524539973955777662639947226566144181176541648162561146
374312313453312672658751476534281367282451989647686494962485282847957484876677835643636882948663369983595636223946539735662847525583337135237
657725674341517314457517872977437584615635962693597374837933993832832447824828324257766543384427365687137896483877556522741157743758623147235
372771127456775223357761286248823729625469464727389249234268533582598285959338783389942977743988349247557954865442128257123555573555665712277
515335142747587238136873274317921898723494161257278958799238797628727856354779782864486553256846279834971983684475434414142845851554475257543
211654411663544148312323264167279678992961187448639679752372227475263666927434479985957627522647248923252666532949661642868618327463727133563
575353243265174755577627351259572411237675399671852542638477264597378258822786682357658459859297678637384567437676676788158223637447654324241
344563236652472466162868335152512983162251322752221983569455659558997445574957993598937383382183666629133836889984451477661412435246555225437
223437426514546151342863435541278568599978275856575243983899862339557836373398238542432233847936295436294528611618584858851828155673555363673
565267721125431772222258467541455975845395974995469284982657533426324232877942832789383857469751548722226928556847584174234736857324544174152
365667545462772453144622182647818785365195919574971662635979473568846497942633855587497515748764182696877559548732422754666482313545142511135
223136324577276227545366164446137136233877236897773326422143147259726566925933512459523473114954553466232232776114255426767556663344523436615
424657666415524464138833867462574616883893546743855815271583277657111922247498683683766485168765665542848175476622462668832226126115112332563
265116573351654653441425345433343264833724888898842991923143888271879144828857645268426289642628397318681665422186228823688656343674532547354
361613244731346314646327483436567238222652636787896548679217398257817686662528758385799662244638311167665784234841576282126433564612123217133
566364152131725364444145773355671731247157114363871741697496591525191862316358584851473882379619342855334363254135373643731231155715651443244
111256566517565774627311267233356113376155286417849697544313299988665623767484297173881389848752358814348845512636815363336672474277513532513
443415375565151466272566277284217717116514368712686791634141681571148667847889493663177485648178298411228854887663854727367737311167552216516
346244164371664363576256455768247347527684384744154279982116974879397294334515785175512974663863243245154443634883878735143151637137165135251
245632136545644623126337434764381666623631161252826613527253566475798597166214319621828796869725132868176464361433567372227351272445425655651
216516466233275546255724611446138854335312811112351914676757679869148379278132566316591751417332726833815854582571241637211356745616512565456
535111311616623261131633537112222351656224611568521375539855193624971352427634275747912521578651888765337513854385447315451356342663443241522
523564135532151626121315326541456154526371646221237578445836513495426246777258961949148742547244632878273363385166566742534466366314635143152
252234333515321424364621267324383352132525716557115147854544432871484997689117152888713844268647861353255157166367574546234537757321331262556
145425651236623323273646167464663122164851147643457787247461125283223626312635725718725441162657418176341734786544172723625253573163221641535
153544115353616111234322326252465422638432222836276323413433218484851271551611574588585845524146471163748483365626544317657137523214565531664
142454216141535536457677212613665411757451356675835477342741324113281212655366244462261387532622858861721373636645712474172445746411324252141
544366355562212456323663575657652624265473162613232765146888652425214745741187347176246123657843165474374431111567224417353336461634551513361
162431526333225556252567676426362353124153634115551752877846847315344364261273381582556742168434542853227112722624762136771622135152112255361
545211215455544513544364765155651334654454375184885815375682761478165137382187757661532864566263223816742121372337523621743544464511511654416
323113161515166212124556631324747676271655372663781844847848363158431826435478212561154784384763281335313764523453446537353211111351216512414
312123524165526161625112775375335447714743527628876182525825513257478674653831351253148186847312267555532675463177543242652353436331666156244
355325535435126443145256221374357314335277645253556148534766883344445466258512416725216676611327231436111554244566637214312626234251335633244
452513136443331344523212472543322532423675742651453274312732754834486467728123135388734127674473266575113615746472746566662242413522623644335

10
day17/notes.org Normal file
View File

@ -0,0 +1,10 @@
#+title: Notes
* so, just traversal doesn' work,
and it's easy to imagine why.
my guess is that i really should put 'paths to explore' into priority queue
and select new ones not only by their length, but also by how far they go from the goal
* lot's of time for no result
* so, for 'dijksra' don't store set of vertices,
but of ways we've entered them

14
day18/example Normal file
View File

@ -0,0 +1,14 @@
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)

8
day18/example2 Normal file
View File

@ -0,0 +1,8 @@
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
U 2 (#d2c081)
L 2 (#59c680)
D 2 (#411b91)
L 2 (#8ceee2)
U 5 (#d2c081)

678
day18/input Normal file
View File

@ -0,0 +1,678 @@
R 10 (#090220)
U 4 (#61ce73)
R 2 (#27ac30)
D 4 (#6e7db3)
R 9 (#42fb60)
U 2 (#267e43)
R 3 (#4a73a0)
U 3 (#0da863)
L 7 (#464882)
U 3 (#667693)
L 4 (#6ed2b2)
U 8 (#1efed3)
L 6 (#898ac2)
D 8 (#596eb3)
L 3 (#4a1fe2)
U 4 (#1c92f3)
L 4 (#42f8d0)
U 3 (#5d3b13)
L 6 (#2913d0)
U 8 (#1941a3)
L 6 (#401a90)
D 9 (#1941a1)
L 3 (#4f3ef0)
D 2 (#53b9d3)
L 3 (#0e2a00)
D 4 (#201683)
L 7 (#547ba0)
D 4 (#662e23)
L 9 (#814d30)
D 2 (#662e21)
L 6 (#0d69f0)
D 8 (#201681)
R 4 (#9da8d0)
D 10 (#3bef23)
L 4 (#235c92)
D 12 (#385aa3)
L 4 (#859100)
D 2 (#7489f3)
L 3 (#479820)
D 9 (#23dbc3)
L 5 (#3f40a2)
U 9 (#345f23)
L 5 (#054280)
U 5 (#7cd843)
R 5 (#054282)
U 7 (#00d753)
R 4 (#495402)
U 5 (#01d321)
L 4 (#131fa0)
U 9 (#67f181)
L 5 (#131fa2)
U 3 (#484a11)
R 11 (#449482)
U 4 (#40f083)
R 3 (#79b112)
U 4 (#03fa03)
L 5 (#9d0da0)
U 6 (#42a593)
L 5 (#a14cf2)
D 6 (#0e7581)
L 4 (#1b7142)
U 5 (#943c61)
L 5 (#251122)
U 4 (#203bd1)
L 5 (#782ad2)
U 6 (#579893)
R 5 (#a1c232)
U 3 (#5cdfa3)
L 3 (#07ec72)
U 10 (#0e7583)
L 6 (#86c2f2)
U 8 (#2d6c93)
R 6 (#2f99f0)
D 4 (#5c8973)
R 4 (#4b9c00)
D 8 (#844383)
R 5 (#5874b0)
D 4 (#083593)
R 3 (#0656e0)
D 7 (#00a893)
R 4 (#512100)
U 6 (#0bc123)
R 5 (#5d94f0)
U 5 (#54fef3)
R 3 (#3cf440)
U 6 (#34bb23)
R 2 (#2e88f0)
U 10 (#1f8b71)
R 4 (#2a6b20)
U 5 (#956d41)
R 9 (#47de90)
D 5 (#0d51e1)
R 6 (#660ce0)
D 6 (#09bfe1)
L 7 (#471c70)
D 4 (#1bf301)
L 4 (#35d600)
D 6 (#47d3b1)
R 9 (#0a6000)
D 3 (#acfc31)
R 2 (#0b2850)
D 8 (#0a4571)
R 4 (#88cfe0)
U 13 (#46fa21)
R 2 (#50e990)
U 10 (#3beb21)
R 3 (#77a972)
U 5 (#853671)
R 6 (#779852)
D 4 (#110871)
R 10 (#5bb5c0)
U 4 (#570241)
R 9 (#266ab0)
U 5 (#2b46c1)
L 10 (#7d5450)
U 6 (#53d241)
L 8 (#26cbf2)
U 5 (#7fbd91)
L 7 (#01d962)
U 9 (#0a2a51)
R 6 (#a49cd2)
U 4 (#39f461)
R 6 (#47dd50)
U 3 (#57b491)
R 4 (#597fa0)
U 5 (#57b493)
R 3 (#2be530)
U 3 (#304e21)
R 6 (#398860)
U 6 (#5d17e3)
R 4 (#64bdb0)
U 5 (#8d7a13)
R 6 (#26dea0)
U 8 (#5d6ab3)
R 9 (#5d7c80)
U 6 (#1e3dd3)
R 11 (#5f4260)
U 5 (#4781f1)
R 2 (#9acef2)
U 3 (#2bebd1)
R 4 (#0f9132)
U 8 (#3f3b41)
R 3 (#aa6020)
U 3 (#32e981)
R 3 (#1f6210)
U 10 (#11dbb1)
R 7 (#0fdc60)
D 7 (#225c03)
R 6 (#2a9790)
D 6 (#30fb13)
R 3 (#1d9890)
U 5 (#4f4fb3)
R 6 (#1d9892)
U 6 (#54c773)
R 7 (#5c5340)
U 3 (#42ccd3)
R 4 (#6c5f50)
D 4 (#54deb3)
R 5 (#346460)
U 4 (#587ed3)
R 5 (#52d9e0)
U 4 (#2ca473)
R 3 (#60a9c2)
U 6 (#46a783)
R 6 (#60a9c0)
U 4 (#44b7c3)
R 6 (#4ee4d0)
U 6 (#5ff893)
R 2 (#822bf2)
U 4 (#135f03)
R 8 (#895082)
D 8 (#5dd3f3)
R 3 (#3705f2)
U 5 (#330ca3)
R 3 (#479970)
D 5 (#6713d3)
R 2 (#6efa40)
D 8 (#1a84c3)
R 3 (#2dc650)
U 10 (#364363)
R 3 (#7b5f50)
U 3 (#703961)
R 3 (#23de92)
D 5 (#0753d3)
R 8 (#7664c2)
D 5 (#0753d1)
L 2 (#638da2)
D 7 (#3c7b51)
L 9 (#8455d0)
D 2 (#2c3021)
L 3 (#797b20)
D 5 (#444701)
L 4 (#495820)
D 6 (#173ab1)
L 7 (#30db32)
D 4 (#80ebf1)
R 6 (#30db30)
D 9 (#69de41)
R 4 (#77ebd0)
D 5 (#0561e1)
R 4 (#46de40)
D 4 (#9b6fe1)
R 11 (#4d17b0)
D 3 (#2329e1)
R 2 (#6c6980)
D 3 (#818f43)
R 7 (#8247f0)
D 10 (#68ebd3)
R 4 (#0cbf10)
D 4 (#9ec973)
L 11 (#44dfe0)
D 4 (#4d5981)
L 3 (#24a940)
D 4 (#29dba1)
L 5 (#473312)
D 9 (#868e71)
L 7 (#473310)
D 4 (#09f1b1)
L 4 (#628210)
U 5 (#818f41)
L 11 (#0a8230)
U 5 (#6ac553)
L 3 (#167900)
U 3 (#53d473)
L 3 (#2bd770)
D 4 (#408de3)
L 6 (#917dc0)
D 5 (#408de1)
R 6 (#103f40)
D 7 (#4d5b33)
R 3 (#388600)
D 3 (#9782a3)
R 8 (#60fec0)
D 3 (#7b0341)
R 8 (#1eb200)
D 4 (#312db1)
R 3 (#58fa10)
U 5 (#1a60c3)
R 4 (#3cb940)
U 2 (#1a60c1)
R 7 (#3d4bd0)
D 5 (#312db3)
L 7 (#21a720)
D 5 (#69da91)
L 11 (#08f6f0)
D 4 (#2b1111)
L 6 (#99d042)
D 3 (#4a68e1)
L 5 (#0e1c52)
D 5 (#3487f1)
L 7 (#a7ec90)
D 7 (#2bdcd1)
L 3 (#214032)
D 7 (#28ec31)
R 5 (#55c3e0)
D 2 (#79c001)
R 10 (#55c3e2)
D 6 (#700531)
R 5 (#6a9e12)
D 10 (#688521)
R 3 (#366c22)
D 3 (#6e5463)
R 5 (#30a512)
D 5 (#129f73)
L 6 (#8db872)
D 2 (#54c511)
L 4 (#4de992)
D 4 (#2c2ec1)
R 4 (#589972)
D 6 (#688523)
R 6 (#5f6de2)
D 4 (#01ea61)
R 5 (#47a442)
U 8 (#85ce21)
R 5 (#47a440)
U 7 (#3b18e1)
L 5 (#40c622)
U 4 (#14af21)
R 7 (#5edf22)
U 5 (#671003)
R 2 (#1b29e2)
U 4 (#273d33)
R 10 (#1b29e0)
U 6 (#493353)
R 3 (#049182)
D 7 (#7fe163)
R 5 (#3a4a52)
D 7 (#005723)
R 4 (#383002)
U 3 (#04e0f3)
R 4 (#782dc2)
U 6 (#8d97f3)
L 4 (#2569e2)
U 5 (#415d41)
R 5 (#27b292)
D 2 (#1c9fa1)
R 2 (#21e3e2)
D 7 (#9ead41)
R 5 (#21e3e0)
D 3 (#15cf41)
R 5 (#382a10)
D 4 (#2db171)
R 7 (#607330)
D 5 (#210cb1)
R 3 (#722700)
D 3 (#58a1d1)
R 4 (#559e92)
D 6 (#23d4e1)
R 3 (#7cfba2)
D 6 (#570981)
R 5 (#382a12)
D 3 (#10dff1)
R 4 (#2dd3b2)
D 5 (#29f051)
R 8 (#5c5322)
U 5 (#29f053)
R 8 (#7c5182)
D 4 (#06d041)
R 7 (#0eb4b2)
D 4 (#aad7d1)
R 6 (#4bb150)
D 5 (#018911)
R 9 (#8f71a0)
D 4 (#018913)
L 5 (#35c370)
D 3 (#06d721)
L 7 (#826820)
D 5 (#06d723)
L 4 (#1a5580)
U 4 (#154ba1)
L 9 (#1b1a40)
U 4 (#312983)
L 5 (#1954f0)
D 8 (#42a4c3)
L 7 (#2bcbf0)
D 6 (#3503a1)
R 7 (#758940)
D 2 (#3ecaa1)
R 8 (#386590)
D 6 (#5e4e81)
R 7 (#0e3c80)
D 5 (#26f911)
R 11 (#968190)
D 5 (#595b61)
R 6 (#6a6f80)
D 2 (#52fe41)
R 6 (#8744e0)
D 3 (#602db1)
R 2 (#8744e2)
D 10 (#0a8c31)
R 6 (#6a6f82)
D 8 (#193131)
L 6 (#5f1f00)
D 4 (#7cdd71)
L 3 (#6b3d50)
D 7 (#249321)
L 9 (#7dc2c0)
D 8 (#62d1f1)
R 9 (#44aca0)
D 4 (#904201)
L 7 (#6b7f80)
D 8 (#904203)
L 3 (#2eb8a0)
D 8 (#63f7a1)
L 4 (#493500)
D 3 (#8cc041)
L 4 (#7b1b20)
D 3 (#8b6cc1)
L 5 (#6c17a0)
D 4 (#73f141)
R 5 (#4c8490)
D 3 (#334893)
R 11 (#098a80)
D 4 (#6a78e3)
L 12 (#098a82)
D 3 (#4e3fe3)
L 4 (#0b86c0)
D 7 (#6a0593)
L 9 (#179fa0)
D 6 (#56f603)
L 2 (#15a970)
D 5 (#1d2413)
L 10 (#a5a530)
D 4 (#1427e3)
R 3 (#40f6d0)
D 7 (#11cd13)
R 6 (#4bf000)
D 4 (#0c91e3)
R 7 (#23bfe0)
D 5 (#0c91e1)
R 3 (#6a9340)
U 13 (#1df6f1)
R 4 (#7a7962)
U 3 (#6ddfe1)
R 7 (#524da2)
D 7 (#70b671)
R 7 (#4e23b2)
D 8 (#30dd71)
L 5 (#20f980)
D 4 (#a02bd1)
L 3 (#1efe40)
D 8 (#508913)
L 3 (#5a2560)
D 4 (#514333)
L 3 (#595c00)
D 9 (#514331)
L 3 (#751590)
D 7 (#508911)
L 6 (#383030)
D 8 (#637d41)
L 3 (#44eda2)
D 3 (#2d6cd1)
R 7 (#3cac72)
D 3 (#6cca11)
R 7 (#505db0)
D 3 (#21e651)
L 5 (#505db2)
D 7 (#5e1611)
L 5 (#3cac70)
U 7 (#39ca81)
L 4 (#53ed02)
D 6 (#1c6103)
L 4 (#4544c2)
U 3 (#63ec43)
L 5 (#0a7bc0)
U 7 (#410b53)
L 5 (#0a7bc2)
U 10 (#31e923)
R 4 (#5650b2)
U 8 (#60bc13)
L 4 (#6c4ed2)
U 3 (#01c071)
L 9 (#6251e0)
U 2 (#6fb8e1)
L 2 (#3e1f70)
U 6 (#4e52e1)
R 3 (#2bea02)
U 5 (#1a6363)
R 8 (#6c00f2)
U 3 (#1a6361)
L 2 (#5249a2)
U 4 (#26e541)
L 8 (#279592)
D 3 (#34d2c1)
L 4 (#6d3cc2)
D 7 (#5f0761)
L 6 (#2996f2)
U 11 (#404d91)
R 3 (#7b7b92)
U 4 (#04e471)
R 6 (#8eaea2)
U 2 (#04e473)
R 7 (#0189f2)
U 4 (#404d93)
L 4 (#1d8222)
U 2 (#408021)
L 9 (#099410)
U 3 (#62d111)
R 4 (#5f0a20)
U 5 (#215971)
R 4 (#054570)
U 7 (#562131)
R 3 (#2cc810)
U 7 (#81bbf3)
L 4 (#8380d0)
U 4 (#080873)
L 8 (#371d70)
U 2 (#89c461)
L 2 (#4910e0)
U 7 (#08fb51)
L 7 (#21af20)
D 4 (#1e4701)
L 5 (#279590)
D 2 (#63a4f1)
L 8 (#8baa52)
D 4 (#208f51)
R 6 (#0cc5a2)
D 2 (#397b91)
R 7 (#150182)
D 3 (#44cd31)
L 4 (#324f92)
D 3 (#6518a1)
L 5 (#82ed62)
D 4 (#5cb001)
L 4 (#2cadb2)
U 4 (#0c40a1)
L 13 (#4e4972)
D 4 (#3e5303)
L 8 (#191842)
U 4 (#3eba41)
L 3 (#900472)
D 4 (#3eba43)
L 8 (#188072)
D 6 (#3e5301)
R 10 (#7f8942)
D 6 (#0f5643)
R 11 (#60f2c2)
D 3 (#293e73)
R 8 (#67ab70)
U 7 (#7bd493)
R 4 (#67ab72)
D 7 (#97e8c3)
R 7 (#4bcf92)
D 2 (#4d67a3)
R 5 (#55dba0)
D 5 (#2fb9d3)
L 12 (#55dba2)
D 3 (#4a35c3)
L 3 (#5de0f0)
D 2 (#41fef3)
L 8 (#733be0)
D 5 (#3fdfb3)
L 7 (#2bcdb2)
D 4 (#692073)
L 4 (#4e7612)
U 3 (#598803)
L 12 (#56d912)
U 4 (#752383)
L 8 (#0e43f2)
U 4 (#138221)
L 7 (#276162)
U 10 (#545561)
L 4 (#7a6f62)
U 9 (#4ebfa1)
L 5 (#2edad2)
U 7 (#0427c1)
L 3 (#6722a2)
U 5 (#2cedc1)
L 3 (#0aa132)
U 7 (#6db771)
L 7 (#5feaa0)
D 7 (#04e323)
L 8 (#01f6d0)
U 4 (#2a1803)
L 6 (#767010)
U 2 (#2a1801)
L 10 (#537b60)
U 5 (#04e321)
L 6 (#16a280)
U 4 (#4edba1)
L 3 (#8f1c22)
U 6 (#30de81)
L 6 (#0df712)
U 4 (#6fb451)
L 3 (#321b02)
U 10 (#44c0c1)
L 6 (#0cbf22)
U 6 (#2dedf1)
R 2 (#645d52)
U 6 (#008341)
R 5 (#5d3e82)
U 5 (#85b221)
R 5 (#747de2)
U 5 (#0f5b41)
R 4 (#0c23e2)
U 4 (#3b9673)
R 7 (#65bf42)
U 3 (#7256d3)
R 3 (#350892)
U 6 (#aded41)
R 3 (#41e0a2)
U 3 (#69f231)
R 9 (#04bf52)
D 5 (#664b91)
R 3 (#5c5c02)
D 10 (#0efdf1)
R 4 (#5f4102)
D 4 (#43efa3)
R 5 (#5abff2)
U 8 (#3ccba3)
R 4 (#429202)
U 2 (#7cebd3)
R 6 (#429200)
U 5 (#0b7163)
R 2 (#5abff0)
U 2 (#3b0713)
R 11 (#9d9132)
U 2 (#1483d3)
R 8 (#52f672)
U 7 (#9469f1)
R 4 (#40a982)
U 4 (#04d1d1)
L 8 (#311ef2)
U 4 (#7cf7b1)
L 6 (#12ff02)
U 7 (#426fe1)
L 6 (#5d3c02)
U 5 (#4c3953)
L 10 (#101c72)
U 3 (#83b373)
L 7 (#1d9202)
U 4 (#0f4ef3)
L 5 (#1d2662)
U 5 (#24e2d3)
L 7 (#5e8242)
U 5 (#833353)
R 7 (#29d152)
U 4 (#10a5c3)
L 3 (#2027e2)
U 3 (#662453)
L 7 (#6cab80)
U 4 (#1a9013)
L 4 (#6cab82)
U 3 (#5b9d73)
L 4 (#6661c2)
U 9 (#08dc33)
L 2 (#7e9792)
U 2 (#407f13)
L 5 (#8f3792)
U 5 (#49b883)
R 6 (#060290)
U 6 (#620dd3)
R 4 (#26e8d0)
U 3 (#41fde3)
R 5 (#4c84e0)
U 7 (#1663f3)
R 4 (#797042)
U 6 (#5c6d93)
L 8 (#289052)
U 6 (#5b9c03)
L 5 (#5e5b60)
U 8 (#44baf3)
L 6 (#6c1932)
U 3 (#1ef693)
L 5 (#7757c2)
U 8 (#1522f3)
L 2 (#3564a0)
U 10 (#3e8e53)
L 3 (#370a00)
U 2 (#4f7043)
L 7 (#55b7b0)
U 7 (#8dfe91)
L 8 (#019ee0)
D 8 (#600a93)
L 6 (#1fabc0)
D 11 (#178133)
R 6 (#418080)
D 8 (#5619b3)
L 6 (#9a4e90)
D 3 (#2b6983)
L 6 (#0da050)
D 4 (#3b7653)
R 3 (#396df0)
D 6 (#4c8b43)
R 6 (#752a72)
D 4 (#726f81)
L 7 (#736dd2)
D 5 (#461241)
L 4 (#4e1ff2)
D 7 (#461243)
R 5 (#3220d2)
D 8 (#726f83)
R 6 (#185fb2)
D 4 (#489f93)
L 9 (#aced52)
D 3 (#440513)
L 2 (#5e9192)
D 6 (#11a943)
L 3 (#3ff332)
D 5 (#55e473)
L 3 (#3ff330)
D 6 (#58fa73)
L 5 (#562302)
D 4 (#60e413)
L 4 (#7457e2)
U 4 (#87c6d3)
L 11 (#3e5822)
U 6 (#1d1bc3)
R 11 (#1604c2)
U 6 (#62d593)
L 4 (#0a2e92)
U 6 (#625e71)
L 3 (#77cce2)
U 6 (#625e73)
L 5 (#3912b2)
U 4 (#3288c3)
L 8 (#1be242)
U 5 (#903ae3)

538
day18/lagoon.go Normal file
View File

@ -0,0 +1,538 @@
package day18
import (
"fmt"
"log"
"os"
"slices"
"strconv"
"strings"
"sync"
)
func Run() int {
log.Println("hello day 18")
log.Println("problem of lagoon bgins")
filename := "day18/input"
instructions := ReadInstructionas2(filename)
h, w := calcHeightWidth(instructions)
log.Printf("read %+v instructions", instructions)
field := CreateField(h, w)
// fmt.Println(field.String())
borderAmount := field.digByInstructions(instructions)
// log.Println(">>> created field", field.BordersFromLeft)
// fmt.Println(field.String())
// WriteToFile("borders.txt", field.String())
// convert -size 3000x6000 xc:white -font "FreeMono" -pointsize 13 -fill black -draw @borders.txt borders.png
log.Printf("starting dig inside for cols %d-%d and rows %d-%d ", field.MinCol, field.MaxCol, field.MinRow, field.MaxRow)
insideAmount := field.digInsides()
log.Printf("border is %d; inside is %d", borderAmount, insideAmount)
// fmt.Println(field.String())
// fmt.Println(field.Height, field.Width)
// WriteToFile("fulldug.txt", field.String())
// convert -size 3000x6000 xc:white -font "FreeMono" -pointsize 13 -fill black -draw @fulldug.txt fulldug.png
// field.countDugOut()
return borderAmount + insideAmount
}
// determine size of field. max(sum(up), sum(down)) for height,
// same for left and right,
// translate (0,0) into center of the field
//
// have cells, with coord. and i guess four sides, with color.
// i guess have directions, map[direction]color
// and have 'opposite' on directoin.
// for each direction apply it to cell coord, get cell, get opposite directoin and color it
//
// then have method on field and cell that excavates cell and colors all neighbors
//
// last part is filling in isides, should be ok with horizontal scans from left by even crossings
type Direction int
const (
Upward Direction = iota
Downward
Leftward
Rightward
)
func (d Direction) opposite() Direction {
switch d {
case Upward:
return Downward
case Downward:
return Upward
case Leftward:
return Rightward
case Rightward:
return Leftward
}
panic("unaccounted direction")
}
var DirectionNames []string = []string{"U", "D", "L", "R"}
func (d Direction) String() string {
return DirectionNames[d]
}
func DirectionFromString(s string) Direction {
index := slices.Index(DirectionNames, s)
if index == -1 {
panic(fmt.Sprint("bad direction", s))
}
return Direction(index)
}
type Instruction struct {
Direction Direction
Steps int
Color string
}
func ReadInstructionas(filename string) (result []Instruction) {
bytes, err := os.ReadFile(filename)
if err != nil {
panic(fmt.Sprint("error reading file: ", filename))
}
text := strings.TrimSpace(string(bytes))
for _, line := range strings.Split(text, "\n") {
result = append(result, ReadInstruction(line))
}
return
}
func ReadInstruction(line string) Instruction {
fields := strings.Fields(line)
direction := DirectionFromString(fields[0])
steps, err := strconv.Atoi(fields[1])
if err != nil {
panic(fmt.Sprint("bad steps in line: ", line))
}
color := fields[2][1 : len(fields[2])-1]
return Instruction{Direction: direction, Steps: steps, Color: color}
}
func ReadInstructionas2(filename string) (result []Instruction) {
bytes, err := os.ReadFile(filename)
if err != nil {
panic(fmt.Sprint("error reading file: ", filename))
}
text := strings.TrimSpace(string(bytes))
for _, line := range strings.Split(text, "\n") {
result = append(result, ReadInstruction2(line))
}
return
}
func ReadInstruction2(line string) Instruction {
fields := strings.Fields(line)
hexDist := fields[2][2 : len(fields[2])-2]
hexDirection := fields[2][len(fields[2])-2 : len(fields[2])-1]
var direction Direction
switch hexDirection {
case "0":
direction = Rightward
case "1":
direction = Downward
case "2":
direction = Leftward
case "3":
direction = Upward
}
dist, err := strconv.ParseUint(hexDist, 16, 64)
if err != nil {
panic(err)
}
return Instruction{
Steps: int(dist),
Direction: direction,
}
}
func calcHeightWidth(instructions []Instruction) (height, width int) {
movements := make(map[Direction]int)
for _, instr := range instructions {
movements[instr.Direction] += instr.Steps
}
if movements[Downward] > movements[Upward] {
height = 2 * movements[Downward]
} else {
height = 2 * movements[Upward]
}
if movements[Leftward] > movements[Rightward] {
width = 2 * movements[Leftward]
} else {
width = 2 * movements[Rightward]
}
height += 10
width += 10
return
}
type Coord struct {
Col, Row int
}
func (c Coord) applyDirection(d Direction) Coord {
switch d {
case Upward:
c.Row -= 1
case Downward:
c.Row += 1
case Leftward:
c.Col -= 1
case Rightward:
c.Col += 1
}
return c
}
type Cell struct {
IsDug bool
ToBeDug bool
Coord Coord
}
type BorderSymbol rune
// ” always left to right
const (
Vertical BorderSymbol = '|'
ToDown BorderSymbol = '7'
ToUp BorderSymbol = 'J'
FromUp BorderSymbol = 'F'
FromDown BorderSymbol = 'L'
)
type Field struct {
Height, Width int
// Cells [][]*Cell
Cells map[Coord]*Cell
MinRow, MaxRow, MinCol, MaxCol int
BordersFromLeft map[int]map[int]BorderSymbol
}
func (f *Field) confirmCoord(c Coord) {
// log.Printf("configming coord %+v", c)
if c.Row-3 < f.MinRow {
f.MinRow = c.Row - 3
}
if c.Row+3 > f.MaxRow {
f.MaxRow = c.Row + 3
}
if c.Col-3 < f.MinCol {
f.MinCol = c.Col - 3
}
if c.Col+3 > f.MaxCol {
f.MaxCol = c.Col + 3
}
}
func CreateField(height, width int) Field {
return Field{
Height: height, Width: width,
Cells: make(map[Coord]*Cell),
BordersFromLeft: make(map[int]map[int]BorderSymbol),
}
}
func PutSymbIntoMMMMap(mmmap map[int]map[int]BorderSymbol, row, col int, symb BorderSymbol) {
rowMap := mmmap[row]
if rowMap == nil {
rowMap = make(map[int]BorderSymbol)
mmmap[row] = rowMap
}
rowMap[col] = symb
}
func (f *Field) digByInstructions(instructions []Instruction) (borderAmount int) {
// for the last turn
instructions = append(instructions, instructions[0])
// but also don't overcount the border
borderAmount -= instructions[0].Steps
runnerCoord := Coord{Col: 0, Row: 0}
// f.Cells[runnerCoord] = &Cell{
// IsDug: true,
// }
// f.confirmCoord(runnerCoord) // should be confirmed when the cycle is closed on last step
// borderAmount += 1
var prevInstruction Instruction
firstInstruction := true
for _, instruction := range instructions {
log.Printf("starting new instruction %+v", instruction)
if !firstInstruction {
turn := getTurnAsIfGoingFromLeft(prevInstruction.Direction, instruction.Direction)
for _, theTurn := range turn {
// log.Printf(">> putting turn %s", string(turn))
PutSymbIntoMMMMap(f.BordersFromLeft, runnerCoord.Row, runnerCoord.Col, theTurn)
}
}
firstInstruction = false
// log.Printf("starting instruction %+v", instruction)
for i := 0; i < instruction.Steps; i++ {
runnerCoord = runnerCoord.applyDirection(instruction.Direction)
// f.Cells[runnerCoord] = &Cell{
// IsDug: true,
// }
f.confirmCoord(runnerCoord)
borderAmount += 1
// log.Printf("inside %+v updated border amount to %d", instruction, borderAmount)
if instruction.Direction == Upward || instruction.Direction == Downward {
_, alreadyCountedTurn := f.BordersFromLeft[runnerCoord.Row][runnerCoord.Col]
if !alreadyCountedTurn {
PutSymbIntoMMMMap(f.BordersFromLeft, runnerCoord.Row, runnerCoord.Col, Vertical)
}
}
}
prevInstruction = instruction
}
return
}
func getTurnAsIfGoingFromLeft(directionFrom, directionTo Direction) []BorderSymbol {
// log.Printf("getTurnAsIfGoingFromLeft from %s to %s", directionFrom.String(), directionTo.String())
var symbol BorderSymbol
if directionTo == Rightward && directionFrom == Upward {
symbol = FromUp
}
if directionTo == Rightward && directionFrom == Downward {
symbol = FromDown
}
if directionTo == Leftward && directionFrom == Upward {
symbol = ToDown
}
if directionTo == Leftward && directionFrom == Downward {
symbol = ToUp
}
if directionFrom == Rightward && directionTo == Upward {
symbol = ToUp
}
if directionFrom == Rightward && directionTo == Downward {
symbol = ToDown
}
if directionFrom == Leftward && directionTo == Upward {
symbol = FromDown
}
if directionFrom == Leftward && directionTo == Downward {
symbol = FromUp
}
// panic(fmt.Sprint("got strange from %s to %s", directionFrom.String(), directionTo.String()))
return []BorderSymbol{symbol}
}
func (f *Field) String() string {
s := "text 15,15 \""
for row := f.MinRow; row <= f.MaxRow; row++ {
rowChars := make([]rune, f.MaxCol-f.MinCol+1)
for col := f.MinCol; col <= f.MaxCol; col++ {
rowBords := f.BordersFromLeft[row]
if rowBords != nil {
bord, exists := rowBords[col]
if exists {
rowChars[col-f.MinCol] = rune(bord)
continue
}
}
cell := f.Cells[Coord{col, row}]
if cell != nil && cell.ToBeDug {
rowChars[col-f.MinCol] = '@'
continue
}
if f.isCellDug(row, col) {
rowChars[col-f.MinCol] = '#'
} else {
rowChars[col-f.MinCol] = '.'
}
}
s += string(rowChars)
s += "\n"
}
s += "\""
return s
}
func (f *Field) digInsides() (result int) {
lineSum := make(chan int)
var wg sync.WaitGroup
rowsCount := f.MaxRow - f.MinRow
wg.Add(rowsCount)
done := make(chan bool)
go func() {
wg.Wait()
close(lineSum)
}()
go func() {
for rowInternalCount := range lineSum {
result += rowInternalCount
}
close(done)
}()
for row := f.MinRow; row < f.MaxRow; row++ {
go func(row int){
if row%10000 == 0 {
log.Printf("processed rows %d out of %d", row, f.MaxRow)
}
specialBorders := f.BordersFromLeft[row]
if len(specialBorders) == 0 {
wg.Done()
return
}
type BorderItem struct {
border BorderSymbol
col int
}
rowBorders := make([]BorderItem, 0)
for col, borderSymbol := range specialBorders {
rowBorders = append(rowBorders, BorderItem{borderSymbol, col})
}
slices.SortFunc(rowBorders, func(a BorderItem, b BorderItem) int {
return a.col - b.col
})
// log.Printf(">>>>>>> for row %d sorted %+v", row, rowBorders)
prevBorder := rowBorders[0]
bordersCrossed := 0
if prevBorder.border == Vertical {
bordersCrossed += 1
}
for _, specialBorder := range rowBorders[1:] {
diff := specialBorder.col - prevBorder.col - 1
if specialBorder.border == ToUp && prevBorder.border == FromUp {
bordersCrossed += 1
prevBorder = specialBorder
continue
}
if specialBorder.border == ToDown && prevBorder.border == FromDown {
bordersCrossed += 1
prevBorder = specialBorder
continue
}
if specialBorder.border == ToUp && prevBorder.border == FromDown {
prevBorder = specialBorder
continue
}
if specialBorder.border == ToDown && prevBorder.border == FromUp {
prevBorder = specialBorder
continue
}
if bordersCrossed%2 == 1 { // is in
for col := prevBorder.col + 1; col < specialBorder.col; col++ {
// f.Cells[Coord{Col: col, Row: row}] = &Cell{
// ToBeDug: true,
// }
}
lineSum <- diff
// countInside += diff
}
if specialBorder.border == Vertical {
bordersCrossed += 1
}
prevBorder = specialBorder
}
wg.Done()
}(row)
}
<-done
return result
}
// func (f *Field) digInsides() (countInside int) {
// for row := f.MinRow; row < f.MaxRow; row++ {
// if row % 10000 == 0 {
// log.Printf("processed rows %d out of %d", row, f.MaxRow)
// }
// isInside := false
// seenUp, seenDown := false, false // for detecting L---7 walls
// for col := f.MinCol; col < f.MaxCol; col++ {
// // TODO next optimization - for each row, store indices of cols with border cells
// // so that count of inside would be done by many at a time
// rightCellIsDug := f.isCellDug(row, col+1)
// if f.isCellDug(row, col) {
// upCellIsDug := f.isCellDug(row-1, col)
// downCellIsDug := f.isCellDug(row+1, col)
// if !rightCellIsDug {
// if (upCellIsDug && seenDown) || (downCellIsDug && seenUp) {
// isInside = !isInside
// }
// seenUp, seenDown = false, false
// }
// } else {
// // not a dug out cell, maybe inside and needs to be dug out
// if isInside {
// // f.Cells[Coord{col, row}] = &Cell{
// // ToBeDug: true,
// // }
// countInside += 1
// // log.Printf("tick count inside for %d %d", row, col)
// // cellPtr.ToBeDug = true
// }
// if rightCellIsDug {
// seenUp = f.isCellDug(row-1, col+1)
// seenDown = f.isCellDug(row+1, col+1)
// }
// }
// }
// }
// return
// }
func (f *Field) isCellDug(row, col int) bool {
cell := f.Cells[Coord{col, row}]
return cell != nil && cell.IsDug
}
func WriteToFile(filename string, content string) {
fileBorder, err := os.Create(filename)
if err != nil {
panic(err)
}
defer func() {
if err := fileBorder.Close(); err != nil {
panic(err)
}
}()
fileBorder.WriteString(content)
}

45
day18/notes.org Normal file
View File

@ -0,0 +1,45 @@
#+title: Notes
* part 2 and i'm struggling.
maybe i need to mark 'inside' cells while i dig?
i don't know which is 'outside' from the getgo?
if i mark 'all the rightside', will that help to calculate inside?
* well, if we dont' have instruction with steps:1 i can just count points above and belove the line
without more complicated things
just count 'seenUp' and 'seenDown' if equal - then we changed side
and - we shouldn't have 'step1' because all numbers are soooo big.
ok. let's do that? with maps of cols.
** CANCELLED add map[row]map[col]any
** CANCELLED separate method to set it up after we have all of the BorderCellCols
** CANCELLED during digInsides on each consecutive - check above and below and count
when there's a jump - compare counts, to make decision on whether to switch 'isInside'
** no. just because they are long doesn't mean they won't ever get one near another
* another idea is to save | and corners, as if we're going from left to right
this seems reasonable.
** DONE i guess []SpecialSymbol which has Col and Symbol
** DONE no, let's make it map. yes will have to init, but yuck anyway
** TODO then different logic on border building.
if U \ D - on all but last add '|'
on last - calc with the next turn, what should be saved 'as if traversing from the left'
for L \ R - on last - calc what the turn was
** TODO !! between last and first movement the corner is unknown.
so, copy the first instruction to the end?
** moment of hope.
my calculation for example input for part 2
day18 result: 952408144115
952408144115
*** YES.
*** about 1M for 4 minutes
** so, my input is ~16M rows
3.5 seconds per 10k
** well, maybe i can parallel.
*** parallel example
day18 result: 952407566854
*** and with separate done channel from the summing goroutine
952408144115
**** YES

View File

@ -3,12 +3,12 @@ package main
import (
"log"
"sunshine.industries/aoc2023/day16"
"sunshine.industries/aoc2023/day18"
)
func main() {
log.Print("> starting run:")
result := day16.Run()
log.Printf("\n\nday16 result: %d\n****\n", result)
result := day18.Run()
log.Printf("\n\nday18 result: %d\n****\n", result)
}