-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b68409c
commit b9b9b4d
Showing
5 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package main | ||
|
||
import "core:fmt" | ||
import "core:strings" | ||
|
||
D10P1 :: proc() { | ||
input_string := #load("inputs/10.txt", string) | ||
lines := strings.split(input_string, "\n", context.temp_allocator) | ||
|
||
sum_trailhead_scores := get_sum_trailhead_scores(lines, false) | ||
fmt.printf("Sum of all trailhead scores: %d\n", sum_trailhead_scores) | ||
} | ||
|
||
get_sum_trailhead_scores :: proc(input: []string, part2: bool) -> int { | ||
trailmap, trailheads := parse_trailmap(input) | ||
defer delete(trailmap) | ||
defer delete(trailheads) | ||
sum: int | ||
for trailhead in trailheads { | ||
sum += get_trailhead_score(trailmap, trailhead, len(input[0]), part2) | ||
} | ||
return sum | ||
} | ||
|
||
parse_trailmap :: proc(input: []string) -> (trailmap: [dynamic]int, trailheads: [dynamic]int) { | ||
max_x := len(input[0]) | ||
for line, y in input { | ||
if line == "" { | ||
continue | ||
} | ||
for char, x in line { | ||
// convert rune to int | ||
append(&trailmap, auto_cast (char - '0')) | ||
|
||
if char == '0' { | ||
append(&trailheads, y * max_x + x) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
get_trailhead_score :: proc( | ||
trailmap: [dynamic]int, | ||
trailhead: int, | ||
map_width: int, | ||
part2: bool, | ||
) -> int { | ||
score := 0 | ||
queue := [dynamic]int{trailhead} | ||
visited := map[int]bool { | ||
trailhead = true, | ||
} | ||
defer delete(queue) | ||
defer delete(visited) | ||
|
||
for len(queue) > 0 { | ||
coord := pop(&queue) | ||
|
||
if part2 { | ||
visited[coord] = true | ||
} | ||
|
||
neighbours := []int { | ||
coord - 1, // left | ||
coord + 1, // right | ||
coord - map_width, // up | ||
coord + map_width, // down | ||
} | ||
|
||
for neighbour in neighbours { | ||
if neighbour < 0 || neighbour >= len(trailmap) { | ||
continue | ||
} | ||
if (coord % map_width == 0 && neighbour == coord - 1) || | ||
(coord % map_width == map_width - 1 && neighbour == coord + 1) { | ||
continue | ||
} | ||
if trailmap[neighbour] - 1 != trailmap[coord] { | ||
continue | ||
} | ||
if !part2 && visited[neighbour] { | ||
continue | ||
} | ||
if trailmap[neighbour] == 9 { | ||
score += 1 | ||
if !part2 { | ||
visited[neighbour] = true | ||
} | ||
continue | ||
} | ||
append(&queue, neighbour) | ||
} | ||
} | ||
return score | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import "core:fmt" | ||
import "core:strings" | ||
|
||
D10P2 :: proc() { | ||
input_string := #load("inputs/10.txt", string) | ||
lines := strings.split(input_string, "\n", context.temp_allocator) | ||
|
||
sum_trailhead_scores := get_sum_trailhead_scores(lines, true) | ||
fmt.printf("Sum of all trailhead scores: %d\n", sum_trailhead_scores) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
7651234510103430129656654566543012101234544567676545 | ||
8340987643212345038743783087452143450925655438585432 | ||
9205676544569416545032192196567854567810710129891201 | ||
0114323456678507896123010105432945498789878778100323 | ||
1023210987897612387654981234941834354308909689011414 | ||
4321301010767887434767876547870921267210218574326505 | ||
5650452121456996545897987676589810898876123465467876 | ||
4781965432348765698018734585476701017965019894894966 | ||
9890874321089450367129653090365432676874320767743987 | ||
0781103016567321458934532101270121085401221678652156 | ||
1692012923498212367012983501989789098320034569565043 | ||
2543127874321003450143477652876652167012123430676530 | ||
3432234566543234321894568943905443256587656321089421 | ||
4391001057872145430765410878912321101498943243989321 | ||
0189432326981056741696321767010630122367050112078930 | ||
3276593410456747890781011054321545012454198703163210 | ||
4566782561267832781652322123765436723543217654254301 | ||
9687601670358901692343123098890127834984503234348911 | ||
8794512385443210543510004567734323941076678105067410 | ||
0123410496554381235671212321065419452345549876154323 | ||
4320321587010190134587389412176108760632438963235432 | ||
5411289698121285421098498503489954321541328754556741 | ||
6700348787232376332567567694567869325670019669430890 | ||
7889465676543101221410698587645678410581204578521321 | ||
4976534587894123430321587450784349525890323467670410 | ||
3098621298765036541543676521698237656765412896789567 | ||
2180590178981047897632101434532118949896106701693458 | ||
3671287065432156788912012321089001232787765432542109 | ||
4576396101276549874301015401672101301289850343432101 | ||
0985405234387632965432109872543985423477891296508765 | ||
1256714985496101876343234563837676510566780187219656 | ||
0349823076567987654894303474988543412365443210389345 | ||
1256712187458978703765412985679812305430301223478234 | ||
0210603492324569812654501874876703276321212984560143 | ||
4389896501215498701967012565945432181430365806543231 | ||
5676287618900398632878123474236701090568456717632100 | ||
5432109807651217540169010089147898101279894328986521 | ||
6701018712349106543254322128056763012389765010147432 | ||
7891127689678100145101013438987652121450110981238901 | ||
8980334574549233236232304587012343210969231476545432 | ||
8901265463032147867347212698743476521878542345696543 | ||
7898378352104056998478923787654389430677653218787656 | ||
6987499243693456787567014568785203434587965109690145 | ||
5496587156782321893459985439690112521097874065432234 | ||
0381016045071040134578376326541045671010123678321101 | ||
1272325432101056923667289017432434982234234349830765 | ||
0365430340342347810756108128921125543145641256545854 | ||
6674321201259659876843201234210014698076450127894903 | ||
7889210398768760145998910165436786787189361898765212 | ||
6901165409694321234107323876325495698321276345654303 | ||
5432076210585012321236454989414324321230985432783212 | ||
4345189823476523410345567898701015450101234321098101 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import "core:fmt" | ||
import "core:testing" | ||
|
||
trailmap_example :: []string { | ||
"89010123", | ||
"78121874", | ||
"87430965", | ||
"96549874", | ||
"45678903", | ||
"32019012", | ||
"01329801", | ||
"10456732", | ||
} | ||
|
||
@(test) | ||
d10p1 :: proc(t: ^testing.T) { | ||
want :: 36 | ||
got := get_sum_trailhead_scores(trailmap_example, false) | ||
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want)) | ||
|
||
free_all() | ||
} | ||
|
||
@(test) | ||
d10p2 :: proc(t: ^testing.T) { | ||
want :: 81 | ||
got := get_sum_trailhead_scores(trailmap_example, true) | ||
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want)) | ||
|
||
free_all() | ||
} | ||
|
||
@(test) | ||
test_parse_trailmap :: proc(t: ^testing.T) { | ||
got_map, got_heads := parse_trailmap(trailmap_example) | ||
want_length := 64 | ||
want_3_3 := 4 | ||
want_7_4 := 6 | ||
max_x := 8 | ||
want_heads_length := 9 | ||
|
||
testing.expect( | ||
t, | ||
len(got_map) == want_length, | ||
fmt.aprintf("Got: %v | Want: %v", len(got_map), want_length), | ||
) | ||
testing.expect( | ||
t, | ||
len(got_heads) == want_heads_length, | ||
fmt.aprintf("Got: %v | Want: %v", len(got_heads), want_heads_length), | ||
) | ||
testing.expect( | ||
t, | ||
got_map[max_x * 3 + 3] == want_3_3, | ||
fmt.aprintf("Got: %v | Want: %v", got_map[3 + 3], want_3_3), | ||
) | ||
testing.expect( | ||
t, | ||
got_map[max_x * 7 + 4] == want_7_4, | ||
fmt.aprintf("Got: %v | Want: %v", got_map[7 + 4], want_7_4), | ||
) | ||
|
||
free_all() | ||
} |