forked from vlang/adventofcode
-
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.
Showing
4 changed files
with
114 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,65 @@ | ||
module main | ||
|
||
import os | ||
import math | ||
|
||
const lr_directions = { | ||
`L`: 0 | ||
`R`: 1 | ||
} | ||
|
||
fn main() { | ||
part_one := solve_day_eight(os.read_file('haunted_wasteland-part1.input')!.split('\n\n'), | ||
false) | ||
part_two := solve_day_eight(os.read_file('haunted_wasteland-part2.input')!.split('\n\n'), | ||
true) | ||
|
||
println('Part 1: ${part_one}') | ||
println('Part 2: ${part_two}') | ||
} | ||
|
||
fn solve_day_eight(parts []string, part_two bool) i64 { | ||
instructions := parts[0].runes().map(lr_directions[it]) | ||
node_list := parts[1].split_into_lines() | ||
.map(it.split_any(' =(,)').map(it.trim_space()).filter(it != '')) | ||
|
||
mut nodes := map[string][]string{} | ||
for node in node_list { | ||
nodes[node[0]] = node[1..] | ||
} | ||
|
||
if !part_two { | ||
return find_path_length('AAA', nodes, instructions) | ||
} | ||
|
||
mut result := i64(0) | ||
mut starts := node_list.filter(it[0][2] == `A`).map(it[0]) | ||
for start in starts { | ||
path_len := find_path_length(start, nodes, instructions) | ||
|
||
if result == 0 { | ||
result = path_len | ||
} else { | ||
result = math.lcm(result, i64(path_len)) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
fn find_path_length(start string, nodes map[string][]string, instructions []int) int { | ||
mut current := start | ||
mut path_length := 0 | ||
outer: for { | ||
for instruction in instructions { | ||
current = nodes[current][instruction] | ||
path_length += 1 | ||
|
||
if current[2] == `Z` { | ||
break outer | ||
} | ||
} | ||
} | ||
|
||
return path_length | ||
} |
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,45 @@ | ||
module main | ||
|
||
import arrays | ||
import os | ||
|
||
fn main() { | ||
lines := os.read_lines('mirage_maintenance.input')!.map(it.split(' ').map(it.int())) | ||
|
||
mut part_one := 0 | ||
mut part_two := 0 | ||
|
||
for line in lines { | ||
mut diffs := arrays.window(line, size: 2).map(it[1] - it[0]) | ||
mut first_numbers := [line[0], diffs[0]] | ||
part_one += line.last() + diffs.last() | ||
|
||
for { | ||
diffs = arrays.window(diffs, size: 2).map(it[1] - it[0]) | ||
part_one += diffs.last() | ||
|
||
if diffs.all(it == 0) { | ||
break | ||
} | ||
|
||
first_numbers << diffs[0] | ||
} | ||
|
||
part_two += foldr(first_numbers, 0, fn (acc int, value int) int { | ||
return value - acc | ||
}) | ||
} | ||
|
||
println('Part 1: ${part_one}') | ||
println('Part 2: ${part_two}') | ||
} | ||
|
||
fn foldr[T, R](array []T, init R, fold_op fn (acc R, elem T) R) R { | ||
mut value := init | ||
|
||
for i := array.len - 1; i >= 0; i-- { | ||
value = fold_op(value, array[i]) | ||
} | ||
|
||
return value | ||
} |
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,2 @@ | ||
Part 1: 2 | ||
Part 2: 6 |
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,2 @@ | ||
Part 1: 114 | ||
Part 2: 2 |