Advent_of_code/2020/day5.ahk

74 lines
2.1 KiB
AutoHotkey

#SingleInstance, Force
loc := A_ScriptDir "\data5.txt"
file := FileOpen(loc, "r")
binID := []
decID := []
while !(file.AtEOF){
line := file.ReadLine()
RegExMatch(line, "\S*", line)
line := RegExReplace(line, "F|L", "0")
line := RegExReplace(line, "B|R", "1")
binID.Push(line)
decID.Push(Dec(line))
}
SortArray(binID)
SortArray(decID)
dialog("Part 1: " binID[binID.MaxIndex()] " | " decID[decID.MaxIndex()])
next := decID[decID.MinIndex()]
for k, v in decID
If (v = next){
next := v + 1
} Else {
dialog("Part 2: " v - 1)
ExitApp
}
dialog(str){
MsgBox, 1,, %str%
IfMsgBox, Cancel
Run, day5.ahk
}
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
}