init and 2024 day 1

This commit is contained in:
tristan 2024-12-04 23:02:57 +00:00
commit 455ea925eb
11 changed files with 266 additions and 0 deletions

56
Scenes/2024/01.gd Normal file
View file

@ -0,0 +1,56 @@
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

85
Scenes/2024/01.tscn Normal file
View file

@ -0,0 +1,85 @@
[gd_scene load_steps=2 format=3 uid="uid://cqbt3uasjeh53"]
[ext_resource type="Script" path="res://Scenes/2024/01.gd" id="1_qvnl7"]
[node name="Control" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_qvnl7")
[node name="Input" type="TextEdit" parent="."]
layout_mode = 0
offset_left = 11.0
offset_top = 95.0
offset_right = 275.0
offset_bottom = 253.0
[node name="Label" type="Label" parent="."]
layout_mode = 0
offset_left = 16.0
offset_top = 58.0
offset_right = 108.0
offset_bottom = 81.0
text = "Enter input:"
[node name="Go" type="Button" parent="."]
layout_mode = 0
offset_left = 26.0
offset_top = 284.0
offset_right = 60.0
offset_bottom = 315.0
text = "Go!"
[node name="Quit" type="Button" parent="."]
layout_mode = 0
offset_left = 28.0
offset_top = 472.0
offset_right = 70.0
offset_bottom = 503.0
text = "Quit
"
[node name="Part1Out" type="Label" parent="."]
layout_mode = 0
offset_left = 93.0
offset_top = 342.0
offset_right = 212.0
offset_bottom = 365.0
text = "Results go here"
[node name="Error" type="Label" parent="."]
layout_mode = 0
offset_left = 380.0
offset_top = 288.0
offset_right = 420.0
offset_bottom = 311.0
text = "Errors go here"
[node name="Part1Label" type="Label" parent="."]
layout_mode = 0
offset_left = 28.0
offset_top = 340.0
offset_right = 68.0
offset_bottom = 363.0
text = "Part 1:"
[node name="Part2Out" type="Label" parent="."]
offset_left = 91.0
offset_top = 385.0
offset_right = 210.0
offset_bottom = 408.0
text = "Results go here"
[node name="Part2Label" type="Label" parent="."]
offset_left = 26.0
offset_top = 383.0
offset_right = 77.0
offset_bottom = 406.0
text = "Part 2:"
[connection signal="pressed" from="Go" to="." method="_on_go_pressed"]
[connection signal="pressed" from="Quit" to="." method="_on_quit_pressed"]

4
Scenes/2024/2024.gd Normal file
View file

@ -0,0 +1,4 @@
extends Control
func _on_01_pressed() -> void:
get_tree().change_scene_to_file("res://Scenes/2024/01.tscn")

21
Scenes/2024/2024.tscn Normal file
View file

@ -0,0 +1,21 @@
[gd_scene load_steps=2 format=3 uid="uid://bw07oo5tgdkg5"]
[ext_resource type="Script" path="res://Scenes/2024/2024.gd" id="1_yeqs6"]
[node name="2024" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_yeqs6")
[node name="01" type="Button" parent="."]
layout_mode = 0
offset_right = 8.0
offset_bottom = 8.0
text = "01
"
[connection signal="pressed" from="01" to="." method="_on_01_pressed"]

7
Scenes/ui.gd Normal file
View file

@ -0,0 +1,7 @@
extends Control
func _on_exit_pressed() -> void:
get_tree().quit()
func _on_2024_pressed() -> void:
get_tree().change_scene_to_file("res://Scenes/2024/2024.tscn")

34
Scenes/ui.tscn Normal file
View file

@ -0,0 +1,34 @@
[gd_scene load_steps=2 format=3 uid="uid://bknnd7hecv0uc"]
[ext_resource type="Script" path="res://Scenes/ui.gd" id="1_gks0l"]
[node name="UI" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_gks0l")
[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 0
offset_left = 2.0
offset_top = 2.0
offset_right = 1150.0
offset_bottom = 649.0
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
layout_mode = 2
[node name="2024" type="Button" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
text = "2024
"
[node name="Exit" type="Button" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
text = "Exit"
[connection signal="pressed" from="MarginContainer/VBoxContainer/2024" to="." method="_on_2024_pressed"]
[connection signal="pressed" from="MarginContainer/VBoxContainer/Exit" to="." method="_on_exit_pressed"]