Skip to content

Commit aa5d435

Browse files
committed
day 8 part 1 & 2
1 parent 0be713b commit aa5d435

File tree

6 files changed

+554
-0
lines changed

6 files changed

+554
-0
lines changed

day08.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#! /bin/bash
2+
3+
CHALLENGE=day08
4+
5+
go build -o $(pwd)/bin/$CHALLENGE ./$CHALLENGE/*.go
6+
7+
echo "Tests"
8+
cat $CHALLENGE/input_test_01.txt | bin/$CHALLENGE
9+
10+
echo "Solution"
11+
cat $CHALLENGE/input.txt | bin/$CHALLENGE

day08/input.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
200210101302123001201232003214413304042424024222111455334421333412020113121332322101331130101222121
2+
120100002010203210130421301201404231101254535242322112224524254031333224202413331320010302202022220
3+
001200100200133300103241144403033124133341435524324143423341133252144040000423121430200100022211211
4+
000012321202320024030320421230202213131514345325422513145221212451233202023402411040301113132131121
5+
111103022312211340103042313022513413532253114352113252432213335135141552003023313142423321320321212
6+
012112331020102411442404102132241553154421513555415233312432515445154422402444314033003303323320102
7+
200110201230142322043222345241215442224221523534521123322555135454422121142320102300340211100120310
8+
120012121330311223132012342543213553334431544155643463443115441515143121132442433102420433301221030
9+
232202001121031432403445341332243414414443326545556526423425612114153351234111432130314202330220010
10+
133033302004430221300431213144135422424546344243432363665246244341243432352514332142044434023131003
11+
323030003440212122341123551231453352464334362343245665335563565343421512431425213111441304340011231
12+
003000034413321402514254435224115366533425244663634336625232232553226312522142535212414040004113221
13+
203203234020411021411532552144456644553434362455342664624546642245543563524451523133334201141411113
14+
103032442424101341214421543433253563535422262333555222632224462463423526635422354541113034433423000
15+
020133314440123532124351233325245665424345546323747756264656566346366665333444354252523314300303020
16+
303102341103425544443131253325266522334544545666744335557473366256323634266213415324324341134211112
17+
023044020203451414535535625633224445223364357435363343536455577666362444542245113553311434011140201
18+
130031020304324235255516652533544625454347643363434554557436654343442356543323225524232341230421320
19+
220403302205252431242554332553635575345744474647573376655365465333365246645632425211423335322131241
20+
214400202003451322143346242334324356456753434655775663756755654555657245563342333124515412311131042
21+
144333342021131124452334346355653767663565565443573444757577736363654665266666525633314514341134041
22+
111433243134224443344522523636635556736633645434676475377437457745566744646666544632522235344014021
23+
421102342525325521256266235434736567476654664744455647574564433767653357626264235361354533341223334
24+
030103204353142134336562363447366767454375766855756788856466566674566447433523254456444314525331002
25+
410404421315115456635435323737634573668554668666466474777667486334467637567356564452622223212300033
26+
012140231351312622653242266664756743747655464548677545754787855754646777456542663563451151125130001
27+
122202541435443225443523376367336378575474446665666578564485767586653557636553243543226514123433024
28+
322341213145516625463356665437453687675788656768568586885644456856643777435654242663525154214512210
29+
140223342343464363346556333753354858857777544686557677466886555488585473373334765233552623543312200
30+
422403124152143243445554363655577858878487546898958587796787475657654747656366532662626445424424410
31+
321231255223524234247766447646866656646454795565859795788557647554485844673466335434256341522445230
32+
434145334313623446664556646655886667557495589675986998765976684664875654474437775454342445551523513
33+
433432522546645452345446344376857768848989787655576756768988889768856644743374354326655353222112422
34+
420512322415655344574666457745755754768778785979569655887679799664764746786677655366663463434125353
35+
124155335466242262756735466446866676986698997576575557966855895697587767454454543472442666623435524
36+
415345153363443642766655644464854747967899588766957797576697788779586574578644737774624632644545113
37+
441333524323362443745375688445665755769658789797769878578585579797547585786676677364322333511241423
38+
434241414244556353633455448684455597576765697869997689879755769799778646786856633667554436244553435
39+
245314523664342366643466667485888767568897999988699997889967759988686546445685776346654553265354523
40+
321432211425653366363657657447568665877957979987786779697868577888595675855456734636524456425431142
41+
033422313632342655364345567644755898785796699996888776886796657757797857688876376354363532536232314
42+
052242355542323647576334458576689579586968867996689878797799969587659875547656644765646363224253444
43+
234434342653226546436658855775896968867876797997996768779677667699889796745768337366436634632245213
44+
445222213235346735753667755676669878598898697986687998696976996987769858656476757667332332625351514
45+
435355546442363575345376588657989988589877788898987788998987888969858697674677876744344654353312151
46+
133553556352633665357787686775799577779997878787787897797699998878965787455885663557545355624324115
47+
545233354544544435573376578458655587598789677877787899897869896658788766588656837747764363655341521
48+
211555453542466735366787685658866856979676887788987997989769978968996968546685834576542622243213155
49+
144113123264426777463777488587699869767977889987787997788887696979756858576885677473635653233543243
50+
311313235543366635754558457576795998769778679778889889889987668888968988774448543757733325643231422
51+
443355342254425446767468656476595865887986787897998879979969967775865587546587644777655235234324344
52+
442443355324564334365767887885668589686866989997898887797879967668978575855866836656334552623632351
53+
413433144665547465436464847768768777996977968777788977797977679895685768676556634643436422334442455
54+
453142566345254453333444885587565578677688779977889999789888796877778557847675867477652535546215422
55+
151332265265444777364585745778956687969888787989789897978868788787686655764465553577745632625231455
56+
445223254646322766445774546689887968566967668798798897976876999668956869458456654344465452425345523
57+
354122314463434747436654644484979696677686898978887878797976779669677694454485555635752664656513133
58+
012232355266625635373378685656767767887668769986988989987778777769685698467858437754343553255322444
59+
024555213224464774353435485656687879797989768686778867676969798596899575656786466333455553355345234
60+
132432444233664544676735848585667856997869777886887797667989767578765688576686457343436442546533142
61+
444143435624244477333448558448667577665667766788798979786987858959699677748784665774745235262414311
62+
231541231646652273467743578658756967656599897986666898787998598868878558884847373763743436333442222
63+
452115141256244235637554556584849575796768969699769887767797597866989785545764765437552564345131455
64+
425244513366263336575446588857846976888767658868676696667958899568857767458773744637333452423533532
65+
105131435443626324576435676555478786567757999689999668897789599599856875674774656477364623225123151
66+
032315232453426453465765574644575446896598678899586759777588775565758668474745575344566425615355433
67+
414341314426235264647543645746457657967695558965975769898785985654576867658657635475666624352134144
68+
100323311242236242255347744575476776858879688698797878579969975874748675444573747344653555211352221
69+
014311441544336355454575445686757688689666888758996965685596966444845578855576543546465554243154142
70+
342254542123656656546356366778675574845697675569556687688586558646447667434456665352633422255244414
71+
211453143353434566563465675745864885674687588998996685977676784567474566434544466522533343353114231
72+
313022332133466332336477634667465867888785648576775865668487487544484845366535575442326624351431012
73+
114211155121535434243656644454647447848458486648876856858767458446765544736737656643222525432544232
74+
010122525344156263354227756636434587458665676586887787858748554686666673755577552352343354534152040
75+
412321535231542523462334653374657358685586488785485678564864446878734675557735423665442154552342011
76+
104424333415333444626345536643374555557465464777678466847574454875666744474766234464241242553110441
77+
124112314331255362622446225633636335345644866876474848854678487634655646573245644334324331251444400
78+
312000445323443215433555564337345455776744675674478888474578745344363573744242633426233313351422233
79+
430022234512251423424326264637465355355746637447648757857473743746654776525262553642221524414041323
80+
233102134414232412332356226435465646465443546335436365365755446655434343246552323324433143130002111
81+
111404313153153442123666355224747746356376564475444457353545546774737575424345446353354342244313412
82+
234334440424325351212523232526466575445565545647676363335445774536647332224524363441415351434423203
83+
123130340344441323432464644565445455655737653455763575457374566467753266262333355253144434121341413
84+
032133414041244541233153446223224334665566654364436733465455436362242424353334354423444542342323120
85+
331120000421342243325115456255364522626457536537755343436545747263466663636524533235411311121411332
86+
332230404213303353133111256254363655255434744674474754434646343454354433433515513135334442222004010
87+
202121313223222511453512322255566444565544643556454452443454524223535655434354335552444042314134332
88+
213300030010040331541141441242645424354334635234565662362526354224246533454523242512414430303043302
89+
303300002321212004244535244451242654252646636554662636263664646235263231315331142352102120130122303
90+
331101014024440131342122251123415223633335635453354263636226636262256541131443215354304023243302102
91+
331202033431032221244232125455313233642635432336534464333635335635423524411525215033323313120333013
92+
033030202002100313111523432311242321352266332543436322536365535415434245433251242122313014031212313
93+
222211100333332310242343252221251543414443226355325632364262625135251521332232522230244440201332023
94+
000203310213011213412344315255131512335223453552465335425413252322422253334440310223340313231332020
95+
011332231323130141310431031222132533232453353124412341254122255424331153344341340113311310010033130
96+
010021033100310312123242131122524221555411455523421253253142545515323423322444300142424333201332021
97+
110101220202201114310003401420513324334525253112533435355244442344311112421320024234333132222021000
98+
020011123333222200003244032430305123255531342321413242534522552125444313014002424233330331130222200
99+
100220000312023302114114440201033304323431155212155522541415515211123243000444221343002001121202102

day08/input_test_01.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
30373
2+
25512
3+
65332
4+
33549
5+
35390

day08/iterate.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"io"
6+
)
7+
8+
type Step int
9+
10+
const (
11+
Continue Step = iota
12+
Break
13+
)
14+
15+
// IterateLines iterates over lines in a reader
16+
// and calls the provided function for each line.
17+
// if the function returns false, the iteration stops.
18+
func IterateLines(r io.Reader, f func(s string) Step) (err error) {
19+
scanner := bufio.NewScanner(r)
20+
for scanner.Scan() {
21+
if c := f(scanner.Text()); c == Break {
22+
break
23+
}
24+
}
25+
return scanner.Err()
26+
}

day08/main.go

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"strconv"
8+
)
9+
10+
type Grid [][]int
11+
12+
func (g Grid) Get(x, y int) int { return g[y][x] }
13+
14+
func main() {
15+
g, err := CreateGrid(os.Stdin)
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
count, err := CountVisibleTrees(g)
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
fmt.Printf("part 1: %d\n", count)
26+
27+
score, err := HighestScenicScore(g)
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
fmt.Printf("part 2: %d\n", score)
33+
}
34+
35+
func CreateGrid(input io.Reader) (grid Grid, err error) {
36+
var y int
37+
err = IterateLines(input, func(s string) Step {
38+
if grid == nil {
39+
grid = Grid{}
40+
41+
for i := 0; i < len(s); i++ {
42+
grid = append(grid, make([]int, len(s)))
43+
}
44+
}
45+
46+
for x, c := range s {
47+
if v, err := strconv.Atoi(string(c)); err == nil {
48+
grid[y][x] = v
49+
} else {
50+
panic(err)
51+
}
52+
}
53+
54+
y++
55+
56+
return Continue
57+
})
58+
59+
return
60+
}
61+
62+
// CountVisibleTrees counts the number of visible trees
63+
// in the grid.
64+
func CountVisibleTrees(grid Grid) (count int, err error) {
65+
grideSize := len(grid)
66+
67+
count = (grideSize * 4) - 4
68+
69+
// iterate trees (don't do the edges)
70+
for y := 1; y < grideSize-1; y++ {
71+
for x := 1; x < grideSize-1; x++ {
72+
if IsVisibleTree(grid, x, y) {
73+
count++
74+
}
75+
}
76+
}
77+
78+
return
79+
}
80+
81+
func IsVisibleTree(grid Grid, x, y int) (visible bool) {
82+
treeHeight := grid.Get(x, y)
83+
visibleSides := 0
84+
85+
callBack := func(xb, yb int) Step {
86+
if t := grid.Get(xb, yb); treeHeight > t {
87+
// check if edge
88+
if IsEdge(grid, xb, yb) {
89+
// if edge then it's visible
90+
visibleSides++
91+
return Break
92+
}
93+
94+
return Continue
95+
}
96+
97+
return Break
98+
}
99+
100+
// check up
101+
WalkGrid(grid, x, y-1, 0, -1, callBack)
102+
// check down
103+
WalkGrid(grid, x, y+1, 0, 1, callBack)
104+
// check left
105+
WalkGrid(grid, x-1, y, -1, 0, callBack)
106+
// check right
107+
WalkGrid(grid, x+1, y, 1, 0, callBack)
108+
109+
return visibleSides >= 1
110+
}
111+
112+
func HighestScenicScore(grid Grid) (score int, err error) {
113+
grideSize := len(grid)
114+
115+
for y := 1; y < grideSize; y++ {
116+
for x := 1; x < grideSize; x++ {
117+
if s := ScenicScore(grid, x, y); s > score {
118+
score = s
119+
}
120+
}
121+
}
122+
123+
return
124+
}
125+
126+
func IsEdge(grid Grid, x, y int) bool {
127+
return x == 0 || y == 0 || x == len(grid)-1 || y == len(grid)-1
128+
}
129+
130+
func ScenicScore(grid Grid, x, y int) (score int) {
131+
treeHeight := grid.Get(x, y)
132+
133+
lineOfSights := []int{}
134+
lineOfSight := 0
135+
136+
// stop if you reach an edge or at the first tree that is the same height or taller than the tree under consideration.
137+
callBack := func(xb, yb int) Step {
138+
lineOfSight++
139+
140+
if t := grid.Get(xb, yb); treeHeight > t {
141+
return Continue
142+
}
143+
144+
return Break
145+
}
146+
147+
// check up
148+
WalkGrid(grid, x, y-1, 0, -1, callBack)
149+
lineOfSights = append(lineOfSights, lineOfSight)
150+
lineOfSight = 0
151+
152+
// check left
153+
WalkGrid(grid, x-1, y, -1, 0, callBack)
154+
lineOfSights = append(lineOfSights, lineOfSight)
155+
lineOfSight = 0
156+
157+
// check right
158+
WalkGrid(grid, x+1, y, 1, 0, callBack)
159+
lineOfSights = append(lineOfSights, lineOfSight)
160+
lineOfSight = 0
161+
162+
// check down
163+
WalkGrid(grid, x, y+1, 0, 1, callBack)
164+
lineOfSights = append(lineOfSights, lineOfSight)
165+
lineOfSight = 0
166+
167+
// fmt.Printf("x: %d, y: %d, lineOfSights: %v \n", x, y, lineOfSights)
168+
169+
score = lineOfSights[0]
170+
for _, l := range lineOfSights[1:] {
171+
score *= l
172+
}
173+
174+
return
175+
}
176+
177+
func WalkGrid(grid Grid, x, y int, dX, dY int, f func(x, y int) Step) (err error) {
178+
for {
179+
if x < 0 || y < 0 || x >= len(grid) || y >= len(grid) {
180+
break
181+
}
182+
183+
if c := f(x, y); c == Break {
184+
break
185+
}
186+
187+
x += dX
188+
y += dY
189+
}
190+
191+
return
192+
}

0 commit comments

Comments
 (0)