Skip to content

Commit

Permalink
hillerstorm day 8 and 9 (vlang#44)
Browse files Browse the repository at this point in the history
* hillerstorm day 8

* hillerstorm day 9
  • Loading branch information
hillerstorm authored Dec 11, 2023
1 parent 487c28d commit b7fd843
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
65 changes: 65 additions & 0 deletions 2023/08/hillerstorm.v
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
}
45 changes: 45 additions & 0 deletions 2023/09/hillerstorm.v
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
}
2 changes: 2 additions & 0 deletions known_outputs/2023/08/hillerstorm.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Part 1: 2
Part 2: 6
2 changes: 2 additions & 0 deletions known_outputs/2023/09/hillerstorm.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Part 1: 114
Part 2: 2

0 comments on commit b7fd843

Please sign in to comment.