Advent_of_code/2020/day5.ahk

63 lines
1.9 KiB
AutoHotkey

#SingleInstance, Force
loc := A_ScriptDir "\data5.txt"
file := FileOpen(loc, "r")
binID := []
decID := []
dialog(str){
MsgBox, 1,, %str%
IfMsgBox, Cancel
Run, day5.ahk
}
while !(file.AtEOF){
line := file.ReadLine()
line := RegExReplace(line, "F|L", "0")
line := RegExReplace(line, "B|R", "1")
binID.Push(line)
decID.Push(Dec(line))
}
SortArray(binID, "D")
SortArray(decID, "D")
dialog("Part 1: " binID[binID.MinIndex()] "`n" decID[decID.MinIndex()])
Dec(x){
b:=StrLen(x),r:=0
loop,parse,x
r|=A_LoopField<<--b
return r
}
; the below is from https://sites.google.com/site/ahkref/custom-functions/sortarray, thank you A_Samurai
SortArray(Array, Order="A") {
;Order A: Ascending, D: Descending, R: Reverse
MaxIndex := ObjMaxIndex(Array)
If (Order = "R") {
count := 0
Loop, % MaxIndex
ObjInsert(Array, ObjRemove(Array, MaxIndex - count++))
Return
}
Partitions := "|" ObjMinIndex(Array) "," MaxIndex
Loop {
comma := InStr(this_partition := SubStr(Partitions, InStr(Partitions, "|", False, 0)+1), ",")
spos := pivot := SubStr(this_partition, 1, comma-1) , epos := SubStr(this_partition, comma+1)
if (Order = "A") {
Loop, % epos - spos {
if (Array[pivot] > Array[A_Index+spos])
ObjInsert(Array, pivot++, ObjRemove(Array, A_Index+spos))
}
} else {
Loop, % epos - spos {
if (Array[pivot] < Array[A_Index+spos])
ObjInsert(Array, pivot++, ObjRemove(Array, A_Index+spos))
}
}
Partitions := SubStr(Partitions, 1, InStr(Partitions, "|", False, 0)-1)
if (pivot - spos) > 1 ;if more than one elements
Partitions .= "|" spos "," pivot-1 ;the left partition
if (epos - pivot) > 1 ;if more than one elements
Partitions .= "|" pivot+1 "," epos ;the right partition
} Until !Partitions
}