Created
December 1, 2021 09:48
-
-
Save MarioLiebisch/ac9dfe3e321304c4d944101af3f8a592 to your computer and use it in GitHub Desktop.
Solution for Advent of Code 2021 day 1 using GDScript
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
# Execute with: | |
# godot --no-window -s aoc2021-day1.gd | |
extends SceneTree | |
func read_ints(file: String) -> Array: | |
var ints = [] | |
var f = File.new() | |
f.open(file, File.READ) | |
if f.is_open(): | |
while not f.eof_reached(): | |
ints.append(int(f.get_line())) | |
return ints | |
func count_increments(data: Array) -> int: | |
var count = 0 | |
for i in len(data) - 1: | |
if data[i] < data[i + 1]: | |
count += 1 | |
return count | |
func merge_window(data: Array, width: int) -> Array: | |
var merged = [] | |
for i in len(data) - width + 1: | |
var sum = 0 | |
for j in range(width): | |
sum += data[i + j] | |
merged.append(sum) | |
return merged | |
func _init() -> void: | |
var data_sample: Array = read_ints("./data/sample-1.txt") | |
var data_input: Array = read_ints("./data/input-1.txt") | |
print("Solution 1 (sample): %d" % count_increments(data_sample)) | |
print("Solution 1 (input): %d" % count_increments(data_input)) | |
print("Solution 2 (sample): %d" % count_increments(merge_window(data_sample, 3))) | |
print("Solution 2 (input): %d" % count_increments(merge_window(data_input, 3))) | |
quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment