From: Brendan Hansen Date: Tue, 15 Dec 2020 15:33:36 +0000 (-0600) Subject: added day 15 X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=3f3662cfe291d66ab0b8b89f2c33b18a85a258f1;p=onyx-aoc-2020.git added day 15 --- diff --git a/day15.onyx b/day15.onyx new file mode 100644 index 0000000..923bc05 --- /dev/null +++ b/day15.onyx @@ -0,0 +1,47 @@ +#include_file "core/std/wasi" + +use package core +use package core.string.reader as reader + +spoken_times :: struct { + recent : u32 = 0; + previous : u32 = 0; +} + +main :: proc (args: [] cstr) { + contents := file.get_contents("input/day15.txt"); + defer string.free(contents); + + file := reader.make(contents); + + nums : map.Map(u32, spoken_times); + map.init(^nums, 32767); + defer map.free(^nums); + + turn := 1; + last_num := 0; + while !reader.empty(^file) { + n := reader.read_u32(^file); + reader.skip_bytes(^file); + + map.put(^nums, n, spoken_times.{ recent = turn }); + turn += 1; + last_num = n; + } + + while turn != 30000001 { + st := map.get(^nums, last_num, spoken_times.{}); + + if st.previous == 0 do last_num = 0; + else do last_num = st.recent - st.previous; + + st = map.get(^nums, last_num, spoken_times.{}); + st.previous = st.recent; + st.recent = turn; + map.put(^nums, last_num, st); + + turn += 1; + } + + printf("30000000th: %i\n", last_num); +} \ No newline at end of file diff --git a/input/day15.txt b/input/day15.txt new file mode 100644 index 0000000..408f526 --- /dev/null +++ b/input/day15.txt @@ -0,0 +1 @@ +1,20,8,12,0,14 \ No newline at end of file