Created
December 20, 2022 23:08
-
-
Save ap29600/57831014abf9d55882eb9cacba433ca7 to your computer and use it in GitHub Desktop.
Advent of Code 2022 day 20 in Odin
This file contains hidden or 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
package main | |
import "core:os" | |
import "core:fmt" | |
import "core:slice" | |
import "core:mem" | |
import "core:strings" | |
import "core:strconv" | |
mix :: proc (numbers: []int, mix: int) -> (result: int) { | |
n := len(numbers) | |
p := make([]int, n) | |
defer delete(p) | |
for it, i in &p {it = i} | |
for _ in 0 ..< mix { | |
for i in 0 ..< n { | |
start, _ := slice.linear_search(p, i) | |
amount := numbers[i] | |
end := (start + amount) %% (n-1) | |
if end == start {continue} | |
if (start < end) { | |
tmp := p[start] | |
mem.copy(&p[start], &p[start+1], size_of(int) * (end - start)) | |
p[end] = tmp | |
} else { | |
tmp := p[start] | |
mem.copy(&p[end+1], &p[end], size_of(int) * (start - end)) | |
p[end] = tmp | |
} | |
} | |
} | |
zero, _ := slice.linear_search(numbers, 0) | |
zero_pos, _ := slice.linear_search(p, zero) | |
for it in ([]int{1e3, 2e3, 3e3}) { | |
result += numbers[p[(zero_pos + it)%n]] | |
} | |
return | |
} | |
main :: proc () { | |
input, ok := os.read_entire_file("input.txt") | |
lines := strings.split(string(input), "\n", context.temp_allocator) | |
lines = lines[:len(lines) - 1] | |
numbers := slice.mapper(lines, strconv.atoi) | |
fmt.println("part 1:", mix(numbers, 1)) | |
for it in &numbers {it *= 811589153} | |
fmt.println("part 2:", mix(numbers, 10)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment