57 lines
1.4 KiB
GDScript
57 lines
1.4 KiB
GDScript
extends Control
|
|
|
|
func _on_quit_pressed() -> void:
|
|
get_tree().change_scene_to_file("res://Scenes/2024/2024.tscn")
|
|
|
|
@onready var input_edit: TextEdit = $Input
|
|
@onready var error_label: Label = $Error
|
|
@onready var part_1_out: Label = $Part1Out
|
|
@onready var part_2_out: Label = $Part2Out
|
|
|
|
func _on_go_pressed() -> void:
|
|
var input_text = input_edit.text
|
|
part_1_out.text = part_1(input_text)
|
|
part_2_out.text = part_2(input_text)
|
|
|
|
func set_error(msg: String) -> void:
|
|
error_label.text = msg
|
|
|
|
func part_1(input_text: String) -> String:
|
|
var lines = split_lines(input_text)
|
|
if lines == []:
|
|
return ""
|
|
return str(get_diff(lines[0], lines[1]))
|
|
|
|
func split_lines(input: String) -> Array[Array]:
|
|
var lines = input.split("\n")
|
|
var left = [];
|
|
var right = [];
|
|
for line in lines:
|
|
var parts = line.split(" ")
|
|
if parts.size() != 2:
|
|
set_error("invalid input!")
|
|
return []
|
|
left.append(parts[0] as int)
|
|
right.append(parts[1] as int)
|
|
return [left, right]
|
|
|
|
func get_diff(left: Array, right: Array) -> int:
|
|
left.sort()
|
|
right.sort()
|
|
var diff = 0
|
|
for i in range(left.size()):
|
|
diff += abs(right[i] - left[i])
|
|
return diff
|
|
|
|
func part_2(input_text: String) -> String:
|
|
var lines = split_lines(input_text)
|
|
if lines == []:
|
|
return ""
|
|
return str(get_similarity(lines[0], lines[1]))
|
|
|
|
func get_similarity(left: Array, right: Array) -> int:
|
|
var similarity = 0
|
|
for n in left:
|
|
similarity += n * right.count(n)
|
|
return similarity
|